TheBulgarianWolf

Shopping

Mar 27th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package softuni;
  2.  
  3. import java.math.BigDecimal;
  4. import java.text.DecimalFormat;
  5. import java.util.Scanner;
  6.  
  7. /**
  8.  *
  9.  * @author SYSTEM
  10.  */
  11. public class SoftUni {
  12.  
  13.     /**
  14.      * @param args the command line arguments
  15.      */
  16.     public static void main(String[] args) {
  17.         Scanner sc = new Scanner(System.in);
  18.         DecimalFormat decFormat = new DecimalFormat("#.##");
  19.         System.out.println("Enter the amount of money Lina has: ");
  20.         BigDecimal shoppingMoney = new BigDecimal(sc.nextLine());
  21.         BigDecimal price;
  22.         int purchases = 0;
  23.         String command = sc.nextLine();
  24.         while(!command.equals("mall.Enter")){
  25.             command = sc.nextLine();
  26.         }
  27.         command = sc.nextLine();
  28.         while(!command.equals("mall.Exit")){
  29.             for(char action : command.toCharArray()){
  30.                 if(action >= 'A' && action <= 'Z'){
  31.                     price = new BigDecimal((int) action).multiply(new BigDecimal(0.5));
  32.                     if(shoppingMoney.compareTo(price) < 0){
  33.                         continue;
  34.                     }
  35.                     shoppingMoney = shoppingMoney.subtract(price);
  36.                     purchases++;
  37.                 }
  38.                 else if(action >= 'a' && action <= 'z'){
  39.                     price = new BigDecimal((int) action).multiply(new BigDecimal(0.3));
  40.                     if(shoppingMoney.compareTo(price) < 0){
  41.                         continue;
  42.                     }
  43.                     shoppingMoney = shoppingMoney.subtract(price);
  44.                     purchases++;  
  45.                 }
  46.                 else if(action == '%'){
  47.                     price = new BigDecimal(2);
  48.                     if(shoppingMoney.compareTo(price) < 0){
  49.                         continue;
  50.                     }
  51.                     shoppingMoney = shoppingMoney.divide(price);
  52.                     purchases++;  
  53.                 }
  54.                 else if(action == '*'){
  55.                     price = new BigDecimal(10);
  56.                     if(shoppingMoney.compareTo(price) < 0){
  57.                         continue;
  58.                     }
  59.                     shoppingMoney = shoppingMoney.add(price);
  60.                     purchases++;
  61.                 }
  62.                 else{
  63.                     price = new BigDecimal((int) action);
  64.                     if(shoppingMoney.compareTo(price) < 0){
  65.                         continue;
  66.                     }
  67.                     shoppingMoney = shoppingMoney.subtract(price);
  68.                     purchases++;
  69.                 }
  70.             }
  71.             command = sc.nextLine();
  72.         }
  73.        
  74.         if(purchases == 0){
  75.             System.out.println("No purchases. Money left: " + decFormat.format(shoppingMoney) + " lv.");
  76.         }
  77.         else{
  78.             System.out.println(purchases + " purchases. Money left: " + decFormat.format(shoppingMoney) + " lv.");
  79.         }
  80.     }
  81.    
  82. }
Add Comment
Please, Sign In to add comment