Advertisement
mmayoub

GamesPark

Dec 30th, 2022
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | Software | 0 0
  1.     public static void main(String[] args) {
  2.         // Define variables
  3.         double a; // price for child card
  4.         double b; // price for adult card
  5.  
  6.         int countA; // counter for child card
  7.         int countB; // counter for adult card
  8.  
  9.         int x; // number of child cards to buy
  10.         int y; // number of adult cards to buy
  11.  
  12.         Scanner in = new Scanner(System.in);
  13.  
  14.         // 1. set price for each card
  15.         // a = 13.70;
  16.         // b = 15.20;
  17.         do {
  18.             System.out.print("Enter price for child card: ");
  19.             a = in.nextDouble();
  20.         } while (a <=0);
  21.        
  22.         do {
  23.             System.out.print("Enter price for adult card: ");
  24.             b = in.nextDouble();
  25.         } while (b <=0);
  26.        
  27.         // 2. initialize counters to zero
  28.         countA = 0;
  29.         countB = 0;
  30.  
  31.         // 3. get number of cards to buy
  32.         System.out.print("Enter number of child cards: ");
  33.         x = in.nextInt();
  34.         System.out.print("Enter number of adult cards: ");
  35.         y = in.nextInt();
  36.  
  37.         // 4. start a loop
  38.         while (x > 0 || y > 0) {
  39.             double total=0;
  40.             if (x>0) {
  41.                 // 4.1 calculate total price
  42.                 total += x*a;
  43.                 // 4.3 update counters
  44.                 countA += x;
  45.             }
  46.            
  47.             if (y>0) {
  48.                 // 4.1 calculate total price
  49.                 total += y*b;
  50.                 // 4.3 update counters
  51.                 countB += y;
  52.             }
  53.            
  54.             // 4.2 show message
  55.             System.out.println("Toatl price is: " + total);
  56.        
  57.             // next customer
  58.             // 4.4 get new values for next customer
  59.             System.out.print("Enter number of child cards: ");
  60.             x = in.nextInt();
  61.             System.out.print("Enter number of adult cards: ");
  62.             y = in.nextInt();
  63.         } // end of while
  64.  
  65.         // 5. print number of cards
  66.         System.out.println("Toatl child cards is: " + countA);
  67.         System.out.println("Toatl adult cards is: " + countB);
  68.        
  69.         // 6. calculate and show total
  70.         System.out.println("Toatl money for this day is: " + (countA*a + countB*b));
  71.        
  72.         // close scanner
  73.         in.close();
  74.     }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement