Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class ComPro2 {
- // Declaring Private Static Variables
- /* we delcare this as private so that it won't intefere on any user-defined object or
- other class when the variable is already used
- */
- private static Scanner userInput = new Scanner(System.in);
- private static int hour = 0;
- private static double rate = 0.0d;
- private static boolean overtime = false;
- // Declaring user defined object static calculatePay(class) calcPay (object)
- static calculatePay calcPay;
- public static void main(String[] args) {
- // We're going to use infinite loop here for the user can use the program a number of times
- while(true)
- {
- // Print the program title
- System.out.println("[Welcome to Payment Calculator]");
- // This infinite loop and try catch method is to avoid any inputmismatch exception when the scanner
- // expected integer values but then a character has input
- while(true) {
- try {
- System.out.print("Please input the number of working hours: ");
- hour = userInput.nextInt();
- break;
- }
- catch(InputMismatchException e) {
- userInput = new Scanner(System.in);
- }
- }
- // This infinite loop and try catch method is to avoid any inputmismatch exception when the scanner
- // expected integer values but then a character has input
- while(true) {
- try {
- System.out.print("Please input the payment rate per hour: ");
- rate = userInput.nextDouble();
- break;
- }
- catch(InputMismatchException e) {
- userInput = new Scanner(System.in);
- }
- }
- // set the object default values
- calcPay = new calculatePay(hour, rate);
- // Print the Result
- calcPay.toPrint();
- // This code will ask user to use the program again or nope if not then program stop.
- userInput = new Scanner(System.in);
- String temp = "";
- while(true)
- {
- System.out.println("Do you want to use it again? (Yes/No): ");
- temp = userInput.next();
- if(temp.equalsIgnoreCase("No") || temp.equalsIgnoreCase("Yes"))
- {
- break;
- }
- }
- if(temp.equalsIgnoreCase("No")) break;
- }
- }
- }
- // Define calculatePay object class
- class calculatePay {
- // We're going to declare a constant variable
- final static int MINIMUM_PER_HOUR = 40;
- final static double BONUS_OVERTIME = 1.5d;
- // private variable double
- private double
- payment,
- rate;
- // private integer
- private int hour;
- // private boolean
- private boolean isOvertime = false;
- // initialize the default values of the declared variables
- calculatePay() {
- payment = 0;
- hour = 0;
- rate = 0;
- isOvertime = false;
- }
- // initialization method for this class
- public calculatePay(int valHour, double valRate)
- {
- hour = valHour;
- rate = valRate;
- // This will check if the hour input is greater than the minimum per hour then set the over time to true
- if(hour > MINIMUM_PER_HOUR) isOvertime = true;
- }
- // To print the calculated result of payment or paycheck
- public void toPrint()
- {
- System.out.println("\n\n=============================");
- if(isOvertime) payment = MINIMUM_PER_HOUR * rate + (hour - MINIMUM_PER_HOUR) * BONUS_OVERTIME * rate;
- else payment = hour *rate;
- System.out.println("Working Hours: " + hour + "\nRate: " + rate + "\nOvertime: "+ ((isOvertime) ? "Yes" : "No") + "\n\nPaycheck: " + payment + " Pesos. ");
- System.out.println("=============================\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement