Advertisement
Guest User

Payment calculator - Clemente

a guest
Dec 9th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.util.*;
  2. public class ComPro2 {
  3.      
  4.     // Declaring Private Static Variables
  5.         /* we delcare this as private so that it won't intefere on any user-defined object or
  6.             other class when the variable is already used
  7.          */
  8.     private static Scanner userInput = new Scanner(System.in);
  9.     private static int hour = 0;
  10.     private static double rate = 0.0d;
  11.     private static boolean overtime = false;
  12.    
  13.     // Declaring user defined object static calculatePay(class) calcPay (object)
  14.     static calculatePay calcPay;
  15.    
  16.     public static void main(String[] args) {
  17.         // We're going to use infinite loop here for the user can use the program a number of times
  18.         while(true)
  19.         {
  20.             // Print the program title
  21.             System.out.println("[Welcome to Payment Calculator]");
  22.            
  23.             // This infinite loop and try catch method is to avoid any inputmismatch exception when the scanner
  24.             // expected integer values but then a character has input
  25.             while(true) {
  26.                 try {
  27.                     System.out.print("Please input the number of working hours: ");
  28.                     hour = userInput.nextInt();
  29.                     break;
  30.                 }
  31.                 catch(InputMismatchException e) {
  32.                     userInput = new Scanner(System.in);
  33.                 }
  34.             }
  35.             // This infinite loop and try catch method is to avoid any inputmismatch exception when the scanner
  36.             // expected integer values but then a character has input
  37.             while(true) {
  38.                 try {
  39.                     System.out.print("Please input the payment rate per hour: ");
  40.                     rate = userInput.nextDouble();
  41.                     break;
  42.                 }
  43.                 catch(InputMismatchException e) {
  44.                     userInput = new Scanner(System.in);
  45.                 }
  46.             }
  47.             // set the object default values
  48.             calcPay  = new calculatePay(hour, rate);
  49.             // Print the Result
  50.             calcPay.toPrint();
  51.            
  52.             // This code will ask user to use the program again or nope if not then program stop.
  53.             userInput = new Scanner(System.in);
  54.             String temp = "";
  55.             while(true)
  56.             {
  57.                 System.out.println("Do you want to use it again? (Yes/No): ");
  58.                 temp = userInput.next();
  59.                 if(temp.equalsIgnoreCase("No") || temp.equalsIgnoreCase("Yes"))
  60.                 {
  61.                     break;
  62.                 }
  63.             }
  64.             if(temp.equalsIgnoreCase("No")) break;
  65.         }
  66.     }
  67. }
  68.  
  69. // Define calculatePay object class
  70. class calculatePay {
  71.    
  72.     // We're going to declare a constant variable
  73.     final static int MINIMUM_PER_HOUR = 40;
  74.     final static double BONUS_OVERTIME = 1.5d;
  75.    
  76.     // private variable double
  77.     private double
  78.         payment,
  79.         rate;
  80.        
  81.     // private integer
  82.     private int hour;
  83.    
  84.     // private boolean
  85.     private boolean isOvertime = false;
  86.    
  87.     // initialize the default values of the declared variables
  88.     calculatePay() {
  89.         payment = 0;
  90.         hour = 0;
  91.         rate = 0;
  92.         isOvertime = false;
  93.     }
  94.     // initialization method for this class
  95.     public calculatePay(int valHour, double valRate)
  96.     {
  97.         hour = valHour;
  98.         rate = valRate;
  99.         // This will check if the hour input is greater than the minimum per hour then set the over time to true
  100.         if(hour  > MINIMUM_PER_HOUR) isOvertime = true;
  101.     }
  102.     // To print the calculated result of payment or paycheck
  103.     public void toPrint()
  104.     {
  105.         System.out.println("\n\n=============================");
  106.         if(isOvertime) payment =  MINIMUM_PER_HOUR * rate + (hour - MINIMUM_PER_HOUR) * BONUS_OVERTIME * rate;
  107.         else payment = hour *rate;
  108.         System.out.println("Working Hours: " + hour + "\nRate: " + rate + "\nOvertime: "+ ((isOvertime) ? "Yes" : "No") + "\n\nPaycheck: " + payment + " Pesos. ");
  109.         System.out.println("=============================\n");
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement