CSCI 24000 Spring 2017 Assignment #5-A Healthy Dose of Inheritance Due: 4/18/201
ID: 3812679 • Letter: C
Question
CSCI 24000 Spring 2017 Assignment #5-A Healthy Dose of Inheritance Due: 4/18/2017 This fifth assignment will allow you to explore the important OO concept of Inheritance in a Java implementation. This assignment will also give you time to explore this pillar of the oo paradigm and allow you to ramp up to the exciting, fun, yet very challenging sixth assignment. For this assignment, you are tasked with the responsibility of building the framework for the Healthy Pharmacy that we discussed in labrecitation. The Healthy Pharmacy is looking to develop a software application to model their employee hierarchy. Currently the pharmacy has the following types of job titles for their employees: Pharmacist, Pharmacy Manager, Technician, and Senior Technician. Healthy Pharmacy has elected to proceed with an hourly payment schedule for all employees -obviously the rates for each job title will vary (e.g., a Pharmacy Manager will make more per hour than a Technician). Every employee will have the following attributes Employee ID First Name Last Name Hourly Rate This program will be written in Java and must compile and run on Tesla (tesla-cs.iupui.edu) Your program will be menu driven in which you will provide the following prompts to the user: l Load Employees (From File) 2. Exit Program Once the user has loaded the employees the following options should be provided: l. Print Employee Information 2. Enter Hours Worked 3. Calculate Paychecks 4. Exit Program The text file containing employee data will be provided to you. The filename will be employees.txt-this file can be found (and downloaded) on Canvas. The file will contain four (4) employees one in each role. The following is the hourly rate for each respective job title-this is a predefined and set amount: Pharmacy Manager (ID l): S50hour Phamacist (ID 2): $40/hour Technician (D 3): S20hour Senior Technician (ID 4): S25/hourExplanation / Answer
/*
* The java program that display a menu of choice
* and allows user to enter choice. Then execute
* the corresponding method and print results to console
* */
//Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Driver
{
private final static int size=4;
private static Scanner scanner=new Scanner(System.in);
private static Employee[] emps=new Employee[size];
public static void main(String[] args)
{
double hours = 0;
boolean repeat=true;
while(repeat)
{
int choice=getChoice();
if(choice==1)
{
loadFile();
boolean hoursEntered=false;
boolean run=true;
while(run)
{
int ch=menu();
switch(ch)
{
case 1:
printInfo();
break;
case 2:
System.out.println("Please enter the hours worked:");
hours=Double.parseDouble(scanner.nextLine());
hoursEntered=true;
break;
case 3:
if(!hoursEntered)
{
System.out.print("Please enter the hours worked(option #2)");
System.out.println(" before trying to calculate the paycheck amount:");
}
printChecks(hours);
break;
case 4:
System.out.println("Goodbye!");
run=false;
repeat=false;
break;
}
}
}
else
repeat=false;
}
}
//print pay checks
private static void printChecks(double hours) {
for (Employee emp : emps)
{
emp.setHours(hours);
System.out.printf("ID:%-10s Check Amount :%5.2f ",emp.getID(),emp.getPay());
}
}
//print employee info to console
private static void printInfo() {
for (Employee employee : emps)
{
System.out.println(employee);
}
}
//Metod to read file data into array employee
private static void loadFile() {
Scanner filescanner=null;
try
{
int index=0;
filescanner=new Scanner(new File("employees.txt"));
while(filescanner.hasNextLine())
{
String dataLine=filescanner.nextLine();
String[] data=dataLine.split(",");
int jobid=Integer.parseInt(data[0]);
String empid=data[1];
String firstName=data[2];
String lastName=data[3];
String startDate=data[4];
emps[index]=new Employee(jobid, empid, firstName, lastName, startDate);
index++;
}
filescanner.close();
System.out.println("File Successfully Loaded");
}catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
//Get user choice
public static int getChoice()
{
System.out.println("1. Load Employee (From File)");
System.out.println("2. Exit Program");
System.out.println("Enter Your Selection :");
int choice=Integer.parseInt(scanner.nextLine());
return choice;
}
//Menu method
public static int menu()
{
System.out.println("1. Print Employee Information");
System.out.println("2. Enter Hours Worked");
System.out.println("3. Calculate Paychecks");
System.out.println("4. Exit Program");
System.out.println("Enter Your Selection");
int choice=Integer.parseInt(scanner.nextLine());
return choice;
}
}
------------------ ------------------ ------------------ ------------------ ------------------
/**
* The Employee class that resembles the employee object
* that set id, empid, first name, last name and data and
* call setRate to set rate and set hours method to set
* hourse
* */
//Employee.java
public class Employee
{
private int jobid;
private String empid;
private String firstName;
private String lastName;
private String startDate;
private int rate;
private double hours;
//constructor to set instance variables
public Employee(int jobid,String empid,
String firstName,String lastName,
String startDate)
{
this.jobid=jobid;
this.empid=empid;
this.firstName=firstName;
this.lastName=lastName;
this.startDate=startDate;
setRate(jobid);
setHours(0);
}
public String getID()
{
return empid;
}
public void setHours(double hours)
{
this.hours=hours;
}
//Method to set rate
private void setRate(int jobid)
{
switch (jobid)
{
case 1:
rate=50;
break;
case 2:
rate=40;
break;
case 3:
rate=20;
break;
case 4:
rate=25;
break;
}
}
//return rate
public double getRate()
{
return rate;
}
//return pay check
public double getPay()
{
return rate*hours;
}
//Override toString method
public String toString() {
return String.format("ID:%-15s Name: %-20s Rate:%-10d",empid,firstName+" "+lastName,rate);
}
}
------------------ ------------------ ------------------ ------------------ ------------------
employees.txt
1,90000123,John, Jones,12/1/2001
2,90012342,Crystal, Konard,6/1/2015
3,90006783,Tim, Duncan,7/1/2010
4,90001234,Brittany, Willis,1/1/2017
------------------ ------------------ ----------------- ------------------ ------------------
Sample Output:
1. Load Employee (From File)
2. Exit Program
Enter Your Selection :
1
File Successfully Loaded
1. Print Employee Information
2. Enter Hours Worked
3. Calculate Paychecks
4. Exit Program
Enter Your Selection
1
ID:90000123 Name: John Jones Rate:50
ID:90012342 Name: Crystal Konard Rate:40
ID:90006783 Name: Tim Duncan Rate:20
ID:90001234 Name: Brittany Willis Rate:25
1. Print Employee Information
2. Enter Hours Worked
3. Calculate Paychecks
4. Exit Program
Enter Your Selection
3
Please enter the hours worked(option #2) before trying to calculate the paycheck amount:
ID:90000123 Check Amount : 0.00
ID:90012342 Check Amount : 0.00
ID:90006783 Check Amount : 0.00
ID:90001234 Check Amount : 0.00
1. Print Employee Information
2. Enter Hours Worked
3. Calculate Paychecks
4. Exit Program
Enter Your Selection
2
Please enter the hours worked:
40
1. Print Employee Information
2. Enter Hours Worked
3. Calculate Paychecks
4. Exit Program
Enter Your Selection
3
ID:90000123 Check Amount :2000.00
ID:90012342 Check Amount :1600.00
ID:90006783 Check Amount :800.00
ID:90001234 Check Amount :1000.00
1. Print Employee Information
2. Enter Hours Worked
3. Calculate Paychecks
4. Exit Program
Enter Your Selection
4
Goodbye!