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