/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code26sep17; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code26Sep17 { /** * @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; //for(i = 0; i < 5; i++ ) while (moreEmployees) { 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; } double payRate; payRate = 0.0; char empType = ' '; String empTypeIn; 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("You entered "+empType); //System.out.println("empType == 'H' ? "+(empType=='H')); System.out.println(""); double numberHrsWorked = 45; // hours worked in a week System.out.println("What is the number of hours worked?"); numberHrsWorked = in.nextDouble(); /* Nested if-else has three cases which each checked two values for a total of 6 cases */ switch (empType) { case 'H': case 'h': { // calculating hourly pay 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.println(name+" worked "+numberHrsWorked+" in 1 week."); System.out.println("Their work week is 39 hours and the overtime pay rate is "+overtimeRate); */ //System.out.println("The pay per hour for "+name+" is "+payRate); } break; case 's': case 'S': { System.out.println(""); //payRate = 500.00; // salary pay per week System.out.println("What is the weekly salary?"); payRate = in.nextDouble(); totalPay = payRate; in.nextLine(); //empties the input buffer after reading a number /* 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': { System.out.println(""); double itemCost; // cost for one item that pays commission System.out.println("What is the cost of the commission item?"); itemCost = in.nextDouble(); in.nextLine(); //empties the input buffer after reading a number int numberItemsSold; // items sold in a week System.out.println("How many of the commissioned item did they sell this week?"); numberItemsSold = in.nextInt(); double commissionPct; // percent paid for the commission 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(); //How to calculate commission pay given the code above? totalPay = itemCost * numberItemsSold * commissionPct / 100; /* System.out.println(""); System.out.println(name + " sells items worth $"+itemCost); System.out.println(name + " receives %"+commissionPct+" on each item sold"); System.out.println(name + " 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': { 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?"); double 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); } default: { System.out.print("In case default: "); System.out.println("The selection was invalid."); } break; } System.out.printf("\n\n%18s%10s\n","Name","Total Pay"); System.out.printf("%18s%10.2f\n\n",name,totalPay); //System.out.println(name+"'s "+"Total Pay is : $"+totalPay); System.out.println(""); name = ""; System.out.println("Are there more employees to process? Y/N "); String more = in.next(); /* if ( ! (more.equalsIgnoreCase("Y")) ) { moreEmployees = false; } */ 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."); /* Sep 28 Quiz: Write in words the algorithm for determining if a year is a leap year or a normal year? Examples - are these leap years? 1960 yes 1962 no 2011 no 2012 yes --------- 1900 no 1800 no 1700 no -------- 2000 yes 1600 yes */ } }