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