MainApplication.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*Built by Rachelle and Nafis
  2. * runAdvanced built by Nafis
  3. */
  4. import java.util.Scanner;
  5. import java.util.InputMismatchException;
  6. public class MainApplication {
  7. Scanner scanner = new Scanner(System.in);
  8. Product lemons = new Product("lemon",584389);
  9. Product sugar = new Product("sugar",590452);
  10. Product water = new Product("water",590329);
  11. Product[] products = {lemons, sugar, water};
  12. Product[] newProducts;
  13. Inventory inventory = new Inventory(products);
  14. public void runAdvanced(){
  15. int numProducts;
  16. System.out.println("How many products do you have?");
  17. numProducts = getNumberProds();
  18. System.out.println("You have " + numProducts + " product(s)!");
  19. newProducts = new Product[numProducts];
  20. if (numProducts > 0){
  21. for (int i = 0; i < numProducts; i++){
  22. Scanner input = new Scanner(System.in);
  23. System.out.println("What is product " + (i+1) + "?");
  24. String productName = input.next();
  25. System.out.println("What is it's price");
  26. double productPrice = input.nextDouble();
  27. System.out.println("What is it's ID?");
  28. int productID = input.nextInt();
  29. System.out.println("How many do you have?");
  30. int productQuantity = input.nextInt();
  31. input.close();
  32. newProducts[i] = new Product(productName, productPrice, productID, productQuantity);
  33. }
  34. }
  35. double total = inventory.getWholeInventoryValue(newProducts);
  36. StringBuilder list = new StringBuilder();
  37. for (Product element: newProducts){
  38. list.append(element.getName() + " ");
  39. }
  40. System.out.println("Total value of your inventory: "+total+"\n"
  41. + "Your products are: \n" + list.toString());
  42. }
  43. public static int getNumberProds(){
  44. int numberProds = 0;
  45. Scanner input = new Scanner(System.in);
  46. boolean error = true;
  47. do{
  48. try{
  49. numberProds = input.nextInt();
  50. error = false;
  51. } catch (InputMismatchException e){
  52. input.next();
  53. System.out.println("Error! Please type a number");
  54. }
  55. } while (error);
  56. if (numberProds == 0){
  57. return 0;
  58. }
  59. return numberProds;
  60. }
  61. public void runManager(){
  62. for(int i = 0; i < 3; i++){
  63. System.out.println("Enter price, quantity of "+ products[i].getName());
  64. double price = scanner.nextInt();
  65. products[i].setPrice(price);
  66. int quantity = scanner.nextInt();
  67. products[i].setQuantity(quantity);
  68. }
  69. double total = inventory.getWholeInventoryValue(products);
  70. System.out.println("Total value of your inventory: "+total);
  71. }
  72. }