Advertisement
__rain1

Products exercise

Jun 2nd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Products {
  4.  
  5.     double usersMoney = 0; // builds up as user buy more stuff.
  6.     Scanner input = new Scanner(System.in);
  7.  
  8.        public static void main(String[] args) {
  9.         Products product = new Products();
  10.         product.menu();
  11.        }
  12.  
  13.     public void menu() {
  14.         printMenu();
  15.         String userInput = input.nextLine().toUpperCase();
  16.         while (!userInput.contains("Q")) {
  17.             switch (userInput) {
  18.                 case "1":
  19.                     printProductMenu();
  20.                     break;
  21.                 case "2":
  22.                     calculateChange();
  23.                     break;
  24.                 case "Q":
  25.                     System.exit(0);
  26.  
  27.  
  28.             }
  29.         }
  30.     }
  31.  
  32.     public static void printMenu(){
  33.         System.out.println("Welcome to the Local Mart. Please select an Option form below: ");
  34.         System.out.println("1 - Display Products");
  35.         System.out.println("2 - Proceed to Purchase");
  36.     }
  37.  
  38.     public void printProductMenu() {
  39.         System.out.println("1 - Soda Bottle, Price: £2.00");
  40.         System.out.println("2 - Packet of Crisps, Price: £1.00");
  41.         System.out.println("3 - Pack of Gum, Price: £0.50p");
  42.         System.out.println("4 - Go back");
  43.         int userInput = input.nextInt();
  44.  
  45.         switch (userInput) {
  46.             case 1:
  47.                 this.usersMoney += 2;
  48.                 System.out.println("Your total is:" + usersMoney);
  49.                 break;
  50.  
  51.             case 2:
  52.                 this.usersMoney += 1;
  53.                 System.out.println("Your total is:" + usersMoney);
  54.                 break;
  55.  
  56.             case 3:
  57.                 this.usersMoney += 0.50;
  58.                 System.out.println("Your total is:" + usersMoney);
  59.                 break;
  60.  
  61.             case 4:
  62.                 menu();
  63.                 break;
  64.         }
  65.     }
  66.  
  67.     public void calculateChange() {
  68.         System.out.println("You have to pay" + usersMoney);
  69.         double change = 0;
  70.         int userInput = input.nextInt();
  71.         while (usersMoney != 0) {
  72.  
  73.             if (userInput < usersMoney) {
  74.                 change = usersMoney - usersMoney;
  75.                 System.out.println("You still have to pay" + change);
  76.             }
  77.             if (userInput > usersMoney) {
  78.                 change = userInput - usersMoney;
  79.                 System.out.println("Your remaining change is" + change);
  80.             }
  81.         }
  82.         System.out.println("Congratulations you've paid the amount, Enjoy :)");
  83.     }
  84.  
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement