Angel_Kalinkov

JavaBook-ProblemsForChampions-PartII-PassionDays

Dec 18th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.RoundingMode;
  3. import java.util.Scanner;
  4.  
  5. public class PassionDays2 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         BigDecimal money = new BigDecimal(scanner.nextLine());
  10.         String command = scanner.nextLine();
  11.  
  12.         int purchasesCount = 0;
  13.  
  14.         while (!command.equals("mall.Enter")) {
  15.             command = scanner.nextLine();
  16.         }
  17.         command = scanner.nextLine();
  18.         while (!command.equals("mall.Exit")) {
  19.             for (char action : command.toCharArray()) {
  20.                 if (action == '*') {
  21.                     money = money.add(new BigDecimal("10.00"));
  22.                     continue;
  23.                 }
  24.                 BigDecimal price = new BigDecimal("0.00");
  25.                 if (Character.isUpperCase(action)) {
  26.                     price = new BigDecimal(action).multiply(new BigDecimal("0.5"));
  27.                 } else if (Character.isLowerCase(action)) {
  28.                     price = new BigDecimal(action).multiply(new BigDecimal("0.3"));
  29.                 } else if (action == '%') {
  30.                     price = money.multiply(new BigDecimal("0.5"));
  31.                 } else {
  32.                     price = new BigDecimal(action);
  33.                 }
  34.                 if (money.compareTo(price) < 0 || money.compareTo(new BigDecimal("0.00")) == 0) {
  35.                     continue;
  36.                 }
  37.                 money = money.subtract(price);
  38.                 purchasesCount++;
  39.             }
  40.             command = scanner.nextLine();
  41.         }
  42.         if (purchasesCount == 0) {
  43.             System.out.printf("No purchases. Money left: %s lv.%n",
  44.                     money.setScale(2, RoundingMode.HALF_UP));
  45.         } else {
  46.             System.out.printf("%d purchases. Money left: %s lv.%n", purchasesCount,
  47.                     money.setScale(2, RoundingMode.HALF_UP));
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment