arthurbicego / ada-santander-coders-01

Grocery Store Project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ada - Santander Coders: Unit 01

Grocery Store Project

This project is a grocery store software that allows users to create, edit, delete, search for products, and do purchases. Product data is saved in a .txt file.

See the project in Development Environment


Statement

Create a grocery store program with the following options below. For each operation a file must be manipulated to save product data, so that if you exit the program, the information will persist.

Note: You will only need to save the product data in a file.

  • Create product containing the information product name, quantity and price.
  • Edit product, which takes the new information and updates the product chosen by the identifier. (To do this list the products with an identifier being the position of the product).
  • Exclude product, which takes the position of the product and deletes it. (For this, list the products with an identifier being the position of the product)
  • Product search. Get the name or part of it, filter all products that contain the name and show the filtered list.
  • Purchase of products, where the user can choose products and quantities as he wants, as soon as he chooses to finalize, show everything he bought, the prices and the total. When he chooses the product and quantity, check if the product has that quantity, if not, inform the user that it does not contain the quantity of this product in stock. Once the user confirms the purchase, deduct the quantities of the selected products.
  • An exit option that closes the program if the user chooses to.

Concepts

Casting
split[2] = String.valueOf(Integer.parseInt(split[2]) - quantity);
Input
String id = scanner.nextLine();
Output
System.out.println("Are you sure you want to " + method + " Product " + id + "?");
System.out.printf("The total checkout amount is: $%.2f", checkoutValue);
System.out.println();
Array and String
String[] split = productLine.split("\\|");
split[1].toUpperCase().contains(name.toUpperCase())
Objects.equals(split[0], id)
Logical Operator
productLine != null
Selection Statement
if (size == 0) {
      product.setId("0");
} else {
List<String> products = Files.readAllLines(path);
size--;
String[] split = products.get(size).split("\\|");
Integer valueOf = Integer.valueOf(split[0]);
valueOf++;
      product.setId(String.valueOf(valueOf));
      }
Enhanced Switch
switch (choice) {
      case "1" -> {
      groceryController.listProducts();
  }
For, Enhanced For, ForEach
for (int i = 0; i < cartProducts.size(); i++) {
      if (Objects.equals(cartProducts.get(i).getId(), id)) {
      cartProducts.remove(i);
      cart.setProductsCart(cartProducts);
  }
          }
for (Product product : products) {
checkoutValue = checkoutValue + (product.getQuantity() * product.getPrice());
      }
products.forEach(product -> GroceryView.showProduct(product))
Exceptions
try {
quantity = scanner.nextInt();
  scanner.nextLine();
} catch (Exception e) {
      System.out.println();
  System.out.println("Error registering the quantity. Default quantity (1) has been set.");
quantity = 1;
      }
File Handling
Path path = Paths.get("src/tech/ada/products.txt")
Files.readAllLines(path);
Files.write(path, products);
Recursion
public void displayProducts (List<Product> productsCart, int index) {
  if (index < productsCart.size()) {
      Product product = productsCart.get(index);
      String showProduct = product.toString().replaceAll("\n", "");
      GroceryView.showProduct(showProduct);
      displayProducts(productsCart, index + 1);
  }
}
Array List
List<Product> productsCart = cart.getProductsCart();
public class Cart {
private List<Product> productsCart = new ArrayList<>();

public void addProduct(Product product) {
  productsCart.add(product);
}

public List<Product> getProductsCart() {
  return this.productsCart;
}

public void setProductsCart(List<Product> products) {
  this.productsCart = products;
}
}
toString
@Override
public String toString() {
return product.get("id") + "|" + product.get("name") + "|" + product.get("quantity") + "|" + product.get("price") + "\n";
}
HashMap
private Map<String, Object> product = new HashMap<>();

About

Grocery Store Project


Languages

Language:Java 100.0%