You are on page 1of 34

9.

15

Pieceworker.java
public class Pieceworker extends Employee
{
private double wage;
private int pieces;

public Pieceworker(String first, String last, String ssn,


Date bdate, double w, int p)
{
super(first, last, ssn, bdate);
setWage(w);
setPieces(p);
}

public void setWage(double w)


{
wage = w;
}

public void setPieces(int p)


{
pieces = p;
}

public double getWage()


{
return wage;
}

public int getPieces()


{
return pieces;
}

@Override
public double earnings() {
return getPieces() * getWage();
}

@Override
public String toString()
{
return String.format("Pieceworker: %s\n%s: $%,.2f\n%s: %d",
super.toString(), "Wage", getWage(), "Pieces", getPieces());
} // end method toString

}
SalariedEmployee.java
public class SalariedEmployee extends Employee
{
private double weeklySalary;

// four-argument constructor
public SalariedEmployee(String first, String last, String ssn,
Date bdate, double salary)
{
super(first, last, ssn, bdate); // pass to Employee constructor
setWeeklySalary(salary); // validate and store salary
} // end four-argument SalariedEmployee constructor

// set salary
public void setWeeklySalary(double salary)
{
if (salary >= 0.0)
weeklySalary = salary;
else
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
} // end method setWeeklySalary

// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary

// calculate earnings; override abstract method earnings in Employee


@Override
public double earnings()
{
return getWeeklySalary();
} // end method earnings

// return String representation of SalariedEmployee object


@Override
public String toString()
{
return String.format("salaried employee: %s\n%s: $%, .2f",
super.toString(), "weekly salary", getWeeklySalary());
} // end method toString
} // end class SalariedEmployee

Employee.java
// Employee abstract superclass

public abstract class Employee


{
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthDate;

// three-argument constructor
public Employee(String first, String last, String ssn, Date bdate)
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthDate = bdate;
} // end three-argument Employee constructor

// set first name


public void setFirstName(String first)
{
firstName = first; // should validate
} // end method setFirstName

// return first name


public String getFirstName()
{
return firstName;
} // end method getFirstName

// set last name


public void setLastName(String last)
{
lastName = last; // should validate
} // end method setLastName

// return last name


public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber(String ssn)
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber

// return social security number


public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber

public void setBirthDate(Date bdate)


{
birthDate = bdate;
}

public Date getBirthDate()


{
return birthDate;
}

// return String representation of Employee object


@Override
public String toString()
{
return String.format("%s %s\nSocial Security Number: %s\nBirthdate: %s",
getFirstName(), getLastName(), getSocialSecurityNumber(), getBirthDate());
} // end method toString

// abstract method overridden by concrete subclasses


public abstract double earnings(); // no implementation here
} // end abstract class Employees

HourlyEmployee.java

// HourlyEmployee class extends Employee

public class HourlyEmployee extends Employee


{
private double wage; // wage per hour
private double hours; // hours worked for week

// five-argument constructor
public HourlyEmployee(String first, String last, String ssn,
Date bdate, double hourlyWage, double hoursWorked)
{
super(first, last, ssn, bdate);
setWage(hourlyWage); // validate hourly wage
setHours(hoursWorked); // validate hours worked
} // end five-argument HourlyEmployee constructor

// set wage
public void setWage(double hourlyWage)
{
if (hourlyWage >= 0.0)
wage = hourlyWage;
else
throw new IllegalArgumentException(
"Hourly wage must be >= 0.0");
} // end method setWage

// return wage
public double getWage()
{
return wage;
} // end method getWage

// set hours worked


public void setHours(double hoursWorked)
{
if ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0))
hours = hoursWorked;
else
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");
} // end method setHours

// return hours worked


public double getHours()
{
return hours;
} // end method getHours

// calculate earnings; override abstract method earnings in Employee


@Override
public double earnings()
{
if ( getHours() <= 40)
return getWage() * getHours();
else
return 40 * getWage() + ( getHours() - 40) * getWage() * 1.5;
} // end method earnings

// return String representation of HourlyEmployee object


@Override
public String toString()
{
return String.format("hourly employee: %s\n%s: $%,.2f; %s: %,.2f",
super.toString(), "hourly wage", getWage(),
"hours worked", getHours() );
} // end method toString
} // end class HourlyEmployee

Date.java
// Date class declaration.

public class Date


{
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year

private static final int[] daysPerMonth = // days in each month


{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 , 31};

// constructor: call checkMonth to confirm proper value for month;


// call checkDay to confirm proper value for day
public Date(int theMonth, int theDay, int theYear)
{
month = checkMonth(theMonth); // validate month
year = theYear; // could validate the year
day = checkDay( theDay); // validate day

System.out.printf(
"Date object constructor for date %s\n", this);
} // end Date constructor

// utility method to confirm proper month value


private int checkMonth(int testMonth)
{
if ( testMonth > 0 && testMonth <= 12) // validate month
return testMonth;
else // month is invalid
throw new IllegalArgumentException("month must be 1-12");
} // end method checkMonth

// utility method to confirm proper day value based on month and year
private int checkDay(int testDay)
{
// check if day in range for month
if (testDay > 0 && testDay <= daysPerMonth[month])
return testDay;

// check for leap year


if ( month == 2 && testDay == 29 && (year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;

throw new IllegalArgumentException(


"day out-of-range for the specified month and year");
} // end method checkDay

public int getMonth()


{
return month;
}

public int getDay()


{
return day;
}

public int getYear()


{
return year;
};
// return a String of the form month/day/year
public String toString()
{
return String.format("%d/%d/%d", month, day, year);
} // end method toString
} // end class Date

Main.java
// Employee hierarchy test program.

public class Main


{
public static void main(String[] args)
{
int currentMonth = 3;
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee("John", "Smith", "111-11-1111", new Date(1,31,1990),
8000.00);
HourlyEmployee hourlyEmployee =
new HourlyEmployee("Karen", "Price", "222-22-2222", new Date(2,28,1990), 16.75,
40);
Pieceworker pieceworker =
new Pieceworker(
"James", "Tran", "555-55-5555", new Date(7,2,1997), .60, 1500);

// create four-element Employee array


Employee[] employees = new Employee[5];

// initialize array with Employees


employees[0] = salariedEmployee;
employees[1] = hourlyEmployee;
// employees[2] = commissionEmployee;
// employees[3] = basePlusCommissionEmployee;
employees[4] = pieceworker;

System.out.println("Employees plocessed polymorphically: \n");

// generically process each element in array employees


for ( Employee currentEmployee : employees)
{
System.out.println(currentEmployee); // invokes toString
System.out.printf(
"earned $%,.2f\n\n", currentEmployee.earnings());
} // end for

// get type name of each object in employees array


for ( int j = 0; j < employees.length; j++)
System.out.printf("Employee %d is a %s\n", j,
employees[j].getClass().getName());
} // end main
} // end class PayrollSystemTest

sample output

Date object constructor for date


1/31/1990
Date object constructor for date
2/28/1990
Date object constructor for date
7/2/1997
Employees plocessed
polymorphically:

salaried employee: John


Smith
Social Security Number: 111-11-
1111
Birthdate:
1/31/1990
weekly salary: $
8,000.00
earned
$8,000.00

hourly employee: Karen


Price
Social Security Number: 222-22-
2222
Birthdate:
2/28/1990
hourly wage: $16.75; hours worked:
40.00
earned $670.00

QUESTION 10.2

/**Employee.java**/

public abstract class Employee

private final String firstName;

private final String lastName;

private final String SocialSecurityNumber;

private final Date birthDate;

//constructor

public Employee(String firstName,String lastName, String socialSecurityNumber,Date


birthDate)

{
this.firstName=firstName;

this.lastName=lastName;

this.SocialSecurityNumber=socialSecurityNumber;

this.birthDate=birthDate;

//return first name

public String getFirstName()

return firstName;

//return last name

public String getLastName()

return lastName;

//return social security number

public String getSocialSecurityNumber()

return SocialSecurityNumber;

//return date of birth

public Date getBirthDate()


{

return birthDate;

//return String representation of Employee object

public String toString()

return String.format("%s %s%nsocial security number: %s%nDOB: %s",getFirstName(),


getLastName(), getSocialSecurityNumber(),getBirthDate());

//abstract method

public abstract double earnings();

}//end class Employee

/**SalariedEmployee.java**/

import java.util.IllegalFormatConversionException;

public class SalariedEmployee extends Employee

private double weeklySalary;

//constructor

public SalariedEmployee(String firstName,String lastName,String socialSecurityNumber,


double weeklySalary,Date birthDate)

super(firstName,lastName,socialSecurityNumber,birthDate);

if(weeklySalary <0.0)

throw new IllegalArgumentException("Weekly salary must be >= 0.0");


this.weeklySalary=weeklySalary;

public void setWeeklySalary(double weeklySalary)

if(weeklySalary <0.0)

throw new IllegalArgumentException("Weekly salary must be >= 0.0");

this.weeklySalary=weeklySalary;

public double getWeeklySalary()

return weeklySalary;

public String toString()

return String.format("salaried employee: %s%n%s: $%,.2f",super.toString(),"weekly


salary",getWeeklySalary());

@Override

public double earnings()

return weeklySalary;

}
}

/**HourlyEmployee.java**/

public class HourlyEmployee extends Employee

private double wage;

private double hours;

//constructor

public HourlyEmployee(String firstName, String lastName,String socialSecurityNumber,


double wage, double hours,Date birthDate)

super(firstName,lastName,socialSecurityNumber,birthDate);

if(wage <0.0)//validate wage

throw new IllegalArgumentException("Hourly wage must be >= 0.0");

if((hours < 0.0) || (hours >168.0))// validate hours

throw new IllegalArgumentException("Hours worked must be <= 168.0");

this.wage=wage;

this.hours=hours;

public void setWage(double wage)

if(wage <0.0)

throw new IllegalArgumentException("Hourly wage must be >= 0.0");

this.wage=wage;

}
public double getWage()

return wage;

public void setHours(double hours)

if((hours < 0.0) || (hours >168.0))

throw new IllegalArgumentException("Hours worked must be <= 168.0");

this.hours=hours;

public double getHours()

return hours;

@Override

public double earnings()

if(getHours()<=40)

return getWage()*getHours();

else

return 40*getWage()+(getHours()-40)*getWage()*1.5;

public String toString()


{

return String.format("hourly employee: %s%n%s: $%,.2f; %s:


%,.2f",super.toString(),"hourly wage",getWage(),"Hours worked",getHours());

/**CommissionEmployee.java**/

public class CommissionEmployee extends Employee

private double grossSales;

private double commissionRate;

//constructor

public CommissionEmployee(String firstName,String lastName,String


socialSecurityNumber, double grossSales,double commissionRate,Date birthDate)

super(firstName,lastName,socialSecurityNumber,birthDate);

if(commissionRate <0.0 || commissionRate >=1.0)//validate commission rate

throw new IllegalArgumentException("Commission rate must be > 0.0 and <1.0");

if(grossSales < 0.0)//validate gross sales

throw new IllegalArgumentException("Gross sales must be >= 0.0");

this.grossSales=grossSales;

this.commissionRate=commissionRate;

public void setGrossSales(double grossSales)

{
if(grossSales < 0.0)

throw new IllegalArgumentException("Gross sales must be >= 0.0");

this.grossSales=grossSales;

public double getGrossSales()

return grossSales;

public void setCommissionRate(double commissionRate)

if(commissionRate <0.0 || commissionRate >=1.0)

throw new IllegalArgumentException("Commission rate must be > 0.0 and <1.0");

this.commissionRate=commissionRate;

public double getCommissionRate()

return commissionRate;

@Override

public double earnings()

return getCommissionRate() * getGrossSales();

}
public String toString()

return String.format("%s: %s%n%s: $%,.2f; %s: %.2f","commission employee",


super.toString(),"gross sales", getGrossSales(),"commission rate", getCommissionRate());

/**BasePlusCommissionEmployee.java**/

public class BasePlusCommissionEmployee extends CommissionEmployee

private double baseSalary;

//constructor

public BasePlusCommissionEmployee(String firstName,String lastName,String


socialSecurityNumber, double grossSales, double commissionRate, double baseSalary,Date
birthDate)

super(firstName,lastName,socialSecurityNumber,grossSales,commissionRate,birthDate);

if(baseSalary < 0.0 )//validate base salary per week

throw new IllegalArgumentException("Base salary must be >= 0.0");

this.baseSalary=baseSalary;

public void setBaseSalary(double baseSalary)

if(baseSalary < 0.0 )

throw new IllegalArgumentException("Base salary must be >= 0.0");


this.baseSalary=baseSalary;

public double getBaseSalary()

return baseSalary;

public double earnings()

return getBaseSalary()+super.earnings();

public String toString()

return String.format("%s %s: %s: $%,.2f","base-salaried",super.toString(),"base


salary",getBaseSalary());

/**PayrollSystemTest.java**/

import java.util.Calendar;

public class PayrollSystemTest

public static void main(String[] args)

//Create subclass objects


SalariedEmployee salariedEmployee=new SalariedEmployee("John","Smith","111-11-
1111",800.00,new Date(07,23,2016));

HourlyEmployee hourlyEmployee=new HourlyEmployee("Karen","Price","222-22-


2222",16.75,40,new Date(4,2,1981));

CommissionEmployee commissionEmployee= new


CommissionEmployee("Sue","Jones","333-33-3333",10000,.06,new Date(5,8,1993));

BasePlusCommissionEmployee basePlusCommissionEmployee=new
BasePlusCommissionEmployee("Bob","Lewis","444-44-4444",5000,.04,300,new
Date(2,29,2000));

//create four-element Employee array

Employee [] employees=new Employee[4];

//Initialize array with Employees

employees[0]=salariedEmployee;

employees[1]=hourlyEmployee;

employees[2]=commissionEmployee;

employees[3]=basePlusCommissionEmployee;

System.out.printf("Employees processed polymorphically: %n%n");

//calculate the payroll for each Employee (polymorphically)

for(Employee currentEmployee: employees)

//add a $100.00 bonus to the person’s payroll amount if the current month is

//the one in which the Employee’s birthday occurs.

if
(currentEmployee.getBirthDate().getMonth()==Calendar.getInstance().get(Calendar.MONTH)+
1)
{

System.out.printf("%s%n%s:
$%,.2f%n",currentEmployee,"earned",currentEmployee.earnings());

System.out.printf("Birthday bonus : $%,.2f%n",100.00);

System.out.printf("earned $%,.2f%n%n", currentEmployee.earnings()+100);

//Otherwise

else

System.out.printf("%s%n%s:
$%,.2f%n%n",currentEmployee,"earned",currentEmployee.earnings());

for(int j=0;j<employees.length;j++)

System.out.printf("Employee %d is a %s%n",j,employees[j].getClass().getName());

/**Date.java**/

public class Date

private int month;

private int day;

private int year;


private static final int[] daysPerMonth={0,31,28,31,30,31,30,31,31,30,31,30,31};

//construcotr: confirm proper value for month and day given the year

public Date(int month,int day,int year)

//check if month in range

if(month <=0 || month >12)

throw new IllegalArgumentException("month ("+month+") must be 1-12");

//Check if day in range for month

if(day <=0 || (day > daysPerMonth[month] && !(month == 2 && day == 29)))

throw new IllegalArgumentException("day ("+day+") out-of-range for the specified


month and year");

//Check for leap year if month is 2 and day is 29

if(month ==2 && day==29 && !(year % 400 ==0 || (year % 4 ==0 && year % 100 !=0 )))

throw new IllegalArgumentException("day ("+day+") out-of-range for the specified


month and year");

this.month=month;

this.day=day;

this.year=year;

//return Month

public int getMonth()

return month;

}
//return day

public int getDay()

return day;

//return year

public int getYear()

return year;

//return a String of the form month/day/year

public String toString()

return String.format("%d/%d/%d",getMonth(),getDay(),getYear());

}//end class Date

QUESTION 12.4

PieceWorkerTest.java:

package sample1;

public class PieceWorkerTest {

public static void main(String args[]){

Employee[] empArr = new PieceWorker[3];

empArr[0] = new PieceWorker("john", "ray", "1",3.0,100);


empArr[1] = new PieceWorker("jon", "ray", "2",4.0,200);

empArr[2] = new PieceWorker("san", "ray", "3",2.0,100);

for(Employee e :empArr){

System.out.println("Employee : "+e.toString()+"\tEarnings : "+e.earnings());

PieceWorker.java:

package sample1;

public class PieceWorker extends Employee {

private double wage;

private int pieces;

public double getWage() {

return wage;

public void setWage(double wage) {

this.wage = wage;

public int getPieces() {

return pieces;

public void setPieces(int pieces) {

this.pieces = pieces;
}

public PieceWorker(String firstName, String lastName, String socialSecurityNumber) {

super(firstName, lastName, socialSecurityNumber);

// TODO Auto-generated constructor stub

public PieceWorker(String firstName, String lastName, String socialSecurityNumber,double


wage,int pieces) {

super(firstName, lastName, socialSecurityNumber);

this.wage=wage;

this.pieces = pieces;

// TODO Auto-generated constructor stub

@Override

public double earnings() {

// TODO Auto-generated method stub

return this.getWage()*this.getPieces();

Employee.java:

package sample1;

public class Employee {

private String firstName;

private String lastName;

private String socialSecurityNumber;


public Employee(String firstName, String lastName, String socialSecurityNumber) {

super();

this.firstName = firstName;

this.lastName = lastName;

this.socialSecurityNumber = socialSecurityNumber;

public String getFirstName() {

return firstName;

public void setFirstName(String firstName) {

this.firstName = firstName;

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

this.lastName = lastName;

public String getSocialSecurityNumber() {

return socialSecurityNumber;

public void setSocialSecurityNumber(String socialSecurityNumber) {

this.socialSecurityNumber = socialSecurityNumber;
}

public double earnings() {

return 0.0;

public String toString() {

return "FirstName : " + this.getFirstName() + "\t Last name : " + this.getLastName() + "\t SSN : "

+ this.getSocialSecurityNumber();

QUESTION 10.15

PayableInterfaceTest.java
------------------------------------------------------------
public class PayableInterfaceTest{

public static void main(String[] args) {

Payable[] payableObjects = new Payable[6];

payableObjects[0] = new Invoice("01234", "seat", 2, 375.00);


payableObjects[1] = new Invoice("56789", "tire", 4, 79.95);
payableObjects[2] =
new SalariedEmployee("John", "Smith", "111-11-1111", 800.00);
payableObjects[3] =
new HourlyEmployee("Bob", "Marley", "222-22-2222", 33.00, 40);
payableObjects[4] =
new CommissionEmployee("Jessica", "Johnson", "333-33-3333", 1350, 0.85);
payableObjects[5] =
new BasePlusCommissionEmployee("Jenny", "James", "666-66-6666", 1175, 0.95,
660.00);

System.out.println(
"Invoices and Employees processed polymorphically:\n");

for(Payable currentPayable : payableObjects){


System.out.printf("%s \n%s: $%,.2f\n\n",
currentPayable.toString(),
"Payment due", currentPayable.getPaymentAmount());
}

for (Payable currentPayable : payableObjects){


if(currentPayable instanceof BasePlusCommissionEmployee){
BasePlusCommissionEmployee employee =
(BasePlusCommissionEmployee)currentPayable;
double employeePay = employee.getBaseSalary();
double newSalary = employeePay + (employeePay * .10);
employee.setBaseSalary(newSalary);

System.out.printf("The base pay of " + employee.getFirstName() + " " +


employee.getLastName() + " with a 10%% raise is %.2f\n",
newSalary);
}

}
}
}
------------------------------------------------------------------------------------------------------------
BasePlusCommissionEmployee.java
------------------------------------------------------------------
public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary;
public BasePlusCommissionEmployee(String first, String last, String ssn,
double sales, double rate, double salary){
super(first, last, ssn, sales, rate);
setBaseSalary(salary);
}

public void setBaseSalary(double salary){


if(salary >= 0.0){
baseSalary = salary;
}else{
throw new IllegalArgumentException(
"Base salary must be >= 0.0");
}
}

public double getBaseSalary(){


return baseSalary;
}

@Override
public double getPaymentAmount(){
return getBaseSalary() + super.getPaymentAmount();
}

@Override
public String toString(){
return String.format("%s %s; %s: $%,.2f",
"Base-salaried", super.toString(),
"Base salary", getBaseSalary());
}
}
------------------------------------------------------------------------------------------------
CommissionEmployee.java
------------------------------------------------------------
public class CommissionEmployee extends Employee
{
private double grossSales;
private double commissionRate;

public CommissionEmployee(String first, String last, String ssn,


double sales, double rate){
super(first, last, ssn);
setGrossSales(sales);
setCommissionRate(rate);
}

public void setCommissionRate(double rate){


if(rate > 0.0 && rate < 1.0){
commissionRate = rate;
}else{
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
}
}

public double getCommissionRate(){


return commissionRate;
}

public void setGrossSales(double sales){


if(sales >= 0.0){
grossSales = sales;
}else{
throw new IllegalArgumentException(
"Gross sales must be >= 0.0");
}
}
public double getGrossSales(){
return grossSales;
}

@Override
public double getPaymentAmount(){
return getCommissionRate() * getGrossSales();
}

@Override
public String toString(){
return String.format("%s: %s\n%s: $%,.2f; %s: %.2f",
"Commission employee", super.toString(),
"Gross sales", getGrossSales(),
"Commission rate", getCommissionRate());
}

}
-----------------------------------------------------------------------------------------------
Employee.java
------------------------------------------------
public abstract class Employee implements Payable{
private String firstName;
private String lastName;
private String socialSecurityNumber;

public Employee(String firstName, String lastName, String socialSecurityNumber) {


this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}

public String getFirstName() {


return firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public String getLastName() {


return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getSocialSecurityNumber() {


return socialSecurityNumber;
}

public void setSocialSecurityNumber(String socialSecurityNumber) {


this.socialSecurityNumber = socialSecurityNumber;
}

@Override
public String toString(){
return String.format("%s %s\nSocial Security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber());
}
}
--------------------------------------------------------------------------------------------------
HourlyEmployee.java
---------------------------------------------------------
public class HourlyEmployee extends Employee
{
private double wage;
private double hours;

public HourlyEmployee(String first, String last, String ssn,


double hourlyWage, double hoursWorked){
super(first, last, ssn);
setWage(hourlyWage);
setHours(hoursWorked);
}

public void setWage(double hourlyWage){


if(hourlyWage >= 0.0){
wage = hourlyWage;
}else{
throw new IllegalArgumentException(
"Houly wage must be >= 0.0");
}
}

public double getWage(){


return wage;
}
public void setHours(double hoursWorked){
if ((hoursWorked >= 0.0)&&(hoursWorked <= 168.0)){
hours = hoursWorked;
}else{
throw new IllegalArgumentException(
"Hours worked must be >= 0.0 and <= 168.0");
}
}

public double getHours(){


return hours;
}

@Override
public double getPaymentAmount(){

if(getHours() <= 40){


return getWage()*getHours();
}else{
return 40*getWage() + (getHours()-40)*getWage()*1.5;
}
}

@Override
public String toString(){
return String.format("Hourly employee: %s\n%s: $%,.2f; %s: %,.2f",
super.toString(), "Hourly wage", getWage(),
"Hours worked", getHours());
}
}
-------------------------------------------------------------------------------------------------------
Invoice.java
--------------------------------------------
public class Invoice implements Payable{

private String partNumber;


private String partDescription;
private int quantity;
private double pricePerItem;

public Invoice(String part, String description, int count,


double price){
partNumber = part;
partDescription = description;
setQuantity(count);
setPricePerItem(price);
}

public String getPartNumber() {


return partNumber;
}

public void setPartNumber(String partNumber) {


this.partNumber = partNumber;
}

public String getPartDescription() {


return partDescription;
}

public void setPartDescription(String partDescription) {


this.partDescription = partDescription;
}

public int getQuantity() {


return quantity;
}

public void setQuantity(int quantity) {


if(quantity >= 0){
this.quantity = quantity;
}else{
throw new IllegalArgumentException("Quantity must be >=0");
}
}

public double getPricePerItem() {


return pricePerItem;
}
public void setPricePerItem(double pricePerItem) {
if(pricePerItem >= 0){
this.pricePerItem = pricePerItem;
}else{
throw new IllegalArgumentException(
"Price per item must be >=0");
}
}

@Override
public String toString(){
return String.format("%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f",
"Invoice", "Part number", getPartNumber(), getPartDescription(),
"Quantity", getQuantity(), "Price per item", getPricePerItem());
}

@Override
public double getPaymentAmount(){
return getQuantity()*getPricePerItem();
}
}
-----------------------------------------------------------------------------------------------------
Payable.java
-------------------------------------------------
public interface Payable {

double getPaymentAmount();
}
-------------------------------------------------------------------------------------------------
SalariedEmployee.java
--------------------------------------------------------------
public class SalariedEmployee extends Employee{

private double weeklySalary;

public SalariedEmployee(String firstName, String lastName, String socialSecurityNumber,


double weeklySalary) {
super(firstName, lastName, socialSecurityNumber);
setWeeklySalary(weeklySalary);
}

public double getWeeklySalary() {


return weeklySalary;
}

public void setWeeklySalary(double weeklySalary) {


if(weeklySalary >= 0.0){
this.weeklySalary = weeklySalary;
}else{
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0");
}
}

@Override
public double getPaymentAmount(){
return getWeeklySalary();
}

@Override
public String toString(){
return String.format("Salaried employee: %s\n%s: $%,.2f",
super.toString(), "Weekly salary", getWeeklySalary());
}
}

You might also like