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