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