Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.math.BigDecimal;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- BigDecimal money = new BigDecimal(0);
- String text = "";
- String cmd;
- BigDecimal nutPrice = new BigDecimal(2);
- BigDecimal waterPrice = new BigDecimal(0.7);
- BigDecimal crispsPrice = new BigDecimal(1.5);
- BigDecimal sodaPrice = new BigDecimal(0.8);
- BigDecimal cokePrice = new BigDecimal(1);
- while (!text.equals("start"))
- {
- cmd = sc.nextLine();
- text = cmd.toLowerCase();
- if (text.equals("0,1") || text.equals("0,2") || text.equals("0,5") || text.equals("1") || text.equals("2") || text.equals("0.1") || text.equals("0.2") || text.equals("0.5"))
- {
- money = money.add(BigDecimal.valueOf(Double.parseDouble(text)));
- }
- else if (!text.equals("start"))
- {
- System.out.printf("Cannot accept %.2f\n",Double.parseDouble(cmd));
- }
- }
- while (!text.equals("end"))
- {
- cmd = sc.nextLine();
- text = cmd.toLowerCase();
- if (!text.equals("end"))
- {
- switch (text)
- {
- case ("nuts"):
- {
- if (money.compareTo(nutPrice)<0)
- {
- System.out.println("Sorry, not enough money");
- break;
- }
- else
- {
- System.out.println("Purchased " + cmd);
- money = money.subtract(nutPrice);
- break;
- }
- }
- case ("water"):
- {
- if (money.compareTo(waterPrice)<0)
- {
- System.out.println("Sorry, not enough money");
- break;
- }
- else
- {
- System.out.println("Purchased "+cmd);
- money = money.subtract(waterPrice);
- break;
- }
- }
- case ("crisps"):
- {
- if (money.compareTo(crispsPrice)<0)
- {
- System.out.println("Sorry, not enough money");
- break;
- }
- else
- {
- System.out.println("Purchased "+cmd);
- money = money.subtract(crispsPrice);
- break;
- }
- }
- case ("soda"):
- {
- if (money.compareTo(sodaPrice)<0)
- {
- System.out.println("Sorry, not enough money");
- break;
- }
- else
- {
- System.out.println("Purchased "+cmd);
- money = money.subtract(sodaPrice);
- break;
- }
- }
- case ("coke"):
- {
- if (money.compareTo(cokePrice)<0)
- {
- System.out.println("Sorry, not enough money");
- break;
- }
- else
- {
- System.out.println("Purchased "+cmd);
- money = money.subtract(cokePrice);
- break;
- }
- }
- default:
- System.out.println("Invalid product");
- break;
- }
- }
- }
- System.out.printf("Change: %.2f",money);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement