Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class CustomerSim
- {
- public static void main(String[] args)
- {
- final double COFFEE_PRICE = 1.5;
- final double LATTE_PRICE = 3.5;
- final double CAPPUCCINO_PRICE = 3.25;
- final double ESPRESSO_PRICE = 2.0;
- final String COFFEE_NAME = "Coffee";
- final String LATTE_NAME = "Latte";
- final String CAPPUCCINO_NAME = "Cappuccino";
- final String ESPRESSO_NAME = "Espresso";
- int selection, quantity, numCustomers, runAnotherSimulation = 0;
- double totalCost = 0.0;
- String selectionName = "";
- Random randomNumbers = new Random();
- Scanner keyboard = new Scanner(System.in);
- // use a do-while loop to allow for multiple simulations
- do
- {
- // prompt the user for number of customers
- System.out.print("\nEnter the number of customers: ");
- numCustomers = keyboard.nextInt();
- // print the table header
- System.out.printf("\n%-15s%-15s%12s%18s\n", "Customer", "Selection",
- "Quantity", "Total Cost($)");
- System.out.printf("%-15s%-15s%12s%18s\n", "--------", "---------",
- "--------", "-------------");
- // simulate n customers
- for (int i = 1; i <= numCustomers; i++)
- {
- // generate the customer selection and quantity
- // 0 - Coffee
- // 1 - Latte
- // 2 - Cappuccino
- // 3 - Espresso
- // assumption: quantity is in the range 1 - 5
- selection = randomNumbers.nextInt(4);
- quantity = randomNumbers.nextInt(5) + 1;
- // based on the selection, set the name and compute the total cost
- switch(selection)
- {
- case 0: // Coffee
- selectionName = COFFEE_NAME;
- totalCost = quantity * COFFEE_PRICE;
- break;
- case 1: // Latte
- selectionName = LATTE_NAME;
- totalCost = quantity * LATTE_PRICE;
- break;
- case 2: // Cappuccino
- selectionName = CAPPUCCINO_NAME;
- totalCost = quantity * CAPPUCCINO_PRICE;
- break;
- case 3: // Espresso
- selectionName = ESPRESSO_NAME;
- totalCost = quantity * ESPRESSO_PRICE;
- break;
- }
- System.out.printf("%-15s%-15s%12d%18.2f\n", "Customer " + i, selectionName,
- quantity, totalCost);
- } // end of for loop
- // prompt the user about running another simulation
- System.out.print("\nEnter a 1 to run another simulation or 0 to exit: ");
- runAnotherSimulation = keyboard.nextInt();
- } while(runAnotherSimulation == 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement