Advertisement
Guest User

Untitled

a guest
Sep 26th, 2018
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.math.BigDecimal;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.         BigDecimal money = new BigDecimal(0);
  12.         String text = "";
  13.         String cmd;
  14.         BigDecimal nutPrice = new BigDecimal(2);
  15.         BigDecimal waterPrice = new BigDecimal(0.7);
  16.         BigDecimal crispsPrice = new BigDecimal(1.5);
  17.         BigDecimal sodaPrice = new BigDecimal(0.8);
  18.         BigDecimal cokePrice = new BigDecimal(1);
  19.         while (!text.equals("start"))
  20.         {
  21.             cmd = sc.nextLine();
  22.             text = cmd.toLowerCase();
  23.             if (text.equals("0,1") || text.equals("0,2") || text.equals("0,5") || text.equals("1") || text.equals("2") || text.equals("0.1") || text.equals("0.2") || text.equals("0.5"))
  24.             {
  25.                 money = money.add(BigDecimal.valueOf(Double.parseDouble(text)));
  26.             }
  27.             else if (!text.equals("start"))
  28.             {
  29.                 System.out.printf("Cannot accept %.2f\n",Double.parseDouble(cmd));
  30.             }
  31.         }
  32.         while (!text.equals("end"))
  33.         {
  34.             cmd = sc.nextLine();
  35.             text = cmd.toLowerCase();
  36.             if (!text.equals("end"))
  37.             {
  38.                 switch (text)
  39.                 {
  40.                     case ("nuts"):
  41.                     {
  42.                         if (money.compareTo(nutPrice)<0)
  43.                         {
  44.                             System.out.println("Sorry, not enough money");
  45.                             break;
  46.                         }
  47.                         else
  48.                         {
  49.                             System.out.println("Purchased " + cmd);
  50.                             money = money.subtract(nutPrice);
  51.                             break;
  52.                         }
  53.  
  54.                     }
  55.                     case ("water"):
  56.                     {
  57.                         if (money.compareTo(waterPrice)<0)
  58.                         {
  59.                             System.out.println("Sorry, not enough money");
  60.                             break;
  61.                         }
  62.                         else
  63.                         {
  64.                             System.out.println("Purchased "+cmd);
  65.                             money = money.subtract(waterPrice);
  66.                             break;
  67.                         }
  68.  
  69.                     }
  70.                     case ("crisps"):
  71.                     {
  72.                         if (money.compareTo(crispsPrice)<0)
  73.                         {
  74.                             System.out.println("Sorry, not enough money");
  75.                             break;
  76.                         }
  77.                         else
  78.                         {
  79.                             System.out.println("Purchased "+cmd);
  80.                             money = money.subtract(crispsPrice);
  81.                             break;
  82.                         }
  83.  
  84.                     }
  85.                     case ("soda"):
  86.                     {
  87.                         if (money.compareTo(sodaPrice)<0)
  88.                         {
  89.                             System.out.println("Sorry, not enough money");
  90.                             break;
  91.                         }
  92.                         else
  93.                         {
  94.                             System.out.println("Purchased "+cmd);
  95.                             money = money.subtract(sodaPrice);
  96.                             break;
  97.                         }
  98.  
  99.                     }
  100.                     case ("coke"):
  101.                     {
  102.                         if (money.compareTo(cokePrice)<0)
  103.                         {
  104.                             System.out.println("Sorry, not enough money");
  105.                             break;
  106.                         }
  107.                         else
  108.                         {
  109.                             System.out.println("Purchased "+cmd);
  110.                             money = money.subtract(cokePrice);
  111.                             break;
  112.                         }
  113.  
  114.                     }
  115.                     default:
  116.                         System.out.println("Invalid product");
  117.                         break;
  118.                 }
  119.  
  120.             }
  121.         }
  122.         System.out.printf("Change: %.2f",money);
  123.  
  124.  
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement