Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.26 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class HW4 {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6. //create variables to check to see if the shopper is still shopping and which option they choose. startingBudget will be used at the end to determine the Total Amount.
  7. int stillShopping = 0;
  8. int option = 0;
  9. double budget = 0, startingBudget = 0, amountSpent = 0;
  10. //created quantity variable for use with item selection and addition to cart
  11. int quantity = 0, itemNumber = 1;
  12. //created variables to track whether specific types of items have been bought, and the string which will be printed for the invoice, and the shipping
  13. String invoice = "";
  14. int hasFig = 0, hasFood = 0, hasDvd = 0;
  15. double shippingCost = 0;
  16. //Opening and receive budget, check if budget makes sense, used while loop instead of if loop to make sure that the budget is changed to a positive integer
  17. System.out.printf("Welcome to the Panther Gift Store \n \nPlease enter shopping budget ($): ");
  18. budget = input.nextDouble();
  19. while (budget<0){
  20. System.out.println("Invalid budget.");
  21. System.out.print("Please enter shopping budget ($): ");
  22. budget = input.nextDouble();
  23. }
  24. startingBudget = budget;
  25. System.out.printf("\n================================================================ \n \n");
  26. //make a while loop where the shopper continues shopping until they want to stop
  27. while (stillShopping == 0){
  28. System.out.printf("%-49s %15s%.2f \n", "Panther Gift Store", "Balance: $", budget);
  29. System.out.println("------------------");
  30. System.out.println("1 Panther Dvds");
  31. System.out.println("2 Panther Figurines");
  32. System.out.println("3 Panther Foods \n");
  33. System.out.print("Please select your category (0 to exit): ");
  34. option = input.nextInt();
  35. while (option < 0 || option >= 4 ){
  36. System.out.println("Invalid choice");
  37. System.out.print("Please select your category (0 to exit): ");
  38. option = input.nextInt();
  39. }
  40. System.out.println("");
  41. //This is the main switch to direct the user from the main menu to the sub menus. The submenus are also switches which will add items to the total amount of each item (declared at the beginning of the program)
  42. switch (option){
  43. case 1:
  44. //The DVDs menu, prompts the user for an option then executes the related method
  45. System.out.printf("%-49s %15s%.2f \n", "Panther DVDs", "Balance: $", budget);
  46. System.out.println("------------");
  47. System.out.println("1 The Jungle Book ($10.95)");
  48. System.out.println("2 Monsoon Railway ($8.25)");
  49. System.out.println("3 Great Peaks ($49.95)");
  50. System.out.println("4 Thomas and Friends ($5.00)\n");
  51. System.out.print("Please select your DVD (1-4, 0 to main menu): ");
  52. option = input.nextInt();
  53. while (option < 0 || option >= 5 ){
  54. System.out.println("Invalid choice");
  55. System.out.print("Please select your DVD (1-4, 0 to main menu): ");
  56. option = input.nextInt();
  57. }
  58. switch (option){
  59. case 1:
  60. System.out.print("Please enter quantity (0 to cancel): ");
  61. quantity = input.nextInt();
  62. while (quantity < 0) {
  63. System.out.println("Invalid quantity.");
  64. System.out.print("Please enter quantity (0 to cancel): ");
  65. }
  66. if (quantity == 0){
  67. System.out.println("");
  68. }
  69. else {
  70. if (10.95 * quantity > budget){
  71. System.out.println("Order amount exceeds budget -- cannot proceed.");
  72. }
  73. else {
  74. budget = budget - (10.95*quantity);
  75. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "The Jungle Book", 10.95, quantity, 10.95*quantity);
  76. itemNumber++;
  77. System.out.printf("Added to shopping cart: The Jungle Book [%d] (%.2f)\n\n", quantity, quantity*10.95);
  78. hasDvd = 1;
  79. }
  80. }
  81. break;
  82. case 2:
  83. System.out.print("Please enter quantity (0 to cancel): ");
  84. quantity = input.nextInt();
  85. while (quantity < 0) {
  86. System.out.println("Invalid quantity.");
  87. System.out.print("Please enter quantity (0 to cancel): ");
  88. }
  89. if (quantity == 0){
  90. System.out.println("");
  91. }
  92. else {
  93. if (8.25 * quantity > budget){
  94. System.out.println("Order amount exceeds budget -- cannot proceed.");
  95. }
  96. else {
  97. budget = budget - (8.25*quantity);
  98. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Monsoon Railway", 8.25, quantity, 8.25*quantity);
  99. itemNumber++;
  100. System.out.printf("Added to shopping cart: Monsoon Railway [%d] (%.2f)\n\n", quantity, quantity*8.25);
  101. hasDvd = 1;
  102. }
  103. }
  104. break;
  105. case 3:
  106. System.out.print("Please enter quantity (0 to cancel): ");
  107. quantity = input.nextInt();
  108. while (quantity < 0) {
  109. System.out.println("Invalid quantity.");
  110. System.out.print("Please enter quantity (0 to cancel): ");
  111. }
  112. if (quantity == 0){
  113. System.out.println("");
  114. }
  115. else {
  116. if (49.95 * quantity > budget){
  117. System.out.println("Order amount exceeds budget -- cannot proceed.");
  118. }
  119. else {
  120. budget = budget - (49.95*quantity);
  121. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Great Peaks", 49.95, quantity, 49.95*quantity);
  122. itemNumber++;
  123. System.out.printf("Added to shopping cart: Great Peaks [%d] (%.2f)\n\n", quantity, quantity*49.95);
  124. hasDvd = 1;
  125. }
  126. }
  127. break;
  128. case 4:
  129. System.out.print("Please enter quantity (0 to cancel): ");
  130. quantity = input.nextInt();
  131. while (quantity < 0) {
  132. System.out.println("Invalid quantity.");
  133. System.out.print("Please enter quantity (0 to cancel): ");
  134. }
  135. if (quantity == 0){
  136. System.out.println("");
  137. }
  138. else {
  139. if (5 * quantity > budget){
  140. System.out.println("Order amount exceeds budget -- cannot proceed.");
  141. }
  142. else {
  143. budget = budget - (5*quantity);
  144. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Thomas and Friends", 5, quantity, 5*quantity);
  145. itemNumber++;
  146. System.out.printf("Added to shopping cart: Thomas and Friends [%d] (%.2f)\n\n", quantity, quantity*5);
  147. hasDvd = 1;
  148. }
  149. }
  150. break;
  151. case 0:
  152. System.out.println("");
  153. break;
  154. }
  155. break;
  156. case 2:
  157. //The Figurines menu, prompts the user for an option then executes the related method
  158. System.out.printf("%-49s %15s%.2f \n", "Panther Figurines", "Balance: $", budget);
  159. System.out.println("------------");
  160. System.out.println("1 Thomas the Tank-Engine ($5.75)");
  161. System.out.println("2 Superman ($17.99)");
  162. System.out.println("3 Mickey Mouse (6-piece Set) ($10.00)");
  163. System.out.println("4 Florida Railroad Coach Model ($39.99)\n");
  164. System.out.print("Please select your figurine (1-4, 0 to main menu): ");
  165. option = input.nextInt();
  166. while (option < 0 || option >= 5 ){
  167. System.out.println("Invalid choice");
  168. System.out.print("Please select your figurine (1-4, 0 to main menu): ");
  169. option = input.nextInt();
  170. }
  171. switch (option){
  172. case 1:
  173. System.out.print("Please enter quantity (0 to cancel): ");
  174. quantity = input.nextInt();
  175. while (quantity < 0) {
  176. System.out.println("Invalid quantity.");
  177. System.out.print("Please enter quantity (0 to cancel): ");
  178. }
  179. if (quantity == 0){
  180. System.out.println("");
  181. }
  182. else {
  183. if (5.75 * quantity > budget){
  184. System.out.println("Order amount exceeds budget -- cannot proceed.");
  185. }
  186. else {
  187. budget = budget - (5.75*quantity);
  188. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Thomas the Tank-Engine", 5.75, quantity, 5.75*quantity);
  189. itemNumber++;
  190. System.out.printf("Added to shopping cart: Thomas the Tank-Engine [%d] (%.2f)\n\n", quantity, quantity*5.75);
  191. hasFig = 1;
  192. }
  193. }
  194. break;
  195. case 2:
  196. System.out.print("Please enter quantity (0 to cancel): ");
  197. quantity = input.nextInt();
  198. while (quantity < 0) {
  199. System.out.println("Invalid quantity.");
  200. System.out.print("Please enter quantity (0 to cancel): ");
  201. }
  202. if (quantity == 0){
  203. System.out.println("");
  204. }
  205. else {
  206. if (17.99 * quantity > budget){
  207. System.out.println("Order amount exceeds budget -- cannot proceed.");
  208. }
  209. else {
  210. budget = budget - (17.99*quantity);
  211. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Superman", 17.99, quantity, 17.99*quantity);
  212. itemNumber++;
  213. System.out.printf("Added to shopping cart: Superman [%d] (%.2f)\n\n", quantity, quantity*17.99);
  214. hasFig = 1;
  215. }
  216. }
  217. break;
  218. case 3:
  219. System.out.print("Please enter quantity (0 to cancel): ");
  220. quantity = input.nextInt();
  221. while (quantity < 0) {
  222. System.out.println("Invalid quantity.");
  223. System.out.print("Please enter quantity (0 to cancel): ");
  224. }
  225. if (quantity == 0){
  226. System.out.println("");
  227. }
  228. else {
  229. if (10 * quantity > budget){
  230. System.out.println("Order amount exceeds budget -- cannot proceed.");
  231. }
  232. else {
  233. budget = budget - (10*quantity);
  234. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Micky Mouse (6-piece Set)", 10, quantity, 10*quantity);
  235. itemNumber++;
  236. System.out.printf("Added to shopping cart: Mickey Mouse (6-piece Set) [%d] (%.2f)\n\n", quantity, quantity*10.00);
  237. hasFig = 1;
  238. }
  239. }
  240. break;
  241. case 4:
  242. System.out.print("Please enter quantity (0 to cancel): ");
  243. quantity = input.nextInt();
  244. while (quantity < 0) {
  245. System.out.println("Invalid quantity.");
  246. System.out.print("Please enter quantity (0 to cancel): ");
  247. }
  248. if (quantity == 0){
  249. System.out.println("");
  250. }
  251. else {
  252. if (39.99 * quantity > budget){
  253. System.out.println("Order amount exceeds budget -- cannot proceed.");
  254. }
  255. else {
  256. budget = budget - (39.99*quantity);
  257. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Florida Railroad Coach Model", 39.99, quantity, 39.99*quantity);
  258. itemNumber++;
  259. System.out.printf("Added to shopping cart: Florida Railroad Coach Model [%d] (%.2f)\n\n", quantity, quantity*39.99);
  260. hasFig = 1;
  261. }
  262. }
  263. break;
  264. case 0:
  265. System.out.println("");
  266. break;
  267. }
  268. break;
  269. case 3:
  270. //The Food menu, prompts the user for an option then executes the related method
  271. System.out.printf("%-49s %15s%.2f \n", "Panther Foods", "Balance: $", budget);
  272. System.out.println("------------");
  273. System.out.println("1 Sun Chips (2-pack) ($4.18)");
  274. System.out.println("2 Corn on the Cob ($2.40)");
  275. System.out.println("3 Orange Juice (12-pack) ($8.00)");
  276. System.out.print("Please select your food (1-3, 0 to main menu): ");
  277. option = input.nextInt();
  278. while (option < 0 || option >= 4 ){
  279. System.out.println("Invalid choice");
  280. System.out.print("Please select your food (1-3, 0 to main menu): ");
  281. option = input.nextInt();
  282. }
  283. switch (option){
  284. case 1:
  285. System.out.print("Please enter quantity (0 to cancel): ");
  286. quantity = input.nextInt();
  287. while (quantity < 0) {
  288. System.out.println("Invalid quantity.");
  289. System.out.print("Please enter quantity (0 to cancel): ");
  290. }
  291. if (quantity == 0){
  292. System.out.println("");
  293. }
  294. else {
  295. if (4.18 * quantity > budget){
  296. System.out.println("Order amount exceeds budget -- cannot proceed.");
  297. }
  298. else {
  299. budget = budget - (4.18*quantity);
  300. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Sun Chips", 4.18, quantity, 4.18*quantity);
  301. itemNumber++;
  302. System.out.printf("Added to shopping cart: Sun Chips [%d] (%.2f)\n\n", quantity, quantity*4.18);
  303. hasFood = 1;
  304. }
  305. }
  306. break;
  307. case 2:
  308. System.out.print("Please enter quantity (0 to cancel): ");
  309. quantity = input.nextInt();
  310. while (quantity < 0) {
  311. System.out.println("Invalid quantity.");
  312. System.out.print("Please enter quantity (0 to cancel): ");
  313. }
  314. if (quantity == 0){
  315. System.out.println("");
  316. }
  317. else {
  318. if (2.4 * quantity > budget){
  319. System.out.println("Order amount exceeds budget -- cannot proceed.");
  320. }
  321. else {
  322. budget = budget - (2.4*quantity);
  323. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Corn on the Cob (6-pack)", 2.4, quantity, 2.4*quantity);
  324. itemNumber++;
  325. System.out.printf("Added to shopping cart: Corn on the Cob (6-pack) [%d] (%.2f)\n\n", quantity, quantity*2.4);
  326. hasFood = 1;
  327. }
  328. }
  329. break;
  330. case 3:
  331. System.out.print("Please enter quantity (0 to cancel): ");
  332. quantity = input.nextInt();
  333. while (quantity < 0) {
  334. System.out.println("Invalid quantity.");
  335. System.out.print("Please enter quantity (0 to cancel): ");
  336. }
  337. if (quantity == 0){
  338. System.out.println("");
  339. }
  340. else {
  341. if (8 * quantity > budget){
  342. System.out.println("Order amount exceeds budget -- cannot proceed.");
  343. }
  344. else {
  345. budget = budget - (8*quantity);
  346. invoice = invoice + String.format("\n%-3d %-38s %-9.2f %3d %10.2f", itemNumber, "Orange Juice (12-pack)", 8, quantity, 8*quantity);
  347. itemNumber++;
  348. System.out.printf("Added to shopping cart: Orange Juice (12-pack) [%d] (%.2f)\n\n", quantity, quantity*8);
  349. hasFood = 1;
  350. }
  351. }
  352. break;
  353. case 0:
  354. System.out.println("");
  355. break;
  356. }
  357. break;
  358. case 0:
  359. //Ends the shopping loop and sends the user to the invoice menu
  360. stillShopping = 1;
  361. amountSpent = startingBudget - budget;
  362. break;
  363. }
  364. }
  365. //Left to do: add shipping costs to order, create invoice, print invoice
  366. //create catch. If nothing was bought, the program ends.
  367. if (hasDvd == 0 && hasFig == 0 && hasFood == 0){}
  368. else {
  369. if (hasDvd == 1){
  370. shippingCost = shippingCost + 1;
  371. }
  372. if (hasFig == 1){
  373. shippingCost = shippingCost + 7;
  374. }
  375. if (hasFood == 1){
  376. shippingCost = shippingCost + 3;
  377. }
  378. //time to output the invoice. the bulk of the invoice content was created as the items were purchased, since items aren't to be clubbed together.
  379. System.out.println("====================== Invoice - Panther Home ======================");
  380. System.out.printf("%8s %39s %7s %10s \n", "Item", "Price", "Qty", "Amount");
  381. System.out.print("--------------------------------------------------------------------");
  382. System.out.println(invoice);
  383. System.out.printf("%13s %53.2f\n", "Shipping:", shippingCost);
  384. System.out.println("--------------------------------------------------------------------");
  385. System.out.printf("%19s %47.2f\n", "Total Amount", amountSpent + shippingCost);
  386. System.out.println("--------------------------------------------------------------------");
  387. System.out.println("");
  388. System.out.print("Thank you for shopping at Panther Gift Store");
  389. }
  390. }
  391. }
Add Comment
Please, Sign In to add comment