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