//********************************************** // PROGRAM NAME: Payroll.java // MY NAME: VK // BRIEF DESCRIPTION: A payroll program for // JMTPP. Takes employee ID, outputs payroll // information. Will create payroll info if // none available in system. //********************************************** import java.io.*; import java.util.*; import java.text.*; public class Payroll { public static String dateOut(int i) { // Adds zeros to the date if necessary String returnNumber = Integer.toString(i); while (returnNumber.length() < 2) { returnNumber = "0".concat(returnNumber); } return returnNumber; } public static String payOut(double i) { // Rounding to nearest Hundreths: i = i*100; i = Math.round(i); String returnNumber = Double.toString(i/100); returnNumber = (i % 10 == 0.0) ? returnNumber.concat("0") : returnNumber; return returnNumber; } public static String newEmplQues(String prompt) { System.out.print(prompt); Scanner sc1 = new Scanner (System.in); String input = sc1.nextLine(); return input; } public static void main(String[] args) throws FileNotFoundException { // Get Employee Number System.out.print("Please enter an employee ID: "); Scanner sc1 = new Scanner (System.in); int EmployeeNumber = sc1.nextInt(); // Convert Employee Number to File Name String FileName = Integer.toString(EmployeeNumber); while (FileName.length() < 3) { FileName = "0".concat(FileName); } // Open Employee's Records; create new records if none exists while (new File(FileName + "-hours.txt").exists() == false) { String[] newEmployeeInfo = new String[10]; PrintStream createEmplFile = new PrintStream(FileName + "-hours.txt"); System.out.print("Your information was not found in our records.\nPlease answer the following questions:"); System.out.println(); newEmployeeInfo[0] = newEmplQues("What is your name? "); newEmployeeInfo[1] = newEmplQues("When was the first date of this pay period? (mm/dd/yy) "); newEmployeeInfo[2] = newEmplQues("What is your hourly wage? $"); newEmployeeInfo[3] = newEmplQues("How many dependents do you have? "); newEmployeeInfo[4] = newEmplQues("How much is your pre-tax retirement deduction? "); newEmployeeInfo[5] = newEmplQues("How many hours did you work on Monday? "); newEmployeeInfo[6] = newEmplQues("How many hours did you work on Tuesday? "); newEmployeeInfo[7] = newEmplQues("How many hours did you work on Wednesday? "); newEmployeeInfo[8] = newEmplQues("How many hours did you work on Thursday? "); newEmployeeInfo[9] = newEmplQues("How many hours did you work on Friday? "); createEmplFile.println(newEmployeeInfo[1] + "\t" + newEmployeeInfo[0] + " " + newEmployeeInfo[2] + " " + newEmployeeInfo[3] + " " + newEmployeeInfo[4]); createEmplFile.println(newEmployeeInfo[5]); createEmplFile.println(newEmployeeInfo[6]); createEmplFile.println(newEmployeeInfo[7]); createEmplFile.println(newEmployeeInfo[8]); createEmplFile.println(newEmployeeInfo[9]); System.out.println("Thank you very much. Your file has been created."); System.out.println(""); } Scanner sc3 = new Scanner (new File (FileName + "-hours.txt")); // Takes first line String FirstLine = sc3.nextLine(); // Collect worked hours into array double[] hoursArray = new double[5]; int i = 0; while (sc3.hasNextDouble()) { hoursArray[i] = sc3.nextDouble(); i++; } //BEGIN DATE CODE // Get today's date: DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT); Date now = new Date(); String todayDate = df1.format(now); // Gets First Date int[] firstDateArray = new int[3]; String Month = FirstLine.substring(0,2); Scanner getMonth = new Scanner (Month); firstDateArray[0] = getMonth.nextInt(); String Day = FirstLine.substring(3,5); Scanner getDay = new Scanner (Day); firstDateArray[1] = getDay.nextInt(); String Year = FirstLine.substring(6,8); Scanner getYear = new Scanner (Year); firstDateArray[2] = getYear.nextInt(); int[] secondDateArray = new int[3]; int numDays = 0; switch (firstDateArray[0]) { // Months with 31 days, not December case 1: case 3: case 5: case 7: case 8: case 10: numDays = 31; // Decide month of 2nd date; advance to next month if necessary secondDateArray[0] = (firstDateArray[1] > (numDays - 4)) ? (firstDateArray[0] + 1) : firstDateArray[0]; // Decide day of 2nd date; advance to next month if necessary secondDateArray[1] = (firstDateArray[1] > (numDays - 4)) ? (firstDateArray[1] + 4 - numDays) : (firstDateArray[1] + 4); secondDateArray[2] = firstDateArray[2]; break; // December case 12: numDays = 31; // Decide month of 2nd date; advance to next January if necessary secondDateArray[0] = (firstDateArray[1] > (numDays - 4)) ? 1 : 12; // Decide day of 2nd date; advance to next month if necessary secondDateArray[1] = (firstDateArray[1] > (numDays - 4)) ? (firstDateArray[1] + 4 - numDays) : (firstDateArray[1] + 4); // Decide if year advanges secondDateArray[2] = (firstDateArray[1] > (numDays - 4)) ? ( (firstDateArray[2] == 99) ? 0 : firstDateArray[2] + 1 ) : firstDateArray[2]; break; // Months with 30 days case 4: case 6: case 9: case 11: numDays = 30; // Decide month of 2nd date; advance to next month if necessary secondDateArray[0] = (firstDateArray[1] > (numDays - 4)) ? (firstDateArray[0] + 1) : firstDateArray[0]; // Decide day of 2nd date; advance to next month if necessary secondDateArray[1] = (firstDateArray[1] > (numDays - 4)) ? (firstDateArray[1] + 4 - numDays) : (firstDateArray[1] + 4); secondDateArray[2] = firstDateArray[2]; break; // February case 2: numDays = ( (double) firstDateArray[2] % 4 == 0) ? 29 : 28; // Decide month of 2nd date; advance to next month if necessary secondDateArray[0] = (firstDateArray[1] > (numDays - 4)) ? (firstDateArray[0] + 1) : (firstDateArray[0]); // Decide day of 2nd date; advance to next month if necessary secondDateArray[1] = (firstDateArray[1] > (numDays - 4)) ? (firstDateArray[1] + 4 - numDays) : firstDateArray[1] + 4; secondDateArray[2] = firstDateArray[2]; break; default: numDays = 0; break; } String firstDateLine = dateOut(firstDateArray[0]).concat("/").concat(dateOut(firstDateArray[1])).concat("/").concat(dateOut(firstDateArray[2])); String secondDateLine = dateOut(secondDateArray[0]).concat("/").concat(dateOut(secondDateArray[1])).concat("/").concat(dateOut(secondDateArray[2])); //END DATE CODE // Gets Name String EmployeeName = ""; Scanner inline = new Scanner (FirstLine); while (inline.hasNextDouble() != true) { EmployeeName = EmployeeName.concat(inline.next().toString()).concat(" "); } int EmployeeNameLength = EmployeeName.length(); EmployeeName = EmployeeName.substring(8, EmployeeNameLength); EmployeeName = EmployeeName.trim(); FirstLine = FirstLine.substring(EmployeeNameLength); // Gets Employee's wages, dependents, retirement deduction // What goes where: // 0 = Hourly Wage // 1 = Number of Dependents // 2 = $ amount of pre-tax retirement deduction double[] EmployeeNumbers = new double[100]; int j = 0; Scanner inline2 = new Scanner (FirstLine); while (inline2.hasNextDouble()) { EmployeeNumbers[j] = inline2.nextDouble(); j++; } //Calculations: // Recall that: // # of hrs. worked on day 1 = hoursArray[0] // # of hrs. worked on day 2 = hoursArray[1], etc. double GrossPay = 0.0; // Calculate Total Hours Worked double TotalHours = 0.0; for (int h=0; h < 5; h++) { TotalHours = (TotalHours + hoursArray[h]); } // Calculate Pay + overtime double OvertimeHours = 0.0; while (TotalHours > 40) { OvertimeHours = TotalHours - 40; break; } // Gross Pay = (hourly wage)*(total hours) + (0.5 more for each OT hour)*(overtime hours) GrossPay = EmployeeNumbers[0]*TotalHours + 0.5*EmployeeNumbers[0]*OvertimeHours; // Taxable Income (Gross Pay - $100 for the employee - $50*(dependent) - retirement deduction double TaxablePay = GrossPay - 100 - 50*EmployeeNumbers[1] - EmployeeNumbers[2]; while (TaxablePay <= 0) { TaxablePay = 0; break; } // Calculate tax rate; -2% for each dependent double taxRate = 0.18; taxRate = (taxRate - (EmployeeNumbers[1]*0.02)); while (taxRate <= 0) { taxRate = 0.0; break; } // Paycheck amount double Paycheck = (TaxablePay - taxRate*TaxablePay); while (Paycheck <= 0) { Paycheck = 0; break; } // end while // BEGIN OUTPUT CODE System.out.println(todayDate); System.out.print ("Pay to the order of ".concat(EmployeeName).concat(": ")); System.out.println ("$" + payOut(Paycheck)); System.out.println ("Memo: employee #" + EmployeeNumber + " for the period " + firstDateLine + " to " + secondDateLine); // END OUTPUT CODE } // end main } // end class