Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SoftUniFund.BasicSyntax;
- import java.util.Scanner;
- public class VendingMachine2 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String input = scan.nextLine();
- double sum = 0;
- while (!input.equals("Start")) {
- if (input.equals("0.1") || input.equals("0.2") || input.equals("0.5") || input.equals("1") || input.equals("2")) {
- sum += Double.parseDouble(input);
- } else {
- System.out.println(String.format("Cannot accept %.2f", Double.parseDouble(input)));
- }
- input = scan.nextLine();
- }
- String product = scan.nextLine();
- while (!product.equals("End")) {
- boolean isProduct = false;
- double price = 0;
- switch (product) {
- case "Nuts":
- price = 2.0;
- isProduct = true;
- break;
- case "Water":
- price = 0.7;
- isProduct = true;
- break;
- case "Crisps":
- price = 1.5;
- isProduct = true;
- break;
- case "Soda":
- price = 0.8;
- isProduct = true;
- break;
- case "Coke":
- price = 1.0;
- isProduct = true;
- break;
- }
- if (price > sum ){
- System.out.println("Sorry, not enough money");
- } else if (isProduct) {
- System.out.println("Purchased " + product);
- sum -= price;
- isProduct = false;
- } else {
- System.out.println("Invalid product");
- }
- product = scan.nextLine();
- }
- System.out.println(String.format("Change: %.2f", sum));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement