borovaneca

Computer Store

Feb 17th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package Advance.Exams.MidExams;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ComputerStore {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.         String parts = scanner.nextLine();
  11.         double sum = 0;
  12.         double taxes = 0;
  13.         double total = 0;
  14.  
  15.         while (!parts.equals("special") && !parts.equals("regular")) {
  16.  
  17.             double currentPrice = Double.parseDouble(parts);
  18.             if (currentPrice < 0) {
  19.                 System.out.println("Invalid price!");
  20.                 parts = scanner.nextLine();
  21.                 continue;
  22.             } else {
  23.                 sum += currentPrice;
  24.             }
  25.  
  26.             parts = scanner.nextLine();
  27.         }
  28.  
  29.         switch (parts) {
  30.             case "special":
  31.  
  32.                 taxes = sum * 0.20;
  33.                 total = sum + taxes;
  34.                 total = total - (total * 0.10);
  35.                 execution(sum, taxes, total);
  36.  
  37.                 break;
  38.             case "regular":
  39.  
  40.                 taxes = sum * 0.20;
  41.                 total = taxes + sum;
  42.                 execution(sum, taxes, total);
  43.  
  44.                 break;
  45.         }
  46.     }
  47.  
  48.     private static void execution(double sum, double taxes, double total) {
  49.         if (isNegative(total)) {
  50.             System.out.println("Invalid order!");
  51.         } else {
  52.             System.out.println("Congratulations you've just bought a new computer!");
  53.             System.out.printf("Price without taxes: %.2f$%n", sum);
  54.             System.out.printf("Taxes: %.2f$%n", taxes);
  55.             System.out.println("-----------");
  56.             System.out.printf("Total price: %.2f$", total);
  57.         }
  58.     }
  59.  
  60.     private static boolean isNegative(double total) {
  61.         return total <= 0;
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment