/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code2nov17; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code2Nov17 { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner in = new Scanner(System.in); /* Determine how a person is paid, then ask correct questions to calculate payment */ double totalPay = 0; String defaultName = "Charlie Brown"; String name = defaultName; int i; boolean moreEmployees = true; boolean fileValid = true; File inputFileVar = new File("EmpData.txt"); Scanner inFile; try { //Scanner inFile = new Scanner(inputFileVar); inFile = new Scanner(inputFileVar); } catch (FileNotFoundException fnf) { System.out.println("File not found"); inFile = new Scanner(System.in); fileValid = false; } //for(i = 0; i < 5; i++ ) while (moreEmployees || inFile.hasNextLine()) { if (fileValid) { name = inFile.nextLine(); if (name.length() == 0) // name.equals("") { name = defaultName; } System.out.println("The next employee from the file is "+name); } else { System.out.println("What is the person's name? [The default name is "+defaultName+"]"); name = in.nextLine(); if (name.length() == 0) // name.equals("") { name = defaultName; } } // we now have the person's name double payRate; payRate = 0.0; char empType = ' '; String empTypeIn; if (fileValid) { empTypeIn = inFile.next(); // read empTypeIn from file } else { System.out.println(""); System.out.println("Is the person an hourly, salary, commission or occasional employee?"); System.out.println("H - hourly\nS - salary\nC - commission\nO - occasional contracted"); System.out.println("Please enter H, S, C, or O: "); empTypeIn = in.next(); } if (empTypeIn.length() >= 1) { empType = empTypeIn.charAt(0); empTypeIn = ""; } System.out.println("The employee type is "+empType); //System.out.println("empType == 'H' ? "+(empType=='H')); System.out.println(""); /* double numberHrsDay1 = 0; double numberHrsDay2 = 0; double numberHrsDay3 = 0; double numberHrsDay4 = 0; double numberHrsDay5 = 0; double numberHrsDay6 = 0; double numberHrsDay7 = 0; */ double numberHrsWorked = 0; // hours worked in a week final int daysInWeek = 7; double[] numberHrsDay = new double[daysInWeek]; // Monday - Sunday order int[] gigCount = new int[daysInWeek]; if (fileValid) { /* numberHrsDay1 = inFile.nextDouble(); numberHrsDay2 = inFile.nextDouble(); numberHrsDay3 = inFile.nextDouble(); numberHrsDay4 = inFile.nextDouble(); numberHrsDay5 = inFile.nextDouble(); numberHrsDay6 = inFile.nextDouble(); numberHrsDay7 = inFile.nextDouble(); */ for(int index = 0; index < 7; index++) { numberHrsDay[index] = inFile.nextDouble(); } // error checking info // No more than 24 -> mod of 24 [check 2nd] // No less than 0 -> absolute value [check 1st] // No more than 16 hours per day -> change to 16 [check 3rd] // Must use all three cases // Must come out with a number that is between 0 and 16 // Input is a double representing hours in a day //Quiz 2 Nov 2017 //Write a Java fragment using the array numberHrsDay to replace the block of code below // Monday is location 0 in the array for(int index = 0; index < 7; index++) { numberHrsDay[index] = HoursWorkedInDayCheck(numberHrsDay[index]); } /* numberHrsDay[1] = HoursWorkedInDayCheck(numberHrsDay[1]); numberHrsDay[2] = HoursWorkedInDayCheck(numberHrsDay[2]); numberHrsDay[3] = HoursWorkedInDayCheck(numberHrsDay[3]); numberHrsDay[4] = HoursWorkedInDayCheck(numberHrsDay[4]); numberHrsDay[5] = HoursWorkedInDayCheck(numberHrsDay[5]); numberHrsDay[6] = HoursWorkedInDayCheck(numberHrsDay[6]); numberHrsDay[0] = HoursWorkedInDayCheck(numberHrsDay[0]); */ } else { for(int index = 0; index < 7; index++) { numberHrsDay[index] = weeklyHourInputValidation(in, index); } /* numberHrsDay1 = weeklyHourInputValidation(in, 1); numberHrsDay2 = weeklyHourInputValidation(in, 2); numberHrsDay3 = weeklyHourInputValidation(in, 3); numberHrsDay4 = weeklyHourInputValidation(in, 4); numberHrsDay5 = weeklyHourInputValidation(in, 5); */ } /* System.out.println("Hours worked : "+numberHrsDay1 +" "+ numberHrsDay2+" " + numberHrsDay3 +" " + numberHrsDay4+" " + numberHrsDay5+" " + numberHrsDay6+" " + numberHrsDay7); */ int index = 0; System.out.println("Hours worked : "); for(index = 0; index < 7; index++) { System.out.print(""+numberHrsDay[index]+" "); } /* numberHrsWorked = numberHrsDay1 + numberHrsDay2 + numberHrsDay3 + numberHrsDay4 + numberHrsDay5 + numberHrsDay6 + numberHrsDay7 ; */ for(index = 0; index < 7; index++) { numberHrsWorked = numberHrsDay[index] + numberHrsWorked; } /* Nested if-else has three cases which each checked two values for a total of 6 cases */ switch (empType) // integer types or characters or strings { case 'H': case 'h': { // calculating hourly pay if (fileValid) { payRate = inFile.nextDouble(); } else { System.out.println(""); //payRate = 12.50; // pay per hour System.out.println("What is the pay per hour?"); payRate = in.nextDouble(); in.nextLine(); //empties the input buffer after reading a number } double overtimeRate = 1.5; // for hours over 40 in one week double overtimeHrsWorked = 0; if (numberHrsWorked >= 40) { overtimeHrsWorked = numberHrsWorked - 39; } totalPay = (overtimeHrsWorked * overtimeRate + (numberHrsWorked - overtimeHrsWorked)) * payRate; System.out.println(""); System.out.print(name+" worked "+numberHrsWorked+" in 1 week."); System.out.print(" Their work week is 39 hours and the overtime \npay rate is "+overtimeRate); System.out.println(". The pay per hour for "+name+" is "+payRate); } break; case 's': case 'S': { if (fileValid) { payRate = inFile.nextDouble(); } else { System.out.println(""); //payRate = 500.00; // salary pay per week System.out.println("What is the weekly salary?"); payRate = in.nextDouble(); in.nextLine(); //empties the input buffer after reading a number } totalPay = payRate; /* System.out.println(""); System.out.println(name + " receives $"+payRate+" per week in salary"); */ System.out.printf("%s receives $%7.2f per week in salary\n",name,payRate); } break; case 'c': case 'C': { double itemCost; // cost for one item that pays commission double commissionPct; // percent paid for the commission int numberItemsSold; // items sold in a week if (fileValid) { itemCost = inFile.nextDouble(); //*** commissionPct % numberItemsSold "items", i.e.#%#items String pctItemsString = inFile.next(); // read whole string with info Scanner pctItemsInfo = new Scanner(pctItemsString); // connect STRING to Scanner pctItemsInfo.useDelimiter("%"); // set the delimiter that is needed String cPct = pctItemsInfo.next(); // read a string from the string up to the delimiter commissionPct = Double.parseDouble(cPct); // take the new string and convert to number int skip = cPct.length()+1; // find length of new string plus pctItemsString = pctItemsString.substring(skip); pctItemsInfo = new Scanner(pctItemsString); pctItemsInfo.useDelimiter("[a-zA-Z]"); String nItems = pctItemsInfo.next(); numberItemsSold = Integer.parseInt(nItems); // if I want the remaining part pctItemsInfo.reset(); String lastWord = pctItemsInfo.next(); System.out.println("Last word is : "+lastWord); } else { System.out.println(""); System.out.println("What is the cost of the commission item?"); itemCost = in.nextDouble(); in.nextLine(); //empties the input buffer after reading a number System.out.println("What percentage of the item cost is the commission? "); System.out.println("Enter the percentage as a whole number, i.e. 15% should be 15 :"); commissionPct = in.nextDouble(); System.out.println("How many of the commissioned item did they sell this week?"); numberItemsSold = in.nextInt(); } //How to calculate commission pay given the code above? totalPay = itemCost * numberItemsSold * commissionPct / 100; System.out.println(""); System.out.print(name + " sells items worth $"+itemCost); System.out.print(", receives %"+commissionPct+"\n on each item sold"); System.out.println(", and sold "+numberItemsSold+" items this week"); //System.out.printf("%s receives $%10.2f this week in commission\n",name,totalPay); } break; // Add case for occasional employee paid a lump sum S for up to X hours case 'O': case 'o': { double occWorked; if (fileValid) { payRate = inFile.nextDouble(); occWorked = inFile.nextDouble(); totalPay = payRate*occWorked; } else { System.out.println(""); //System.out.println(""); //payRate = 200.00; // salary pay per week System.out.println("What is the pay per occasion?"); payRate = in.nextDouble(); in.nextLine(); //empties the input buffer after reading a number System.out.println("How many occasions were worked this week?"); occWorked = in.nextDouble(); totalPay = payRate*occWorked; } System.out.println(""); System.out.println(name + " receives $"+payRate+" per occasion and worked "+occWorked+" occasions."); //System.out.printf("%s receives $%7.2f this week\n",name,totalPay); } break; default: { System.out.print("In case default: "); System.out.println("The selection was invalid."); } break; } System.out.printf("\n\n%18s%15s\n","Name","Total Pay"); System.out.printf("%18s%15.2f\n\n",name,totalPay); //System.out.println(name+"'s "+"Total Pay is : $"+totalPay); System.out.println(""); name = ""; if (fileValid) { moreEmployees = false; inFile.nextLine(); } else { System.out.println("Are there more employees to process? Y/N "); String more = in.next(); boolean notValid = true; do { if (more.equalsIgnoreCase("N")) { moreEmployees = false; notValid = false; } else if (more.equalsIgnoreCase("Y")) { notValid = false; } else { System.out.println("I'm sorry. Your input could not be processed."); System.out.println("Are there more employees to process? Y/N "); more = in.next(); } } while (notValid); in.nextLine(); } } // Variables //empTypeIn = 12; // Printing tables System.out.println("Thank you for using the pay processing system."); } // access_specifier STATIC return_type name(list of parameters with type and name) // method body public static double weeklyHourInputValidation(Scanner in, int wkNum) { // body of the method System.out.println("What is the number of hours worked for Day "+wkNum+"?"); double numberHrsWk = in.nextDouble(); // validate input if ((numberHrsWk > 24) || (numberHrsWk < 0 )) { System.out.println("Invalid value was entered"); if (numberHrsWk > 12) // not a sensible thing for the real world { numberHrsWk = 8; // return 40; } else { numberHrsWk = 2; // return 20; } } return numberHrsWk; } // error checking info // No more than 24 -> mod of 24 [check 2nd] // No less than 0 -> absolute value [check 1st] // No more than 16 hours per day -> change to 16 [check 3rd] // Must use all three cases // Must come out with a number that is between 0 and 16 // Input is a double representing hours in a day public static double HoursWorkedInDayCheck(double hours)// hours is parameter/argument { if (hours < 0) { System.out.println("hours before abs = "+hours); hours = Math.abs(hours); System.out.println("hours after abs = "+hours); } if (hours > 24) { System.out.println("hours before mod = "+hours); hours = hours % 24; System.out.println("hours after mod = "+hours); } if (hours > 16) { System.out.println("hours before >16 = "+hours); hours = 16; System.out.println("hours after >16 = "+hours); } return hours; } }