Advertisement
myrdok123

P05TouristShop

Apr 20th, 2024
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package ExamPrep;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P05TouristShop {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         double budget = Double.parseDouble(scanner.nextLine());
  10.  
  11.         String command = scanner.nextLine();
  12.  
  13.         double sum = 0;
  14.         int countProducts = 0;
  15.         int countThirdProduct = 0;
  16.         while (!command.equals("Stop")){
  17.  
  18.             double price = Double.parseDouble(scanner.nextLine());
  19.             countProducts++;
  20.             countThirdProduct++;
  21.  
  22.             if(countThirdProduct == 3){
  23.                 sum = sum + price / 2;
  24.                 countThirdProduct = 0;
  25.             }else {
  26.                 sum += price;
  27.             }
  28.  
  29.             if(sum > budget){
  30.                 break;
  31.             }
  32.  
  33.  
  34.             command = scanner.nextLine();
  35.         }
  36.  
  37.  
  38.         if(sum < budget){
  39.             System.out.printf("You bought %d products for %.2f leva.", countProducts, sum);
  40.         }else {
  41.             System.out.println("You don't have enough money!");
  42.             System.out.printf("You need %.2f leva!", sum - budget);
  43.         }
  44.  
  45.  
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement