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