Advertisement
Guest User

Untitled

a guest
May 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class VendingMashine {
  6.     public static void main(String[] args) {
  7.         Scanner scaner= new Scanner(System.in);
  8.         double coins=0;
  9.         double totalMoney=0;
  10.         String input=scaner.nextLine();
  11.         while(!input.equals("Start")){
  12.  
  13.             coins=Double.parseDouble(input);
  14.             if(coins==0.1||coins==0.2||coins==0.5||coins==1||coins==2){
  15.                 totalMoney += coins;
  16.             }else {
  17.                 System.out.printf("Cannot accept %.2f%n",coins);
  18.             }
  19.             input = scaner.nextLine();
  20.         }
  21.        
  22.         String input2=scaner.nextLine();
  23.         while(!input2.equals("End")){
  24.             switch (input2){
  25.                 case "Nuts":
  26.                     if(totalMoney>=2){
  27.                         System.out.println("Purchased Nuts");
  28.                         totalMoney-=2;
  29.                     }else {
  30.                         System.out.println("Sorry, not enough money");
  31.                     }
  32.                     break;
  33.                 case "Water":
  34.                     if(totalMoney>=0.7){
  35.                         System.out.println("Purchased Water");
  36.                         totalMoney-=0.7;
  37.                     }else {
  38.                         System.out.println("Sorry, not enough money");
  39.                     }
  40.                     break;
  41.                 case "Crisps":
  42.                     if(totalMoney>=1.5){
  43.                     System.out.println("Purchased Crisps");
  44.                         totalMoney-=1.5;
  45.                 }else {
  46.                     System.out.println("Sorry, not enough money");
  47.                 }
  48.                 break;
  49.                 case "Soda":
  50.                     if(totalMoney>=0.8){
  51.                         System.out.println("Purchased Soda");
  52.                         totalMoney-=0.8;
  53.                     }else {
  54.                         System.out.println("Sorry, not enough money");
  55.                     }
  56.                     break;
  57.                 case "Coke":
  58.                     if(totalMoney>=1.0){
  59.                         System.out.println("Purchased Coke");
  60.                         totalMoney-=1.0;
  61.                     }else {
  62.                         System.out.println("Sorry, not enough money");
  63.                     }
  64.                     break;
  65.                     default:
  66.                         System.out.println("Invalid product");
  67.                         break;
  68.             }
  69.             input2=scaner.nextLine();
  70.         }
  71.         System.out.printf("Change: %.2f",totalMoney);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement