The purpose of this assignment is to use an ArrayList, inheritance, overridden m
ID: 3737182 • Letter: T
Question
The purpose of this assignment is to use an ArrayList, inheritance, overridden methods, interfaces, and polymorphism. In addition you will use multiple packages. In this application you will create an interface to describe things that are "payable". To use this interface you will create classes for two types of things that are paid - employees and bills. Employees will be modeled using a hierarchy of classes for different types of employees. There will be a single class for bills that are paid. The overall UML class diagram is shown below. Notice Payable is an interface and Employee is an abstract class.
The UML class diagram for each service class and interface is provided below.
These classes are all service classes. In addition you will need to create an application class named ManagePayablesApplication. Objects of the service classes will be processed polymorphically in the ManagePayablesApplication class. Observe how you can store any of the service class objects in a properly created ArrayList. The detail requirements for each class are provided below.
Create an Eclipse project named Payments. Use a package name of edu.arizonastate.payments Add one class to this package. The class name is ManagePayablesApplication. This class will have the main method. More requirements for this class are listed below.
Create a second package named edu.arizonastate.payable. Create an interface in this package named Payable. Create one public abstract method in this interface named computeAmountToPay. This method should return a double and have no parameters.
Create a third package named edu.arizonastate.bill. Add a class named Bill to this package. This class represents an amount we owe to some vendor. The following requirements apply to the Bill class.
Create three private variables. Use a String to represent the vendor name. Use a double to represent the amount owed. Use a LocalDate to represent the date when the amount is due.
The Bill class must implement the Payable interface. The computeAmountToPay method should return the amount owed. You will need to import the edu.arizonastate.payable package.
Create get and set methods for each private variable. Overload the set method for the due date. Per the UML class diagram, this overloaded method should have three integers that represent the year, month, and day of the date. Use this data to create the LocalDate object. Throw the DateTimeException if the LocalDate object cannot be created. Implement the following edits: the vendor name cannot be null nor can it have a length less than one; the amount owed must be greater than zero; the due date cannot be null. Throw an InvalidArgumentException if any of the values are invalid. Add this exception class to a new package named edu.arizonastate.exceptions.
public class InvalidArgumentException extends Exception {
public InvalidArgumentException(){
super("Invalid values sent to method");
}
public InvalidArgumentException(String msg){
super(msg);
Create a constructor that has parameters for the vendor, amount, and due date. Call the set methods from the constructor to use these parameters to update the class variables so the edits only have to be coded once.
Create a toString method that displays the class name and the values of each of the three instance variables.
Create a fourth package named edu.arizonastate.employees. This package will contain the following classes: Employee, HourlyEmployee, Manager, and SalesManager.
The following requirements apply to the Employee class.
Create three private instance variables. Use a String to represent the employee's first name. Use a String to represent the employee's last name. Use an int to represent the employee's ID.
The Employee class must implement the Payable interface. Do NOT implement the computeAmountToPay method. This will make the Employee class an abstract class. You will still need to import the edu.arizonastate.payable package.
Create get and set methods for each private variable. Implement the following edits: the first name cannot be null nor can it have a length less than one; the last name cannot be null nor can it have a length less than one. the ID must be greater than zero; Throw an InvalidArgumentException if any of the values are invalid.
Create a constructor that has parameters for the first name, last name, and ID. Call the set methods from the constructor to use these parameters to update the class variables so the edits only have to be coded once.
Create a toString method that displays the class name and the values of each of the three instance variables.
The following requirements apply to the HourlyEmployee class.
Make the HourlyEmployee class a subclass of Employee.
Create two private instance variables. Use a double to represent the hours the employee worked. Use a double to represent the hourly pay rate.
The HourlyEmployee class must implement the Payable interface. The computeAmountToPay method should return the hours worked times the hourly pay rate. You will need to import the edu.arizonastate.payable package.
Create get and set methods for each private variable. Implement the following edits: the hours worked must be greater than zero; the hourly pay rate must be greater than zero. Throw an InvalidArgumentException if any of the values are invalid.
Create a constructor that has parameters for the first name, last name, and ID, hours worked, and hourly rate. Call the superclass constructor passing the first name, last name, and ID. Call the set methods from the constructor to use the parameters to update the class variables so the edits only have to be coded once.
Create a toString method that displays the data from the superclass and the class name and the values of each of the two instance variables in the HourlyEmployee class.
The following requirements apply to the Manager class.
Make the Manager class a subclass of Employee.
Create one private instance variable for the annual salary. Use a double to represent the annual salary.
The Manager class must implement the Payable interface. The computeAmountToPay method should return the annual salary divided by 12. You will need to import the edu.arizonastate.payable package.
Create get and set methods for the private variable. Implement the following edits: the annual salary must greater than zero. Throw an InvalidArgumentException if the annual salary value is invalid.
Create a constructor that has parameters for the first name, last name, and ID, and annual salary. Call the superclass constructor passing the first name, last name, and ID. Call the set methods from the constructor to use the parameters to update the class variables so the edits only have to be coded once.
Create a toString method that displays the data from the superclass and the class name and the annual salary value from the Manager class.
The following requirements apply to the SalesManager class.
Make the SalesManager class a subclass of Manager.
Create two private instance variables. Use a double to represent the sales amount. Use a double to represent the commission rate.
The SalesManager class must implement the Payable interface. The computeAmountToPay method should return the pay value from the super class plus the sales amount times the commission rate. You will need to import the edu.arizonastate.payable package.
Create get and set methods for the private variables. Implement the following edits in the set methods. The sales amount must be greater than or equal to zero. Throw an InvalidArgumentException if the sales amount value is invalid. The commission rate must be between 0 and 1 inclusive. Throw an InvalidArgumentException if the commission rate is invalid.
Create a constructor that has parameters for the first name, last name, and ID, annual salary, sales amount, and commission rate. Call the superclass constructor passing the first name, last name, ID, and annual salary. Call the set methods from the constructor to use the parameters to update the class variables so the edits only have to be coded once.
Create a toString method that displays the data from the superclass, the class name, and the values of each of the two instance variables in the SalesManager class.
The following requirements apply to the ManagePayablesApplication class.
Import all the classes from the following packages: edu.arizonastate.payable, edu.arizonastate.employees, and edu.arizonastate.bill.
This class will have the main method. Create an ArrayList to store Payable objects.
Display a menu that has choices for: 1 - Add hourly employee; 2 - Add manager; 3 - Add sales manager; 4 - Add bill; 5 - List all payables; 6 - Exit. Display an error message and repeat the menu if some other value is entered. This will require you to catch the appropriate exception if letters instead of numbers are entered.
When the user chooses menu option 1, prompt for an employee first name, an employee last name, an employee ID, hours worked, and pay rate. Edit these values such that: the first name cannot have a length less than one or be null; the last name cannot have a length less than one or be null; the ID must be > 0; the hours worked must be > 0; the pay rate must be > 0. If any one piece of data is invalid prompt the user to re-enter that data. If all the data is valid, create a HourlyEmployee object and store it in the ArrayList. The HourlyEmployee class can throw an InvalidArgumentException so write code to catch that exception.
When the user chooses menu option 2, prompt for an employee first name, an employee last name, an employee ID, and annual salary. Edit these values such that: the first name cannot have a length less than one or be null; the last name cannot have a length less than one or be null; the ID must be > 0; the annual salary must be > 0. If any one piece of data is invalid, prompt the user to re-enter that data. If all the data is valid, create a Manager object and store it in the ArrayList. The Manager class can throw an InvalidArgumentException so write code to catch that exception.
When the user chooses menu option 3, prompt for an employee first name, an employee last name, an employee ID, annual salary, sales amount, and commission rate. Edit these values such that: the first name cannot have a length less than one or be null; the last name cannot have a length less than one or be null; the ID must be > 0; the annual salary must be > 0; the sales amount must be > 0; the commission rate must be >= 0 and <= 1. If any one piece of data is invalid prompt the user to re-enter that data. If all the data is valid, create a SalesManager object and store it in the ArrayList. The SalesManager class can throw an InvalidArgumentException so write code to catch that exception.
When the user chooses menu option 4, prompt for an vendor name, an amount due, and a due date. Edit these values such that: the vendor name cannot have a length less than one or be null; the amount must be > 0; the due date must be valid. Catch the DateTimeParseException to determine if the date is invalid. If any one piece of data is invalid prompt the user to re-enter that data. If all the data is valid, create a Bill object and store it in the ArrayList. The Bill class can throw an InvalidArgumentException so write code to catch that exception.
When the user chooses menu option 5, list all the objects contained in the ArrayList by calling their toString method. In addition, display the amount to be paid by calling their computeAmountToPay method. Format the amount to be paid as currency.
Display a closing message and end the application when the user chooses option 6.
Do not add a throws clause to the main method. In other words, use try, catch for any exceptions that make arise in the ManagePaymentsApplication class.
Use a Scanner object for all user input. Use System.out for all user output.
Add a comment to each source code file for your name and the date.
Here is a summary of the packages required in this assignment and what classes or interfaces should be in each package.
- edu.arizonastate.payments: ManagePayablesApplication
- edu.arizonastate.payable: Payable
- edu.arizonastate.bill: Bill
- edu.arizonastate.employees: Employee, HourlyEmployee, Manage, SalesManager
- edu.arizonastate.exceptions: InvalidArgumentException
Explanation / Answer
Below are the required files:
Payable.java
package edu.arizonastate.payable;
public interface Payable {
public abstract double computeAmountToPay();
}
Employee.java
package edu.arizonastate.employees;
import edu.arizonastate.exceptions.InvalidArgumentException;
import edu.arizonastate.payable.Payable;
public abstract class Employee implements Payable {
private String firstName;
private String lastName;
private int id;
public Employee(String firstName, String lastName, int id) throws InvalidArgumentException {
super();
this.setFirstName(firstName);
this.setLastName(lastName);
this.setId(id);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) throws InvalidArgumentException {
if(firstName == null || firstName.length() < 1)
throw new InvalidArgumentException("First Name should be one or more characters long.");
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) throws InvalidArgumentException {
if(lastName == null || lastName.length() < 1)
throw new InvalidArgumentException("Last Name should be one or more characters long.");
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) throws InvalidArgumentException {
if(id <= 0)
throw new InvalidArgumentException("ID muat be greater than zero.");
this.id = id;
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", id=" + id + "]";
}
}
Bill.java
package edu.arizonastate.bill;
import java.time.DateTimeException;
import java.time.LocalDate;
import edu.arizonastate.exceptions.InvalidArgumentException;
import edu.arizonastate.payable.Payable;
public class Bill implements Payable{
private String vendorName;
private double owedAmount;
private LocalDate dueDate;
public Bill(String vendorName, double owedAmount, LocalDate dueDate) throws InvalidArgumentException {
super();
this.setVendorName(vendorName);
this.setOwedAmount(owedAmount);
this.setDueDate(dueDate);
}
@Override
public double computeAmountToPay() {
return owedAmount;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String vendorName) throws InvalidArgumentException {
if(vendorName == null || vendorName.length() < 1)
throw new InvalidArgumentException("Vendor Name must be more than 1 character long.");
this.vendorName = vendorName;
}
public double getOwedAmount() {
return owedAmount;
}
public void setOwedAmount(double owedAmount) throws InvalidArgumentException {
if(owedAmount <= 0)
throw new InvalidArgumentException("Amount owed should be more than zero");
this.owedAmount = owedAmount;
}
public LocalDate getDueDate() {
return dueDate;
}
public void setDueDate(LocalDate dueDate) throws InvalidArgumentException {
if(dueDate == null)
throw new InvalidArgumentException("Due Date shoud not be null");
this.dueDate = dueDate;
}
public void setDueDate(int year, int month, int day) throws DateTimeException{
dueDate = LocalDate.of(year, month, day);
}
@Override
public String toString() {
return "Bill [vendorName=" + vendorName + ", owedAmount=" + owedAmount + ", dueDate=" + dueDate + "]";
}
}
HourlyEmployee.java
package edu.arizonastate.employees;
import edu.arizonastate.exceptions.InvalidArgumentException;
import edu.arizonastate.payable.Payable;
public class HourlyEmployee extends Employee implements Payable{
private double hoursWorked;
private double hourlyRate;
public HourlyEmployee(String firstName, String lastName, int id, double hoursWorked, double hourlyRate)
throws InvalidArgumentException {
super(firstName, lastName, id);
this.setHoursWorked(hoursWorked);
this.setHourlyRate(hourlyRate);
}
public double getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(double hoursWorked) throws InvalidArgumentException {
if(hoursWorked <= 0)
throw new InvalidArgumentException("The hours worked must be greater than zero.");
this.hoursWorked = hoursWorked;
}
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) throws InvalidArgumentException {
if(hourlyRate <= 0)
throw new InvalidArgumentException("The hourly pay rate must be greater than zero");
this.hourlyRate = hourlyRate;
}
@Override
public double computeAmountToPay() {
return hoursWorked * hourlyRate;
}
@Override
public String toString() {
return "HourlyEmployee [hoursWorked=" + hoursWorked + ", hourlyRate=" + hourlyRate + ", FirstName()="
+ getFirstName() + ", LastName()=" + getLastName() + ", Id()=" + getId() + "]";
}
}
Manager.java
package edu.arizonastate.employees;
import edu.arizonastate.exceptions.InvalidArgumentException;
import edu.arizonastate.payable.Payable;
public class Manager extends Employee implements Payable {
private double annualSalary;
public Manager(String firstName, String lastName, int id, double annualSalary) throws InvalidArgumentException {
super(firstName, lastName, id);
this.setAnnualSalary(annualSalary);
}
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary) throws InvalidArgumentException {
if(annualSalary <= 0)
throw new InvalidArgumentException("The annual salary must greater than zero.");
this.annualSalary = annualSalary;
}
@Override
public double computeAmountToPay() {
return annualSalary / 12;
}
@Override
public String toString() {
return super.toString() + ", Manager [annualSalary=" + annualSalary + "]" ;
}
}
SalesManager.java
package edu.arizonastate.employees;
import edu.arizonastate.exceptions.InvalidArgumentException;
import edu.arizonastate.payable.Payable;
public class SalesManager extends Manager implements Payable{
private double salesAmount;
private double commRate;
public SalesManager(String firstName, String lastName, int id, double annualSalary, double salesAmount,
double commRate) throws InvalidArgumentException {
super(firstName, lastName, id, annualSalary);
this.setSalesAmount(salesAmount);
this.setCommRate(commRate);
}
public double getSalesAmount() {
return salesAmount;
}
public void setSalesAmount(double salesAmount) throws InvalidArgumentException {
if(salesAmount < 0)
throw new InvalidArgumentException("The sales amount must be greater than or equal to zero.");
this.salesAmount = salesAmount;
}
public double getCommRate() {
return commRate;
}
public void setCommRate(double commRate) throws InvalidArgumentException {
if(commRate < 0 || commRate > 1)
throw new InvalidArgumentException("The commission rate must be between 0 and 1 inclusive.");
this.commRate = commRate;
}
@Override
public double computeAmountToPay() {
return super.computeAmountToPay() + (salesAmount * commRate);
}
@Override
public String toString() {
return super.toString() + ", SalesManager [salesAmount=" + salesAmount + ", commRate=" + commRate + "]";
}
}
InvalidArgumentException.java
package edu.arizonastate.exceptions;
public class InvalidArgumentException extends Exception {
public InvalidArgumentException(){
super("Invalid values sent to method");
}
public InvalidArgumentException(String msg){
super(msg);
}
}
ManagePayablesApplication.java
package edu.arizonastate.payments;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import edu.arizonastate.bill.Bill;
import edu.arizonastate.employees.HourlyEmployee;
import edu.arizonastate.employees.Manager;
import edu.arizonastate.employees.SalesManager;
import edu.arizonastate.exceptions.InvalidArgumentException;
import edu.arizonastate.payable.Payable;
public class ManagePayablesApplication {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Payable> payables = new ArrayList<Payable>();
String fname, lname;
int id;
double annSalary;
int choice;
do{
System.out.println(" 1 - Add hourly employee");
System.out.println(" 2 - Add manager");
System.out.println("3 - Add sales manager");
System.out.println("4 - Add bill");
System.out.println("5 - List all payables");
System.out.println("6 - Exit");
System.out.println("Enter your choice:");
choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter First Name:");
fname = scan.nextLine();
while(fname == null || fname.length() < 1){
System.out.println("First Name is invalid...Enter Again");
fname = scan.nextLine();
}
System.out.println("Enter Last Name:");
lname = scan.nextLine();
while(lname == null || lname.length() < 1){
System.out.println("Last Name is invalid...Enter Again");
lname = scan.nextLine();
}
System.out.println("Enter ID:");
id = scan.nextInt();
while(id <= 0){
System.out.println("ID is invalid...Enter Again");
id = scan.nextInt();
}
System.out.println("Enter Hours Worked:");
double hours = scan.nextDouble();
while(hours <= 0){
System.out.println("Hours Worked are invalid...Enter Again");
hours = scan.nextDouble();
}
System.out.println("Enter Hourly Pay Rate:");
double rate = scan.nextDouble();
while(rate <= 0){
System.out.println("Pay Rate is invalid...Enter Again");
rate = scan.nextDouble();
}
HourlyEmployee emp = null;
try {
emp = new HourlyEmployee(fname, lname, id, hours, rate);
} catch (InvalidArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
payables.add(emp);
break;
case 2:
System.out.println("Enter First Name:");
fname = scan.nextLine();
while(fname == null || fname.length() < 1){
System.out.println("First Name is invalid...Enter Again");
fname = scan.nextLine();
}
System.out.println("Enter Last Name:");
lname = scan.nextLine();
while(lname == null || lname.length() < 1){
System.out.println("Last Name is invalid...Enter Again");
lname = scan.nextLine();
}
System.out.println("Enter ID:");
id = scan.nextInt();
while(id <= 0){
System.out.println("ID is invalid...Enter Again");
id = scan.nextInt();
}
System.out.println("Enter Annual Salary:");
annSalary = scan.nextDouble();
while(annSalary <= 0){
System.out.println("Annual Salary is invalid...Enter Again");
annSalary = scan.nextDouble();
}
Manager manager = null;
try {
manager = new Manager(fname, lname, id, annSalary);
} catch (InvalidArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
payables.add(manager);
break;
case 3:
System.out.println("Enter First Name:");
fname = scan.nextLine();
while(fname == null || fname.length() < 1){
System.out.println("First Name is invalid...Enter Again");
fname = scan.nextLine();
}
System.out.println("Enter Last Name:");
lname = scan.nextLine();
while(lname == null || lname.length() < 1){
System.out.println("Last Name is invalid...Enter Again");
lname = scan.nextLine();
}
System.out.println("Enter ID:");
id = scan.nextInt();
while(id <= 0){
System.out.println("ID is invalid...Enter Again");
id = scan.nextInt();
}
System.out.println("Enter Annual Salary:");
annSalary = scan.nextDouble();
while(annSalary <= 0){
System.out.println("Annual Salary is invalid...Enter Again");
annSalary = scan.nextDouble();
}
System.out.println("Enter Sales Amount:");
double salesAmount = scan.nextDouble();
while(salesAmount <= 0){
System.out.println("Sales Amount is invalid...Enter Again");
salesAmount = scan.nextDouble();
}
System.out.println("Enter Commision Rate:");
double commRate = scan.nextDouble();
while(commRate < 0 || commRate > 1){
System.out.println("Commision rate is invalid...Enter Again");
commRate = scan.nextDouble();
}
SalesManager salesManager = null;
try {
salesManager = new SalesManager(fname, lname, id, annSalary, salesAmount, commRate);
} catch (InvalidArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
payables.add(salesManager);
break;
case 4:
System.out.println("Enter Vendor Name:");
String vendorName = scan.nextLine();
while(vendorName == null || vendorName.length() < 1){
System.out.println("Vendor Name is invalid...Enter Again");
vendorName = scan.nextLine();
}
System.out.println("Enter Amount Due:");
double amountDue = scan.nextDouble();
while(amountDue <= 0){
System.out.println("Amount Dueis invalid...Enter Again");
amountDue = scan.nextDouble();
}
System.out.println("Enter year, month and day for Due Date:");
int year = scan.nextInt();
int month = scan.nextInt();
int day = scan.nextInt();
LocalDate dueDate = null;
try{
dueDate = LocalDate.of(year, month, day);
}
catch(DateTimeParseException dpe){
dpe.printStackTrace();
}
Bill bill = null;
try {
bill = new Bill(vendorName, amountDue, dueDate);
} catch (InvalidArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
payables.add(bill);
break;
case 5:
for (Iterator iterator = payables.iterator(); iterator.hasNext();) {
Payable payable = (Payable) iterator.next();
System.out.println(payable.toString() + " , Amount to be paid is: $" + payable.computeAmountToPay());
}
break;
case 6:
System.out.println("GoodBye..");
System.exit(0);
default:
System.out.println("Invalid Choice...try again.");
break;
}
}while(choice != 6);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.