CompanyList.java package unl.cse.company; import java.util.ArrayList; public cla
ID: 3586242 • Letter: C
Question
CompanyList.java
package unl.cse.company;
import java.util.ArrayList;
public class CompanyList<T> {
public CompanyList() {
tarray = new ArrayList<T>();
}
private ArrayList<T> tarray;
public void addToArray(T t) {
tarray.add(t);
}
//TODO: Add a toString method for employees for displaying (optional)
public void print() {
System.out.println(tarray.toString());
}
}
Employee.java
package unl.cse.company;
public abstract class Employee implements Serializable,CompanyPortfolio{
int id;
String firstName;
String lastName;
String title;
/* TODO - Do you need any more fields here ? (e.g netPay, grossPay etc)
*
*/
/* TODO Add other methods (e.g getName(), computeSalary())
* Which of them do you think should be abstract ?
*/
/* TODO What other subclasses do you need ? (e.g Hourly, Temporary etc)
* What methods can be implemented there ? (e.g hourlyPay/overtimePay?, getpay? getName? etc)
* Which of them can be abstract here ?*/
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
CompanyPortfolio.java
package unl.cse.company;
public interface CompanyPortfolio {
public void computeProductivity();
}
GenricsDemo.java
package unl.cse.company;
public class GenericsDemo {
public static void main(String args[]) {
// TODO: Make 2 different types of employees (change as appropriate to your design)
Employee e1 = new Permanent("","");
Permanent e2 = new Permanent("","");
//Temporary e3 = new Temporary("E","F");
// TODO: Add them to non-generic company c1 and generic company c2.
CompanyList c1 = new CompanyList();
//c1.addToArray(e1);
c1.print();
CompanyList<Permanent> c2 = new CompanyList<Permanent>();
c2.print();
// TODO: Create a product and try adding to c1 and c2
}
}
PayrollDemo.java
package unl.cse.company;
public class PayrollDemo {
public static void main(String[] args) {
//TODO - Create two different employees of each type , update their details
// and print their names and salaries(change as appropriate to your design)
Employee employee1 = new Temporary("John","Smith");
Employee employee2 = new Permanent("Richard","Feymann");
printSalary(employee1);
printSalary(employee2);
}
public static void printSalary(Employee e) {
System.out.println("Salary of " + e.getName() + " is: "
+ e.getPay());
}
}
PortfolioDemo.java
package unl.cse.company;
import java.io.File;
import java.io.IOException;
public class PortfolioDemo {
public static void main(String[] args) {
//TODO: Create employees and products with details, export to JSON
// and compute their productivity (change as appropriate to your design)
Serializable serializableEmployee = new Temporary("Smith","John");
Serializable serializableProduct = new Product();
try {
serializableEmployee.serializeToJSON(serializableEmployee, new File("data/Employee.json"));
serializableProduct.serializeToJSON(serializableProduct, new File("data/Product.json"));
} catch (IOException e) {
e.printStackTrace();
}
CompanyPortfolio customerPortfolio = (CompanyPortfolio) serializableEmployee;
CompanyPortfolio productPortfolio = (CompanyPortfolio) serializableProduct;
customerPortfolio.computeProductivity();
productPortfolio.computeProductivity();
}
}
Serializable.java
package unl.cse.company;
import java.io.File;
import java.io.IOException;
public interface Serializable {
public void serializeToJSON(Serializable jsonSerializable, File file) throws IOException;
}
Explanation / Answer
1. Complete the program and demonstrate your design and program to lab instructor.
package employee;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.Gson;
import unl.cse.company.CompanyPortfolio;
import unl.cse.company.Serializable;
/*
* This class is abstract Employee class which contains properties and behavior which
* are common to all type of employees.
* In future if we need to implement new type of employees then it will help a lot in reusing a lot of code.
*/
public abstract class Employee implements Serializable, CompanyPortfolio {
private int id;
private String firstName;
private String lastName;
private String title;
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
/**
* This method return the full name.
*
* @return - Full name of employee.
*/
public String getName() {
if (this.getFirstName() != null) {
if (this.getLastName() != null) {
return this.getFirstName() + " " + this.getLastName();
} else {
return this.getFirstName();
}
} else if (this.getLastName() != null) {
return this.getLastName();
}
return null;
}
/*
* This method computes one week salary of employee. Implementation depends
* upon employee type.
*/
public abstract double computeSalary();
/**
* This method serialize Json of the object into the file.
*/
@Override
public void serializeToJSON(Serializable jsonSerializable, File file) throws IOException {
Gson gson = new Gson();
gson.toJson(this, new FileWriter(file));
}
}
package employee;
/**
* This class represents Permanent type of employees.
* @author aniket
*
*/
public class PermanentEmployee extends Employee {
private double baseSalary;
private double overtimeRatePerHour;
private int overtimeHours;
public PermanentEmployee(String firstName, String lastName) {
super(firstName, lastName);
}
public PermanentEmployee(String firstName, String lastName, double baseSalary, double overtimeRatePerHour,
int overtimeHours) {
super(firstName, lastName);
this.baseSalary = baseSalary;
this.overtimeRatePerHour = overtimeRatePerHour;
this.overtimeHours = overtimeHours;
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getOvertimeRatePerHour() {
return overtimeRatePerHour;
}
public void setOvertimeRatePerHour(double overtimeRatePerHour) {
this.overtimeRatePerHour = overtimeRatePerHour;
}
public int getOvertimeHours() {
return overtimeHours;
}
public void setOvertimeHours(int overtimeHours) {
this.overtimeHours = overtimeHours;
}
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return this.getBaseSalary() + (this.getOvertimeRatePerHour() * this.getOvertimeHours());
}
@Override
public void computeProductivity() {
// TODO Auto-generated method stub
double productivity = this.computeSalary() / this.getOvertimeHours();
System.out.println("Productivity:- " + productivity);
}
@Override
public String toString() {
return "PermanentEmployee [baseSalary=" + baseSalary + ", overtimeRatePerHour=" + overtimeRatePerHour
+ ", overtimeHours=" + overtimeHours + ", getId()=" + getId() + ", getFirstName()=" + getFirstName()
+ ", getLastName()=" + getLastName() + ", getTitle()=" + getTitle() + "]";
}
}
package employee;
/**
*
* This class represents Temporary type of employees.
*
*/
public class TemporaryEmployee extends Employee {
private double perHourRate;
private int workingHours;
public TemporaryEmployee(String firstName, String lastName) {
super(firstName, lastName);
// TODO Auto-generated constructor stub
}
public TemporaryEmployee(String firstName, String lastName, double perHourRate, int workingHours) {
super(firstName, lastName);
this.perHourRate = perHourRate;
this.workingHours = workingHours;
// TODO Auto-generated constructor stub
}
public double getPerHourRate() {
return perHourRate;
}
public void setPerHourRate(double perHourRate) {
this.perHourRate = perHourRate;
}
public int getWorkingHours() {
return workingHours;
}
public void setWorkingHours(int workingHours) {
this.workingHours = workingHours;
}
@Override
public double computeSalary() {
// TODO Auto-generated method stub
return this.getPerHourRate() * this.getWorkingHours();
}
@Override
public void computeProductivity() {
// TODO Auto-generated method stub
double productivity = this.computeSalary() / this.getWorkingHours();
System.out.println("Productivity:- " + productivity);
}
@Override
public String toString() {
return "TemporaryEmployee [perHourRate=" + perHourRate + ", workingHours=" + workingHours + ", getId()="
+ getId() + ", getFirstName()=" + getFirstName() + ", getLastName()=" + getLastName() + ", getTitle()="
+ getTitle() + "]";
}
}
package product;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.Gson;
import unl.cse.company.CompanyPortfolio;
import unl.cse.company.Serializable;
/**
* This class represents all properties and behaviors for all types of product.
*
*/
public class Product implements Serializable, CompanyPortfolio {
private String name;
private String type;
private double cost;
private long count;
public Product(String name, String type, double cost, long count) {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
@Override
public void computeProductivity() {
// TODO Auto-generated method stub
double productivity = this.getCost() * (this.getCount()) / (7 * 24);
System.out.println("Productivity:- " + productivity);
}
@Override
public void serializeToJSON(Serializable jsonSerializable, File file) throws IOException {
// TODO Auto-generated method stub
Gson gson = new Gson();
gson.toJson(this, new FileWriter(file));
}
@Override
public String toString() {
return "Product [name=" + name + ", type=" + type + ", cost=" + cost + ", count=" + count + "]";
}
}
package service;
import employee.Employee;
import employee.PermanentEmployee;
import employee.TemporaryEmployee;
public class PayrollDemo {
public static void main(String[] args) {
Employee employee1 = new TemporaryEmployee("John", "Smith", 10, 10);
Employee employee2 = new PermanentEmployee("Richard", "Feymann", 100, 10, 5);
printSalary(employee1);
printSalary(employee2);
}
public static void printSalary(Employee e) {
System.out.println("Salary of " + e.getName() + " is: " + e.computeSalary());
}
}
package unl.cse.company;
import java.io.File;
import java.io.IOException;
public interface Serializable {
public void serializeToJSON(Serializable jsonSerializable, File file) throws IOException;
}
package unl.cse.company;
public interface CompanyPortfolio {
public void computeProductivity();
}
package service;
import java.io.File;
import java.io.IOException;
import employee.TemporaryEmployee;
import product.Product;
import unl.cse.company.CompanyPortfolio;
import unl.cse.company.Serializable;
public class PortfolioDemo {
public static void main(String[] args) {
// TODO: Create employees and products with details, export to JSON
// and compute their productivity (change as appropriate to your design)
Serializable serializableEmployee = new TemporaryEmployee("Smith", "John", 10, 100);
Serializable serializableProduct = new Product("Tally", "Software", 100, 1000);
try {
serializableEmployee.serializeToJSON(serializableEmployee, new File("data/Employee.json"));
serializableProduct.serializeToJSON(serializableProduct, new File("data/Product.json"));
} catch (IOException e) {
e.printStackTrace();
}
CompanyPortfolio customerPortfolio = (CompanyPortfolio) serializableEmployee;
CompanyPortfolio productPortfolio = (CompanyPortfolio) serializableProduct;
customerPortfolio.computeProductivity();
productPortfolio.computeProductivity();
}
}
package unl.cse.company;
import java.util.ArrayList;
public class CompanyList<T> {
private ArrayList<T> tarray;
public CompanyList() {
tarray = new ArrayList<T>();
}
public void addToArray(T t) {
tarray.add(t);
}
// TODO: Add a toString method for employees for displaying (optional)
public void print() {
System.out.println(tarray.toString());
}
}
2.
Abstract Class:
3.
This activity has already covered in above code.
4.
Interface:-
Abstract class:-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.