Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.util.Scanner;
- /**
- * Created by todor on 16.02.2017 г..
- */
- public class Dummy {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- BigDecimal money = new BigDecimal(scan.nextLine());
- String commandString = "";
- BigDecimal price = new BigDecimal("0");
- int purchasesCount = 0;
- do {
- commandString = scan.nextLine();
- } while (!commandString.equals("mall.Enter"));
- commandString = scan.nextLine();
- do {
- for (char commandChar : commandString.toCharArray()) {
- price = price.subtract(price);
- boolean isCharUpperCase = 'A' <= commandChar && commandChar <= 'Z';
- price = isCharUpperCase ? price.add(BigDecimal.valueOf(commandChar * 0.50)) : price;
- boolean isCharLowerCase = 'a' <= commandChar && commandChar <= 'z';
- price = isCharLowerCase ? price.add(BigDecimal.valueOf(commandChar * 0.30)) : price;
- boolean isCharPercent = commandChar == '%';
- price = isCharPercent ? price.add(money.divide(BigDecimal.valueOf(2.0))) : price;
- boolean isCharAsterisk = (commandChar == '*');
- money = isCharAsterisk ? money.add(BigDecimal.valueOf(10.0)) : money;
- boolean isCharOther = !(isCharAsterisk || isCharLowerCase || isCharUpperCase || isCharPercent);
- price = isCharOther ? price.add(BigDecimal.valueOf(commandChar)) : price;
- boolean isPriceMoreThanZero = 1 == price.compareTo(BigDecimal.valueOf(0));
- boolean isPriceLessEqualToMoney = !(1 == price.compareTo(money));
- if (isPriceMoreThanZero && isPriceLessEqualToMoney) {
- money = money.subtract(price);
- purchasesCount++;
- }
- }
- commandString = scan.nextLine();
- } while (!commandString.equals("mall.Exit"));
- String moneyString = money.setScale(2,BigDecimal.ROUND_HALF_UP).toString();
- if (purchasesCount == 0){
- System.out.printf("No purchases. Money left: %s lv.%n", moneyString);
- }
- else {
- System.out.printf("%d purchases. Money left: %s lv.%n", purchasesCount, moneyString);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement