Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class EP07VendingMachine {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- double totalCoinsInserted = 0;
- double productsTotalPrice = 0;
- double nuts = 2.0;
- double water = 0.7;
- double crisps = 1.5;
- double soda = 0.8;
- double coke = 1.0;
- while (true) {
- String command = scanner.nextLine();
- if (command.equalsIgnoreCase("start")) {
- break;
- }
- double insertedCoins = Double.parseDouble(command);
- if (insertedCoins != 0.1 && insertedCoins != 0.2
- && insertedCoins != 0.5 && insertedCoins != 1
- && insertedCoins != 2) {
- System.out.printf("Cannot accept %.2f\n", insertedCoins);
- } else {
- totalCoinsInserted += insertedCoins;
- }
- }
- while (true) {
- String productType = scanner.nextLine();
- if (productType.equalsIgnoreCase("End")) {
- System.out.printf("Change: %.2f", totalCoinsInserted - productsTotalPrice);
- break;
- }
- switch (productType) {
- case "Nuts":
- productsTotalPrice += nuts;
- if (productsTotalPrice > totalCoinsInserted) {
- productsTotalPrice -= nuts;
- System.out.println("Sorry, not enough money");
- } else {
- System.out.printf("Purchased %s\n", productType);
- }
- break;
- case "Water":
- productsTotalPrice += water;
- if (productsTotalPrice > totalCoinsInserted) {
- productsTotalPrice -= water;
- System.out.println("Sorry, not enough money");
- } else {
- System.out.printf("Purchased %s\n", productType);
- }
- break;
- case "Crisps":
- productsTotalPrice += crisps;
- if (productsTotalPrice > totalCoinsInserted) {
- productsTotalPrice -= crisps;
- System.out.println("Sorry, not enough money");
- } else {
- System.out.printf("Purchased %s\n", productType);
- }
- break;
- case "Soda":
- productsTotalPrice += soda;
- if (productsTotalPrice > totalCoinsInserted) {
- productsTotalPrice -= soda;
- System.out.println("Sorry, not enough money");
- } else {
- System.out.printf("Purchased %s\n", productType);
- }
- break;
- case "Coke":
- productsTotalPrice += coke;
- if (productsTotalPrice > totalCoinsInserted) {
- productsTotalPrice -= coke;
- System.out.println("Sorry, not enough money");
- } else {
- System.out.printf("Purchased %s\n", productType);
- }
- break;
- default:
- System.out.println("Invalid product");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement