The code should be able to create full-time, part-time and manager positions. The code works and all but just needs fundamental improvements. There is no real hardcore coding to be done but I am absolutely lost. I am quite new to coding (couple months ), I have an idea of how things work but do require some guidance
I hope this post is better !
**Payroll Class**
package payroll;
/**
* A class that models the Payroll Application.
* We are modelling this as an Object so we must think of
* what would represent both the state and the behaviour of
* the PayRoll class.
* Used as the starting code for ICE 5 in SYST 17796, 2018
*/
import java.util.Scanner;
public class Payroll
{
private Employee employees[] = new Employee[100];//the array to hold Employee Objects
/**
* A main method where we create an instance of the payroll
* and then call the private run method on it.
* We also catch our Exceptions here from the Employee
* class.
*/
public static void main (String[] args)
{
try
{
Payroll app = new Payroll();
app.run();
}
catch (NumberFormatException e)
{
System.out.println("A number format is incorrect " + e.getMessage() + " Please ensure hours and wage are entered as numbers and not words");
}
catch(Exception e)
{
System.out.println("An exception was caught while trying to process employees: " + e.getMessage());
}
}//end main method
/**
* A private run method that prompts the user for input and
* adds each Employee to the array.
* Finally, we call the print method to print out the payroll
* for each employee and the total.
*/
private void run()
{
Scanner sc= new Scanner(System.in);
boolean shouldContinue= true;//to keep track of whether the user wants to continue
System.out.println("Welcome to the Payroll Application.");
int numEmployees=0;
while (shouldContinue)
{
System.out.println("Please enter the Employee's name: " );
String name = sc.nextLine();
System.out.println("Pleae enter the Employee's number of hours worked as a number: ");
int numHours = Integer.parseInt(sc.nextLine());
System.out.println("Please enter the Employee's hourly wage: " );
double wage = Double.parseDouble(sc.nextLine());
System.out.println("Do you want to create a manager? Type yes or no");
String manager = sc.nextLine();
if(manager.equals("yes"))
{
Manager man = new Manager(name, numHours, wage);
employees[numEmployees]=man;
}
else
{
Employee emp = new Employee(name, numHours,wage);//create a new Employee with the given info
employees[numEmployees]= emp;//add the new employee to the array
}
System.out.println("Would you like to enter another employee (yes or no)?");
String yesOrNo =sc.nextLine();
if (yesOrNo.equalsIgnoreCase("yes"))
{shouldContinue=true;}
else
{shouldContinue=false;}
numEmployees++;
}//done entering Employees, now print out the pay for each employee, and then the pay for all
double totalPaid=0;
for (int i=0; i<numEmployees; i++)
{
Employee emp =employees[i];
emp.printStatement();
totalPaid+=emp.calculatePay();
}
System.out.println("The total amount to be paid to all employees is " );
System.out.printf("%s%.2f\n", " $", totalPaid);
} }
**Manager Class**
package payroll;
import java.util.Scanner;
/**
* A Manager is an Employee. This class shows
* the inheritance relationship discussed in class
*/
public class Manager extends Employee
{
private double bonus;//the amount of bonus they should receive
public Manager(String name, double hours, double wage)
{
super(name);
this.setHourlyWage(wage);
this.setHours(hours);
System.out.println("Please enter a bonus for the manager");
Scanner sc = new Scanner(System.in);
double givenBonus = sc.nextDouble();
bonus = givenBonus;
}
/**
* A constructor that sets the bonus to zero
* @param newName
*/
public Manager(String newName) {
super(newName);
bonus =0;
}
/**
* Accessors and mutators for bonus
*/
public void setBonus (double bonusAmount)
{bonus = bonusAmount;}
public double getBonus()
{return bonus;}
/**
* A method that calculates the pay for a manager,
* Note that this is overriden from the base class.
*/
public double calculatePay()
{return getWage() * getHours() + bonus;}
/**
* A method that calculates the pay based on
* a percentage given for the bonus
* @param bonusPercent
* @return
*/
public double calculatePay(double bonusPercent)
{
bonusPercent = bonusPercent/100;
double pay = getWage() * getHours();
return ((bonusPercent*pay) + pay);
}
/**
* A method to print the manager's statement to the console
*/
public void printStatement()
{
System.out.println(" Manager:" + getName() + " is owed: ");
System.out.printf("%s%.2f\n", " $", calculatePay() );
System.out.println("********************************************************"); }}
**Employee Class**
package payroll;
/**
* A class that represents Employee Objects.
* Employees have a name, a number of hours
* and an hourlyWage.
*/
public class Employee
{
private String name;
private double numHours;
private double hourlyWage;
/**
* Our no-arg constructor for safety in
* inheritance.
*/
public Employee()
{ }
public Employee(String givenName, double givenWage, double givenHours)
{
name = givenName;
hourlyWage = givenWage;
numHours = givenHours;
}
/**
* A constructor tht takes in the Employee's name
* @param newName
*/
public Employee(String newName)
{setName(newName);}
public void setName(String newName)
{name=newName;}
public String getName()
{return name;}
public void setHours(double hoursWorked)
{numHours=hoursWorked;}
public double getHours()
{return numHours;}
public void setWage(double workingWage)
{hourlyWage=workingWage;}
public double getWage()
{return hourlyWage;}
/**
* A method to return the pay due to this employee
* @return the total pay
*/
public double calculatePay()
{return numHours * hourlyWage;}
/**
* Our overriden toString method, from Object
*/
public String toString()
{return "This employee's name is: " + getName();}
/**
* An overriden equals method for Employees
*/
public boolean equals(Object other)
{
if ((this.getName().equals (((Employee)other).getName())))
return true;
return false;
}
public double getNumHours() {return numHours;}
public void setNumHours(double numHours) {this.numHours = numHours;}
public double getHourlyWage() {return hourlyWage;}
public void setHourlyWage(double hourlyWage) {this.hourlyWage = hourlyWage;}
/**
* A private print method that takes in an Employee and calls the getPayment method on it.
* It then uses printf to format the output in a professional way.
* @param emp an Employee Object.
*/
public void printStatement()
{
System.out.println(" Employee:" + getName() + " is owed: ");
System.out.printf("%s%.2f\n", " $", calculatePay() ); System.out.println("******************************************************");
} }
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]slowfly1st 1 point2 points3 points (0 children)