Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Products {
- double usersMoney = 0; // builds up as user buy more stuff.
- Scanner input = new Scanner(System.in);
- public static void main(String[] args) {
- Products product = new Products();
- product.menu();
- }
- public void menu() {
- printMenu();
- String userInput = input.nextLine().toUpperCase();
- while (!userInput.contains("Q")) {
- switch (userInput) {
- case "1":
- printProductMenu();
- break;
- case "2":
- calculateChange();
- break;
- case "Q":
- System.exit(0);
- }
- }
- }
- public static void printMenu(){
- System.out.println("Welcome to the Local Mart. Please select an Option form below: ");
- System.out.println("1 - Display Products");
- System.out.println("2 - Proceed to Purchase");
- }
- public void printProductMenu() {
- System.out.println("1 - Soda Bottle, Price: £2.00");
- System.out.println("2 - Packet of Crisps, Price: £1.00");
- System.out.println("3 - Pack of Gum, Price: £0.50p");
- System.out.println("4 - Go back");
- int userInput = input.nextInt();
- switch (userInput) {
- case 1:
- this.usersMoney += 2;
- System.out.println("Your total is:" + usersMoney);
- break;
- case 2:
- this.usersMoney += 1;
- System.out.println("Your total is:" + usersMoney);
- break;
- case 3:
- this.usersMoney += 0.50;
- System.out.println("Your total is:" + usersMoney);
- break;
- case 4:
- menu();
- break;
- }
- }
- public void calculateChange() {
- System.out.println("You have to pay" + usersMoney);
- double change = 0;
- int userInput = input.nextInt();
- while (usersMoney != 0) {
- if (userInput < usersMoney) {
- change = usersMoney - usersMoney;
- System.out.println("You still have to pay" + change);
- }
- if (userInput > usersMoney) {
- change = userInput - usersMoney;
- System.out.println("Your remaining change is" + change);
- }
- }
- System.out.println("Congratulations you've paid the amount, Enjoy :)");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement