1.  
  2. import java.util.Scanner;
  3. import java.util.ArrayList;
  4. import java.io.*;
  5.  
  6. /**
  7. * Shop.java
  8. *
  9. * Uses the Item class to create items and add them to a shopping
  10. * cart stored in an ArrayList.
  11. *
  12. * ACIT1515, Fall 2011, Lab 7
  13. * David Tran a00801942
  14. *
  15. */
  16.  
  17. public class Shop {
  18.  
  19. public static void main(String[] args) throws IOException{
  20. Item item;
  21. ArrayList <String> cart = new ArrayList<String>();
  22. Scanner scan = new Scanner(System.in);
  23. Scanner fileScan, lineScan;
  24. String fileName, itemName;
  25. double cartTotal = 0;
  26.  
  27. System.out.println("==================================================");
  28. System.out.println("||---------Smart Shoppers Shopping System-------||");
  29. System.out.println("==================================================");
  30. System.out.println();
  31. System.out.print("Enter your shopping list: ");
  32. fileName = scan.nextLine();
  33. fileScan = new Scanner(new File(fileName));
  34. do {
  35. double itemPrice = 0;
  36. int itemQuantity = 0;
  37. String itemInfo = "";
  38. itemInfo = fileScan.nextLine();
  39. lineScan = new Scanner(itemInfo);
  40. lineScan.useDelimiter(",");
  41. // using the comma as the delimiter to seperate name, price and quantity.
  42.  
  43. itemName = lineScan.next();
  44. // lets the first set of string variables up to the first comma to
  45. // be the name of the item
  46.  
  47. while(lineScan.hasNext()){
  48. itemPrice = lineScan.nextDouble();
  49. itemQuantity = lineScan.nextInt();
  50.  
  51. cartTotal += (itemPrice*itemQuantity);
  52. // accumulatively adds up the total amount of each item's price
  53. // multiplied by their quantity
  54. item = new Item(itemName, itemPrice, itemQuantity);
  55. cart.add(item.toString());
  56. // calling the ArrayList function to add the toString method of
  57. // each item into the cells of the List.
  58. System.out.println(item.toString());
  59. // Also prints out the toString of the item class.
  60. }
  61. } while (fileScan.hasNextLine());
  62.  
  63. double cutoff = 0;
  64. // let the cutoff equal zero, and do the following whenever the cutoff
  65. // is never equal to zero. If the cutoff is zero, there is no need to
  66. // remove any more items that cost more than the cutoff.
  67. do{
  68. if (cutoff > 0){
  69. // if it is greater than zero, do this.
  70.  
  71. // No such thing is negative currency
  72. // unless we owe customers money. In this program, we do not
  73. // do refunds.
  74.  
  75. Scanner fileScan2 = new Scanner(new File(fileName));
  76. // do a new file scan of our shopping list
  77. cartTotal = 0;
  78. // reset the total amount owed to zero.
  79. cart.clear();
  80. // clear all contents of the array.
  81. do {
  82.  
  83. double itemPrice = 0;
  84. int itemQuantity = 0;
  85. String itemInfo = "";
  86. itemInfo = fileScan2.nextLine();
  87. lineScan = new Scanner(itemInfo);
  88. lineScan.useDelimiter(",");
  89.  
  90. itemName = lineScan.next();
  91.  
  92. while(lineScan.hasNext()){
  93. itemPrice = lineScan.nextDouble();
  94. if (itemPrice > cutoff){
  95. // if the price of the item is greater than the
  96. // cutoff point, terminate. Otherwise, continue.
  97. break;
  98. }
  99. else {
  100. itemQuantity = lineScan.nextInt();
  101. cartTotal += (itemPrice*itemQuantity);
  102. item = new Item(itemName, itemPrice, itemQuantity);
  103. cart.add(item.toString());
  104. System.out.println(item.toString());
  105. }
  106. }
  107.  
  108. } while (fileScan2.hasNextLine());
  109.  
  110. System.out.println();
  111. System.out.println("Your Shopping Cart:");
  112. System.out.println();
  113. // System.out.println(cart.toString());
  114. // Above looks ugly, trying a different method
  115. int index = 0;
  116. while (index < cart.size()){
  117. System.out.println(cart.get(index));
  118. index++;
  119. }
  120. System.out.println("-------------------------------------------------");
  121. System.out.println("Your total: $" + cartTotal);
  122. System.out.println();
  123. System.out.print("Cutoff Price for Cart: ");
  124. cutoff = scan.nextDouble();
  125. System.out.println();
  126. // continue doing this until the customer is satisfied with the
  127. // cutoff point of their shopping list.
  128. }
  129. else{
  130. System.out.println();
  131. System.out.println("Your Shopping Cart:");
  132. System.out.println();
  133. // System.out.println(cart.toString());
  134. // Above looks ugly, trying a different method
  135. int index = 0;
  136. while (index < cart.size()){
  137. System.out.println(cart.get(index));
  138. index++;
  139. }
  140. System.out.println("-------------------------------------------------");
  141. System.out.println("Your total: $" + cartTotal);
  142. System.out.println();
  143. System.out.print("Cutoff Price for Cart: ");
  144. cutoff = scan.nextDouble();
  145. System.out.println();
  146. }
  147. } while (cutoff != 0);
  148. }
  149. }
  150.  
  151.