Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main Class
- package inventory;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.io.FileReader;
- import java.io.FileNotFoundException;
- public class Inventory
- {
- private static Scanner scan = new Scanner(System.in);
- public static void main(String[] args)
- {
- ArrayList<Product> InventoryList = new ArrayList<>();
- Inventory I = new Inventory();
- int option = 0;
- Inventory.openFile(InventoryList);
- while(option != 4)
- {
- System.out.println("1.) Add items to the inventory");
- System.out.println("2.) Show items in the inventory");
- System.out.println("3.) Show the total cost of the inventory.");
- System.out.println("4.) Quit the program.");
- option = scan.nextInt();
- switch(option)
- {
- case 1:
- I.addItems(InventoryList);
- break;
- case 2:
- I.showInventory(InventoryList);
- break;
- case 3:
- I.showTotalCost(InventoryList);
- break;
- case 4:
- System.out.println("\nApplication Terminated.");
- break;
- default:
- System.out.println("This option does not exist in the menu. Try again");
- break;
- }
- }
- }
- public static void openFile(ArrayList<Product> p)
- {
- Scanner scan = null;
- String firstLine = "";
- String line;
- try
- {
- scan = new Scanner(new FileReader("product.csv"));
- firstLine = scan.nextLine();
- while(scan.hasNextLine())
- {
- line = scan.nextLine();
- String[] items = line.split(",");
- Product product = new Product();
- product.setProductName(items[0]);
- product.setQuantity(Integer.parseInt(items[1]));
- product.setPrice(Double.parseDouble(items[2]));
- product.setUPC(items[3]);
- p.add(product);
- }
- scan.close();
- }
- catch(FileNotFoundException e)
- {
- System.err.println("File was not found");
- }
- }
- public static void writeFile(ArrayList<Product> p)
- {
- }
- public void addItems(ArrayList<Product> p)
- {
- int option;
- System.out.println("");
- System.out.println("1.) Add a new product to the inventory.");
- System.out.println("2.) Increase the quantity of an existing product.");
- option = scan.nextInt();
- if(option == 1)
- {
- String productName;
- int quantity;
- double price;
- String UPC;
- scan.nextLine();
- System.out.println("\nEnter the name of the product?");
- productName = scan.nextLine();
- System.out.println("\nHow many of these items do you have?");
- quantity = scan.nextInt();
- System.out.println("\nWhat is the price of this item?");
- price = scan.nextDouble();
- scan.nextLine();
- System.out.println("\nWhat is the UPC number for this item?");
- UPC = scan.nextLine();
- if(p.size() > 0)
- {
- for (Product pro : p)
- {
- while(pro.getUPC().equals(UPC))
- {
- System.out.println("\nNo duplicates allowed, try again.");
- UPC = scan.nextLine();
- }
- }
- }
- Product product = new Product(productName, quantity, price, UPC);
- p.add(product);
- }
- else if(option == 2)
- {
- String oldProduct;
- String response;
- Inventory I = new Inventory();
- scan.nextLine();
- System.out.println("\nEnter the Name of the product.");
- oldProduct = scan.nextLine();
- for(int i=0; ; i++)
- {
- if (p.get(i).getProductName().equalsIgnoreCase(oldProduct))
- {
- }
- else if(i == p.size()-1)
- {
- System.out.println("\nThis product is not in our inventory.");
- break;
- }
- }
- System.out.println("\nIs there another item that you would like to search by Name? ");
- response = scan.nextLine();
- while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
- {
- System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
- response = scan.nextLine();
- }
- if(response.equalsIgnoreCase("Yes"))
- {
- System.out.println("\nStarting a new search");
- I.showInventory(p);
- }
- else if(response.equalsIgnoreCase("No"))
- {
- System.out.println("\nReturning to the main menu\n");
- }
- }
- System.out.println("");
- }
- public void showInventory(ArrayList<Product> p)
- {
- int choice;
- String response;
- Inventory I = new Inventory();
- System.out.println("");
- System.out.println("1.) Search the inventory by row");
- System.out.println("2.) Search the entire inventory");
- System.out.println("3.) Search the inventory by UPC ID");
- System.out.println("4.) Search the inventory by name");
- choice = scan.nextInt();
- scan.nextLine();
- if(choice == 1)
- {
- int arrayLimit = p.size();
- int row;
- System.out.println("\nEnter the row that you would like to search");
- row = scan.nextInt();
- while(row > arrayLimit || row <= 0)
- {
- System.out.println("\nSorry, that row doesn't exist. Try again");
- row = scan.nextInt();
- }
- System.out.print("\nItem found: ");
- System.out.println(p.get(row-1));
- scan.nextLine();
- System.out.println("\nIs there another item that you would like to seacrh by row? ");
- response = scan.nextLine();
- while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
- {
- System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
- response = scan.nextLine();
- }
- if(response.equalsIgnoreCase("Yes"))
- {
- System.out.println("\nStarting a new search");
- I.showInventory(p);
- }
- else if(response.equalsIgnoreCase("No"))
- {
- System.out.println("\nReturning to the main menu\n");
- }
- }
- else if(choice == 2)
- {
- System.out.println("");
- for (Product p1 : p)
- {
- System.out.println(p1);
- }
- System.out.println("");
- }
- else if(choice == 3)
- {
- String UPC;
- System.out.println("\nEnter the UPC serial number.");
- UPC = scan.nextLine();
- System.out.print("\nItem found: ");
- for (Product p1 : p)
- {
- if (p1.getUPC().equals(UPC))
- {
- System.out.println(p1);
- }
- }
- System.out.println("\nIs there another item that you would like to search by UPC? ");
- response = scan.nextLine();
- while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
- {
- System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
- response = scan.nextLine();
- }
- if(response.equalsIgnoreCase("Yes"))
- {
- System.out.println("\nStarting a new search");
- I.showInventory(p);
- }
- else if(response.equalsIgnoreCase("No"))
- {
- System.out.println("\nReturning to the main menu\n");
- }
- }
- else if(choice == 4)
- {
- String productName;
- System.out.println("\nEnter the Name of the product.");
- productName= scan.nextLine();
- for(int i=0; ; i++)
- {
- if (p.get(i).getProductName().equalsIgnoreCase(productName))
- {
- System.out.print("\nItem found: ");
- System.out.println(p.get(i));
- break;
- }
- else if(i == p.size()-1)
- {
- System.out.println("\nThis product is not in our inventory.");
- break;
- }
- }
- System.out.println("\nIs there another item that you would like to search by Name? ");
- response = scan.nextLine();
- while(!(response.equalsIgnoreCase("Yes")) && !(response.equalsIgnoreCase("No")))
- {
- System.out.println("\nOnly, Yes or No are acceptable inputs. Try again.");
- response = scan.nextLine();
- }
- if(response.equalsIgnoreCase("Yes"))
- {
- System.out.println("\nStarting a new search");
- I.showInventory(p);
- }
- else if(response.equalsIgnoreCase("No"))
- {
- System.out.println("\nReturning to the main menu\n");
- }
- }
- }
- public void showTotalCost(ArrayList<Product> p)
- {
- double totalCost = 0;
- for(int i=0; i<p.size(); i++)
- {
- totalCost += p.get(i).getPrice() * p.get(i).getQuantity();
- }
- System.out.println("");
- System.out.printf("The total cost of the entire inventory is %.2f",totalCost);
- System.out.println("\n");
- }
- }
- Product Class
- package inventory;
- public class Product
- {
- private String productName;
- private int quantity;
- private double price;
- private String UPC;
- public Product(String productName, int quantity, double price, String UPC)
- {
- this.productName = productName;
- this.quantity = quantity;
- this.price = price;
- this.UPC = UPC;
- }
- public Product()
- {
- }
- public String getProductName()
- {
- return productName;
- }
- public int getQuantity()
- {
- return quantity;
- }
- public double getPrice()
- {
- return price;
- }
- public String getUPC()
- {
- return UPC;
- }
- public void setProductName(String productName)
- {
- this.productName = productName;
- }
- public void setQuantity(int quantity)
- {
- this.quantity = quantity;
- }
- public void setPrice(double price)
- {
- this.price = price;
- }
- public void setUPC(String UPC)
- {
- this.UPC = UPC;
- }
- @Override
- public String toString()
- {
- return productName + ", " + quantity + ", " + price + ", " + UPC;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement