Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
  2. at ShoppingCart.add(ShoppingCart.java:25)
  3. at ShoppingCart.main(ShoppingCart.java:99)
  4.  
  5. import java.util.Scanner; //Used to read user input from keyboard
  6. import java.text.DecimalFormat; //Used to format output of decimal values
  7.  
  8. public class IceCreamOrder
  9. {
  10. //Private instance variable declarations
  11. private String flavor;
  12. private String vessel;
  13. private String amount;
  14. private double unitPrice;
  15. private int quantity;
  16.  
  17. //Constructor declarations
  18. public IceCreamOrder(String flavor, String vessel, String amount, double unitPrice, int quantity)
  19. {
  20. this.flavor = flavor;
  21. this.vessel = vessel;
  22. this.amount = amount;
  23. this.unitPrice = unitPrice;
  24. this.quantity = quantity;
  25. }
  26.  
  27. public IceCreamOrder(String flavor, String vessel, String amount, double unitPrice)
  28. {
  29. this.flavor = flavor;
  30. this.vessel = vessel;
  31. this.amount = amount;
  32. this.unitPrice = unitPrice;
  33. this.quantity = 1;
  34. }
  35.  
  36. public IceCreamOrder()
  37. {
  38. this.flavor = "";
  39. this.vessel = "";
  40. this.amount = "";
  41. this.unitPrice = 0.0;
  42. this.quantity = 0;
  43. }
  44.  
  45. //Calculates the total price of order
  46. public double price()
  47. {
  48. return quantity * unitPrice;
  49. }
  50.  
  51. //Accessor method declarations
  52. public String getFlavor()
  53. {
  54. return flavor;
  55. }
  56.  
  57. public String getVessel()
  58. {
  59. return vessel;
  60. }
  61.  
  62. public String getAmount()
  63. {
  64. return amount;
  65. }
  66.  
  67. public double getUnitPrice()
  68. {
  69. return unitPrice;
  70. }
  71.  
  72. public int getQuantity()
  73. {
  74. return quantity;
  75. }
  76.  
  77. //Mutator method declarations
  78. public void setFlavor(String flavor)
  79. {
  80. this.flavor = flavor;
  81. }
  82.  
  83. public void setVessel(String vessel)
  84. {
  85. this.vessel = vessel;
  86. }
  87.  
  88. public void setAmount(String amount)
  89. {
  90. this.amount = amount;
  91. }
  92.  
  93. public void setUnitPrice(double unitPrice)
  94. {
  95. this.unitPrice = unitPrice;
  96. }
  97.  
  98. public void setQuantity(int quantity)
  99. {
  100. this.quantity = quantity;
  101. }
  102.  
  103. //toString method declaration
  104. public String toString()
  105. {
  106. DecimalFormat pattern0dot00 = new DecimalFormat("$0.00");
  107.  
  108. return (((getQuantity() == 1) ? (getQuantity() + " order") : (getQuantity() + " orders")) + " of " +
  109. getAmount() + " of " + getFlavor() + " ice cream in a " + getVessel() + " for " +
  110. pattern0dot00.format(price()) + " = " + getQuantity() + " x " + getUnitPrice());
  111. }
  112.  
  113. public static void main(String[] args)
  114. {
  115. //Object declarations
  116. IceCreamOrder newOrder = new IceCreamOrder();
  117. Scanner keyboard = new Scanner(System.in);
  118.  
  119. //Array declarations
  120. String[] flavorList = {"Avocado", "Banana", "Chocolate", "Hazelnut", "Lemon", "Mango", "Mocha", "Vanilla"};
  121. String[] vesselList = {"Cone", "Cup", "Sundae"};
  122. String[] amountList = {"Single Scoop", "Double Scoop", "Triple Scoop"};
  123. String[] quantityList = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"};
  124.  
  125. System.out.println("Placing an order is as easy as ABC, and D.");
  126. System.out.println("Step A: Select your favorite flavour");
  127. String outputFlavor = "";
  128. for (int i = 1; i <= flavorList.length; i++)
  129. {
  130. outputFlavor = " (" + i + ") " + flavorList[i-1];
  131. System.out.println(outputFlavor);
  132. }
  133.  
  134. System.out.print("?-> Enter an option number: ");
  135.  
  136. int inputFlavor = keyboard.nextInt();
  137. String flavorString = "";
  138.  
  139. switch (inputFlavor)
  140. {
  141. case 1:
  142. flavorString = flavorList[0];
  143. break;
  144. case 2:
  145. flavorString = flavorList[1];
  146. break;
  147. case 3:
  148. flavorString = flavorList[2];
  149. break;
  150. case 4:
  151. flavorString = flavorList[3];
  152. break;
  153. case 5:
  154. flavorString = flavorList[4];
  155. break;
  156. case 6:
  157. flavorString = flavorList[5];
  158. break;
  159. case 7:
  160. flavorString = flavorList[6];
  161. break;
  162. case 8:
  163. flavorString = flavorList[7];
  164. break;
  165. }
  166.  
  167. newOrder.setFlavor(flavorString);
  168. System.out.println();
  169.  
  170. System.out.println("Step B: Select a vessel for your ice cream:");
  171. String outputVessel = "";
  172. for (int i = 1; i <= vesselList.length; i++)
  173. {
  174. outputVessel = " (" + i + ") " + vesselList[i-1];
  175. System.out.println(outputVessel);
  176. }
  177.  
  178. System.out.print("?-> Enter an option number: ");
  179.  
  180. int inputVessel = keyboard.nextInt();
  181. String vesselString = "";
  182.  
  183. switch (inputVessel)
  184. {
  185. case 1:
  186. vesselString = vesselList[0];
  187. break;
  188. case 2:
  189. vesselString = vesselList[1];
  190. break;
  191. case 3:
  192. vesselString = vesselList[2];
  193. break;
  194. }
  195.  
  196. newOrder.setVessel(vesselString);
  197. System.out.println();
  198.  
  199. System.out.println("Step C: How much ice cream?");
  200. String outputAmount = "";
  201. for (int i = 1; i <= amountList.length; i++)
  202. {
  203. outputAmount = " (" + i + ") " + amountList[i-1];
  204. System.out.println(outputAmount);
  205. }
  206.  
  207. System.out.print("?-> Enter an option number: ");
  208.  
  209. int inputAmount = keyboard.nextInt();
  210. String amountString = "";
  211.  
  212. switch (inputAmount)
  213. {
  214. case 1:
  215. amountString = amountList[0];
  216. break;
  217. case 2:
  218. amountString = amountList[1];
  219. break;
  220. case 3:
  221. amountString = amountList[2];
  222. break;
  223. }
  224.  
  225. newOrder.setAmount(amountString);
  226. System.out.println();
  227.  
  228. System.out.println("Step D: How many orders of your current selection?");
  229. String outputQuantity = "";
  230. for (int i = 1; i <= quantityList.length; i++)
  231. {
  232. outputQuantity = " (" + i + ") " + quantityList[i-1];
  233. System.out.println(outputQuantity);
  234. }
  235.  
  236. System.out.print("?-> Enter how many orders: ");
  237.  
  238. int inputQuantity = keyboard.nextInt();
  239. newOrder.setQuantity(inputQuantity);
  240. System.out.println();
  241.  
  242. if (newOrder.getAmount() == amountList[0])
  243. {
  244. if (newOrder.getVessel() == vesselList[0])
  245. {
  246. newOrder.setUnitPrice(2.99);
  247. }
  248.  
  249. else if (newOrder.getVessel() == vesselList[1])
  250. {
  251. newOrder.setUnitPrice(3.49);
  252. }
  253.  
  254. else
  255. {
  256. newOrder.setUnitPrice(4.25);
  257. }
  258. }
  259.  
  260. else if (newOrder.getAmount() == amountList[1])
  261. {
  262. if (newOrder.getVessel() == vesselList[0])
  263. {
  264. newOrder.setUnitPrice(3.99);
  265. }
  266.  
  267. else if (newOrder.getVessel() == vesselList[1])
  268. {
  269. newOrder.setUnitPrice(4.49);
  270. }
  271.  
  272. else
  273. {
  274. newOrder.setUnitPrice(5.25);
  275. }
  276. }
  277.  
  278. else
  279. {
  280. if (newOrder.getVessel() == vesselList[0])
  281. {
  282. newOrder.setUnitPrice(4.99);
  283. }
  284.  
  285. else if (newOrder.getVessel() == vesselList[1])
  286. {
  287. newOrder.setUnitPrice(5.49);
  288. }
  289.  
  290. else
  291. {
  292. newOrder.setUnitPrice(6.25);
  293. }
  294. }
  295.  
  296. System.out.println(newOrder);
  297. }
  298.  
  299. public class ShoppingCart
  300. {
  301. private IceCreamOrder[] shoppingCart;
  302. private int maxQuantity;
  303. private int orderTracker;
  304.  
  305. //Constructor declarations
  306. public ShoppingCart()
  307. {
  308. this.shoppingCart = new IceCreamOrder[maxQuantity];
  309. this.maxQuantity = 5;
  310. this.orderTracker = 1;
  311. }
  312.  
  313. public void add(IceCreamOrder order)
  314. {
  315. if (orderTracker > maxQuantity)
  316. {
  317. System.out.println("Shopping cart is full.");
  318. }
  319.  
  320. else
  321. {
  322. shoppingCart[orderTracker - 1] = order;
  323. orderTracker++;
  324. }
  325. }
  326.  
  327. //Method determines if shopping cart is empty
  328. public boolean isEmpty()
  329. {
  330. int orderCount = 0;
  331.  
  332. for (int i = 0; i < shoppingCart.length; i++)
  333. {
  334. if (shoppingCart[i] != null)
  335. {
  336. orderCount++;
  337. }
  338. }
  339.  
  340. return ((orderCount == 0) ? true : false);
  341. }
  342.  
  343. //Method determines if shopping cart is full
  344. public boolean isFull()
  345. {
  346. int orderCount = 0;
  347.  
  348. for (int i = 0; i < shoppingCart.length; i++)
  349. {
  350. if (shoppingCart[i] != null)
  351. {
  352. orderCount++;
  353. }
  354. }
  355.  
  356. return ((orderCount == maxQuantity) ? true : false);
  357. }
  358.  
  359. public IceCreamOrder get(int position)
  360. {
  361. return shoppingCart[position-1];
  362. }
  363.  
  364. //Method determines the number of orders currently in shopping cart
  365. public int size()
  366. {
  367. int orderCount = 0;
  368.  
  369. for (int i = 0; i < shoppingCart.length; i++)
  370. {
  371. if (shoppingCart[i] != null)
  372. {
  373. orderCount++;
  374. }
  375. }
  376.  
  377. return orderCount;
  378. }
  379.  
  380. public static void main(String[] args)
  381. {
  382. ShoppingCart newCart = new ShoppingCart();
  383. IceCreamOrder firstOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
  384. IceCreamOrder secondOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
  385. IceCreamOrder thirdOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
  386. IceCreamOrder fourthOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
  387. IceCreamOrder fifthOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
  388. //IceCreamOrder sixthOrder = new IceCreamOrder("Vanilla", "Cone", "Single Scoop", 2.99);
  389.  
  390. newCart.add(firstOrder);
  391. newCart.add(secondOrder);
  392. newCart.add(thirdOrder);
  393. newCart.add(fourthOrder);
  394. newCart.add(fifthOrder);
  395. //newCart.add(sixthOrder);*/
  396.  
  397. System.out.println(newCart.size());
  398. System.out.println(firstOrder);
  399. System.out.println(newCart.isEmpty());
  400. System.out.println(newCart.isFull());
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement