Advertisement
radostin999

02. Report System - While Loop More Exercises

Feb 12th, 2023
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class P02ReportSystem {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         double neededMoney = Integer.parseInt(scanner.nextLine());
  8.         String input = scanner.nextLine();
  9.         double totalMoney = 0;
  10.  
  11.         double cashMoney = 0;
  12.         int cashCounter = 0;
  13.  
  14.         double creditCardMoney = 0;
  15.         int creditCardCounter = 0;
  16.  
  17.  
  18.         int daysCount = 1;
  19.  
  20.         while (!input.equals("End")) {
  21.             double currentDonation = Double.parseDouble(input);
  22.             if (daysCount % 2 == 0) { // Credit Card
  23.                 if (currentDonation < 10) {
  24.                     System.out.println("Error in transaction!");
  25.                 } else {
  26.                     System.out.println("Product sold!");
  27.                     totalMoney += currentDonation;
  28.                     creditCardMoney += currentDonation;
  29.                     creditCardCounter++;
  30.                 }
  31.             } else { // CASH
  32.                 if (currentDonation > 100) {
  33.                     System.out.println("Error in transaction!");
  34.                 } else {
  35.                     System.out.println("Product sold!");
  36.                     totalMoney += currentDonation;
  37.                     cashMoney += currentDonation;
  38.                     cashCounter++;
  39.                 }
  40.             }
  41.             daysCount++;
  42.  
  43.             if (totalMoney > neededMoney) {
  44.                 break;
  45.             }
  46.  
  47.             input = scanner.nextLine();
  48.         }
  49.  
  50.         if (totalMoney > neededMoney) {
  51.             System.out.printf("Average CS: %.2f%n", cashMoney / cashCounter);
  52.             System.out.printf("Average CC: %.2f%n", creditCardMoney / creditCardCounter);
  53.         } else {
  54.             System.out.println("Failed to collect required money for charity.");
  55.         }
  56.  
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement