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