Advertisement
tchenkov

Exam_21.02.2016_P04_Passion Days

Mar 16th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.Scanner;
  3.  
  4. /**
  5.  * Created by todor on 16.02.2017 г..
  6.  */
  7. public class Dummy {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         BigDecimal money = new BigDecimal(scan.nextLine());
  11.         String commandString = "";
  12.         BigDecimal price = new BigDecimal("0");
  13.         int purchasesCount = 0;
  14.         do {
  15.             commandString = scan.nextLine();
  16.         } while (!commandString.equals("mall.Enter"));
  17.         commandString = scan.nextLine();
  18.         do {
  19.             for (char commandChar : commandString.toCharArray()) {
  20.                 price = price.subtract(price);
  21.                 boolean isCharUpperCase = 'A' <= commandChar && commandChar <= 'Z';
  22.                 price = isCharUpperCase ? price.add(BigDecimal.valueOf(commandChar * 0.50)) : price;
  23.                 boolean isCharLowerCase = 'a' <= commandChar && commandChar <= 'z';
  24.                 price = isCharLowerCase ? price.add(BigDecimal.valueOf(commandChar * 0.30)) : price;
  25.                 boolean isCharPercent = commandChar == '%';
  26.                 price = isCharPercent ? price.add(money.divide(BigDecimal.valueOf(2.0))) : price;
  27.                 boolean isCharAsterisk = (commandChar == '*');
  28.                 money = isCharAsterisk ? money.add(BigDecimal.valueOf(10.0)) : money;
  29.                 boolean isCharOther = !(isCharAsterisk || isCharLowerCase || isCharUpperCase || isCharPercent);
  30.                 price = isCharOther ? price.add(BigDecimal.valueOf(commandChar)) : price;
  31.                 boolean isPriceMoreThanZero = 1 == price.compareTo(BigDecimal.valueOf(0));
  32.                 boolean isPriceLessEqualToMoney = !(1 == price.compareTo(money));
  33.                 if (isPriceMoreThanZero && isPriceLessEqualToMoney) {
  34.                     money = money.subtract(price);
  35.                     purchasesCount++;
  36.                 }
  37.             }
  38.            
  39.             commandString = scan.nextLine();
  40.         } while (!commandString.equals("mall.Exit"));
  41.    
  42.         String moneyString = money.setScale(2,BigDecimal.ROUND_HALF_UP).toString();
  43.         if (purchasesCount == 0){
  44.             System.out.printf("No purchases. Money left: %s lv.%n", moneyString);
  45.         }
  46.         else {
  47.             System.out.printf("%d purchases. Money left: %s lv.%n", purchasesCount, moneyString);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement