DataSetEmployee Can you please help me the JAVA program? Here is the requirement
ID: 3785691 • Letter: D
Question
DataSetEmployee
Can you please help me the JAVA program? Here is the requirement.
Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value?
Make a package com.acme.midmanager. This package should contain the Manager class.
Make a package com.acme.personnel. This package should contain the DataSetEmployee class.
Make a package com.acme.topmanagement. This package should contain the Executive class.
Make a package com.acme.workers. This package should contain the Employee class.
Make a package com.acme.tester. This package should contain your test driver. Your test driver can be either a class with a main method, or JUnit tests.
You'll have to add package and import statements. Your IDE can help with that. All instance variables should be private. You should have no static data (unless you need a constant value for some reason--that would be static final).
If you use a main method to test your code and specify all of an object's data on the constructor, one simple testcase would be the following:
Employee dilbert = new Employee("sophie", 32);
System.out.println(empStore);
In this example, min would be dilbert, max would be dogbert.
--------------here is Book.java code-------------------
public class Book {
private String author;
private String title;
private int pages;
public Book(String auth, String titl, int pag) {
author = auth;
title = titl;
pages = pag;
}
public int getPages() {
return pages;
}
@Override
public String toString() {
return "Book [author=" + author + ", title=" + title + ", pages=" + pages + "] ";
}
// this is a really poor way to compare Book objects, but it will work for us
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (pages != other.pages)
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}
------------------------------------------------------------------------------------------------------------------------------
DataSetBook.java
import java.util.ArrayList;
import java.util.Arrays;
public class DataSetBook extends ArrayList <Book>{
public DataSetBook(){
}
public boolean add(Book objToAdd){
return super.add(objToAdd);
}
public int size(){
return super.size();
}
public Book getMin(){
int size = super.size();
if(size == 0){
return null;
}
else{
Book minBook = super.get(0);
for(int i=0; i<size; i++){
Book b = super.get(i);
if(minBook.getPages() > b.getPages()){
minBook = super.get(i);
}
}
return minBook;
}
}
public Book getMax(){
int size = super.size();
if(size == 0){
return null;
}
else{
Book maxBook = super.get(0);
for(int i=0; i<size; i++){
Book b = super.get(i);
if(maxBook.getPages() < b.getPages()){
maxBook = super.get(i);
}
}
return maxBook;
}
}
public java.lang.String toString(){
return "Number of Books: "+super.size()+" "+"Minimum Book is "+getMin().toString()+" "+"Maximum Book is "+getMax().toString()+" "+"THe content of entire store "+Arrays.toString(super.toArray());
}
}
----------------
Employee.java
public class Employee{
String name;
double salary;
Employee(){
}
Employee(String name, double salary){
this.name = name;
this.salary = salary;
}
public String toString(){
return "Employee Name : " + name + " " + "Employee Salary :" + salary ;
}
}
===========================================================================
Manager.java
public class Manager extends Employee{
String department;
Manager(){
}
Manager(String name, double salary, String department){
super(name, salary);
this.department = department;
}
public String toString(){
return "Manager Name : " + name + " " + "Manager Salary :" + salary +
" " + "Manager department :" + department ;
}
}
====================================================================================
Executive.java
public class Executive extends Manager{
double bonus;
Executive(){
}
Executive(String name, double salary, String department, double bonus){
super(name, salary,department);
this.bonus = bonus;
}
public String toString(){
return "Executive Name : " + name + " " + "Executive Salary :" +
(salary+salary*bonus) + " " + "Executive department :" + department ;
}
}
====================================================================================
Test.java
public class Test{
public static void main(String args[]){
System.out.println("Employee Details:======");
Employee dilbert = new Employee("Dilbert",2000);
System.out.println(dilbert.toString());
System.out.println("Manager Details:========");
Employee pointyHairedBoss = new Manager("Harry",2000,"Innovation");
System.out.println(pointyHairedBoss.toString());
System.out.println("Executive Details:=======");
Employee clueless = new Executive("John",2000,"Innovation",0.2);
System.out.println(clueless.toString());
}
}
=================================================================================
Explanation / Answer
Below are the Employee, Manager, Executive, DataSetEmployee, and Test classes along with the output.
You may change the test class in order to check the program for different inputs.
Also, I have modified you Employee, Executive and Manager classses as well, so that they meet the project instructions; changes I have done in them:
1. Made all instance variables private.
2. Added getters.
3. Made constructors public.
You may post a comment in case of any other doubts in the program.
Employee.java:
package com.acme.workers;
public class Employee{
private String name;
private double salary;
public Employee(){
}
public Employee(String name, double salary){
this.name = name;
this.salary = salary;
}
public double getSalary(){
return this.salary;
}
public String getName(){
return this.name;
}
public String toString(){
return "Employee Name : " + name + " " + "Employee Salary :" + salary ;
}
}
Manager.java:
package com.acme.midmanager;
import com.acme.workers.Employee;
public class Manager extends Employee{
private String department;
public String getDepartment() {
return department;
}
public Manager(){
}
public Manager(String name, double salary, String department){
super(name, salary);
this.department = department;
}
public String toString(){
return "Manager Name : " + getName() + " " + "Manager Salary :" + getSalary() +
" " + "Manager department :" + department ;
}
}
Executive.java:
package com.acme.topmanagement;
import com.acme.midmanager.Manager;
public class Executive extends Manager{
private double bonus;
public Executive(){
}
public Executive(String name, double salary, String department, double bonus){
super(name, salary,department);
this.bonus = bonus;
}
public double getSalary(){
return (super.getSalary() + (super.getSalary()*bonus)) ;
}
public String toString(){
return "Executive Name : " + getName() + " " + "Executive Salary :" + getSalary() + " " + "Executive department :" + getDepartment() ;
}
}
DataSetEmployee.java:
package com.acme.personnel;
import java.util.ArrayList;
import java.util.Arrays;
import com.acme.workers.Employee;
public class DataSetEmployee extends ArrayList<Employee> {
public DataSetEmployee() {
}
public boolean add(Employee objToAdd) {
return super.add(objToAdd);
}
public int size() {
return super.size();
}
public Employee getMin() {
int size = super.size();
if (size == 0) {
return null;
} else {
Employee minEmployee = super.get(0);
for (int i = 0; i < size; i++) {
Employee e = super.get(i);
if (minEmployee.getSalary() > e.getSalary()) {
minEmployee = super.get(i);
}
}
return minEmployee;
}
}
public Employee getMax() {
int size = super.size();
if (size == 0) {
return null;
} else {
Employee maxBook = super.get(0);
for (int i = 0; i < size; i++) {
Employee e = super.get(i);
if (maxBook.getSalary() < e.getSalary()) {
maxBook = super.get(i);
}
}
return maxBook;
}
}
public java.lang.String toString() {
return "Number of Employees: " + super.size() + " " + "Employee with Minimum salary is "
+ getMin().toString() + " " + "Employee with maximum salary is "
+ getMax().toString() + " " + "All employees: "
+ Arrays.toString(super.toArray());
}
}
Test.java:
package com.acme.tester;
import com.acme.midmanager.Manager;
import com.acme.personnel.DataSetEmployee;
import com.acme.topmanagement.Executive;
import com.acme.workers.Employee;
public abstract class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee dilbert = new Employee("sophie", 32);
Employee pointyHair = new Manager("sally", 45, "dogfood");
Employee dogbert = new Executive("jack", 43, "selfEnrichment", .1);
DataSetEmployee empStore = new DataSetEmployee();
empStore.add(dilbert);
empStore.add(pointyHair);
empStore.add(dogbert);
System.out.println(empStore);
}
}
Output I got:
Number of Employees: 3
Employee with Minimum salary is Employee Name : sophie
Employee Salary :32.0
Employee with maximum salary is Executive Name : jack
Executive Salary :47.3
Executive department :selfEnrichment
All employees: [Employee Name : sophie
Employee Salary :32.0, Manager Name : sally
Manager Salary :45.0
Manager department :dogfood, Executive Name : jack
Executive Salary :47.3
Executive department :selfEnrichment]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.