Advertisement
Guest User

andrewhelpsme

a guest
Sep 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class CustomerSim
  5. {
  6. public static void main(String[] args)
  7. {
  8. final double COFFEE_PRICE = 1.5;
  9. final double LATTE_PRICE = 3.5;
  10. final double CAPPUCCINO_PRICE = 3.25;
  11. final double ESPRESSO_PRICE = 2.0;
  12.  
  13. final String COFFEE_NAME = "Coffee";
  14. final String LATTE_NAME = "Latte";
  15. final String CAPPUCCINO_NAME = "Cappuccino";
  16. final String ESPRESSO_NAME = "Espresso";
  17.  
  18. int selection, quantity, numCustomers, runAnotherSimulation = 0;
  19. double totalCost = 0.0;
  20. String selectionName = "";
  21. Random randomNumbers = new Random();
  22. Scanner keyboard = new Scanner(System.in);
  23.  
  24. // use a do-while loop to allow for multiple simulations
  25. do
  26. {
  27. // prompt the user for number of customers
  28. System.out.print("\nEnter the number of customers: ");
  29. numCustomers = keyboard.nextInt();
  30.  
  31. // print the table header
  32. System.out.printf("\n%-15s%-15s%12s%18s\n", "Customer", "Selection",
  33. "Quantity", "Total Cost($)");
  34. System.out.printf("%-15s%-15s%12s%18s\n", "--------", "---------",
  35. "--------", "-------------");
  36.  
  37. // simulate n customers
  38. for (int i = 1; i <= numCustomers; i++)
  39. {
  40. // generate the customer selection and quantity
  41. // 0 - Coffee
  42. // 1 - Latte
  43. // 2 - Cappuccino
  44. // 3 - Espresso
  45. // assumption: quantity is in the range 1 - 5
  46. selection = randomNumbers.nextInt(4);
  47. quantity = randomNumbers.nextInt(5) + 1;
  48.  
  49. // based on the selection, set the name and compute the total cost
  50. switch(selection)
  51. {
  52. case 0: // Coffee
  53. selectionName = COFFEE_NAME;
  54. totalCost = quantity * COFFEE_PRICE;
  55. break;
  56. case 1: // Latte
  57. selectionName = LATTE_NAME;
  58. totalCost = quantity * LATTE_PRICE;
  59. break;
  60. case 2: // Cappuccino
  61. selectionName = CAPPUCCINO_NAME;
  62. totalCost = quantity * CAPPUCCINO_PRICE;
  63. break;
  64. case 3: // Espresso
  65. selectionName = ESPRESSO_NAME;
  66. totalCost = quantity * ESPRESSO_PRICE;
  67. break;
  68. }
  69.  
  70. System.out.printf("%-15s%-15s%12d%18.2f\n", "Customer " + i, selectionName,
  71. quantity, totalCost);
  72. } // end of for loop
  73.  
  74. // prompt the user about running another simulation
  75. System.out.print("\nEnter a 1 to run another simulation or 0 to exit: ");
  76. runAnotherSimulation = keyboard.nextInt();
  77.  
  78. } while(runAnotherSimulation == 1);
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement