grach

Report System - While Loops - more exercise

Apr 16th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ReportSystem {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.         double toCollect = Double.parseDouble(scan.nextLine());
  8.  
  9.         double sumCard = 0;
  10.         double sumCash = 0;
  11.  
  12.         int cashCount = 0;
  13.         int cardCount = 0;
  14.  
  15.         double averageCS = 0;
  16.         double averageCC = 0;
  17.  
  18.  
  19.         String readtext = scan.nextLine();
  20.  
  21.         while (!"End".equals(readtext)) {
  22.             int cashPayment = Integer.parseInt(readtext);
  23.             readtext = scan.nextLine();
  24.             int cardPayment = Integer.parseInt(readtext);
  25.  
  26.             if (cashPayment > 100) {
  27.                 System.out.println("Error in transaction!");
  28.             } else if (cashPayment < 100) {
  29.                 System.out.println("Product sold!");
  30.                 sumCash = sumCash + cashPayment;
  31.                 cashCount++;
  32.             }
  33.             if (cardPayment < 10) {
  34.                 System.out.println("Error in transaction!");
  35.             } else if (cardPayment > 10) {
  36.                 System.out.println("Product sold!");
  37.                 sumCard = sumCard + cardPayment;
  38.                 cardCount++;
  39.             }
  40.  
  41.             if (sumCard + sumCash >= toCollect) {
  42.                 averageCS = sumCash / cashCount;
  43.                 averageCC = sumCard / cardCount;
  44.                 break;
  45.             }
  46.  
  47.             readtext = scan.nextLine();
  48.         }
  49.  
  50.         if ("End".equals(readtext)) {
  51.             System.out.println("Failed to collect required money for charity.");
  52.         } else {
  53.             System.out.printf("Average CS: %.2f%nAverage CC: %.2f", averageCS, averageCC);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment