departmentstore: package departmentstorepkg; import java.util.ArrayList; public
ID: 3881511 • Letter: D
Question
departmentstore:
package departmentstorepkg;
import java.util.ArrayList;
public class DepartmentStore {
private static final int DEFAULT_SIZE = 10;
private StaffMember [] myEmployees;
private int myNumberEmployees;
private String myFileName;
private StaffMember[] employee;
public DepartmentStore (String filename){
myFileName = filename;
myEmployees = employee;
}
public String toString(){
return this.getClass().toString() + ": " + myFileName;
}
public void addEmployee(Employee emp){
}
/**
* prints out all the employees in the array list held in this class
*/
public void print(){
for(int i = 0; i<myNumberEmployees;i++){
System.out.println(this.myEmployees[i]);
System.out.println("");
}
}
/**
*
* prints only instances of Employee objects
*
*/
public void printEmployees(){
//instanceof (think of it as an operator) comparing what kind of obj you are
//finish the work on your own!!
for (int i = 0; i < myNumberEmployees; i ++){
StaffMember sm = myEmployees[i];
if(sm instanceof Hourly ){
}
else if (sm instanceof Executive){
}
else{
System.out.println(sm.toString());
}
}
}
/**
* prints only instances of Executive objects
*
*/
public void printExecutives(){
//instanceof (think of it as an operator) comparing what kind of obj you are
//finish the work on your own!!
for(int i = 0; i< myNumberEmployees;i++){
StaffMember sm = myEmployees[i];
if(sm instanceof Executive ){
System.out.println(sm.toString());
}
}
}
/**
*
* prints only instances of Hourly objects
*/
public void printHourly(){
//instanceof (think of it as an operator) comparing what kind of obj you are
//finish the work on your own!!
for (int i = 0; i < myNumberEmployees; i ++){
StaffMember sm = myEmployees[i];
if(sm instanceof Hourly){
System.out.println(sm.toString());
}
}
}
public Employee findEmployee(String ssn){
for (int i = 0; i < myNumberEmployees; i ++){
StaffMember sm = (Employee)myEmployees[i];
if(sm.getSocialSecurityNumber().equals(ssn)){
return (Employee) sm ;
}
}
return null;
}
public void removeEmployee(String ssn){
for(int i = 0; i< myNumberEmployees;i++){
Employee sm = (Employee) myEmployees[i];
if(sm.socialSecurityNumber.compareTo(ssn) == 0){
}
}
}
public void removeEmployee(Employee emp){
for(int i = 0; i< myNumberEmployees;i++){
Employee sm = (Employee) myEmployees[i];
if(sm.compareTo(emp) == 0){
}
}
}
public boolean updateHours(String ssn, int hours){
boolean r = false;
if(findEmployee(ssn) instanceof Hourly)
{
((Hourly)findEmployee(ssn) ).addHours(hours);
r = true;
}
if (r){
return true;
}
else{
return false;
}
}
}
departmenstoretest:
package departmentstorepkg;
public class DepartmentStoreTest {
public static void main(String[] args) {
// StaffMemberIO empIn = new StaffMemberIO ("./employees.txt");
// StaffMember sm = empIn.getNext();
// while (sm!=null) {
//
// System.out.println(sm);
//
// if(sm instanceof Executive)
// {
// System.out.println(" Executive");
// System.out.println(sm);
// }
// else if(sm instanceof Hourly)
// {
// System.out.println(" Hourly");
// System.out.println(sm);
// }
// else if(sm instanceof Employee)
// {
// System.out.println(" Employee");
// System.out.println(sm);
// }
// System.out.println();
// sm = empIn.getNext();
//
// }
{
System.out.println();
DepartmentStore store = new DepartmentStore("Allied Service Department Store");
System.out.println();
StaffMemberIO empIn = new StaffMemberIO ("employees.txt");
//while another employee to read
while(empIn.hasNext()){
store.addEmployee(empIn.next());
}
System.out.println("My store: " + store.toString());
System.out.println("Here are the people in the store: ");
System.out.println("Testing print method in department store class");
store.print();
System.out.println();
System.out.println("Testing printEmployees method in departmentStore class");
store.printEmployees();
System.out.println();
System.out.println("Testing printExecutives method in departmentStore class");
store.printExecutives();
System.out.println();
System.out.println("Testing printHourly method in departmentStore class");
store.printHourly();
System.out.println();
System.out.println("Testing findEmployee method in departmentStore class");
System.out.println(store.findEmployee("222-22-2222"));
System.out.println(store.findEmployee("123-45-6789"));
System.out.println();
System.out.println("Testing removeEmployee method in departmentStore class");
System.out.println("using both removeEmployee(String ssn) and removeEmployee(Employee emp)");
Employee temp1 = store.findEmployee("123-45-6789");
store.removeEmployee("123-45-6789");
System.out.println(store.findEmployee("123-45-6789"));
// using removeEmployee(Employee emp)
Employee temp2 = store.findEmployee("323-23-5666");
store.removeEmployee(store.findEmployee("323-23-5666"));
System.out.println(store.findEmployee("323-23-5666"));
System.out.println("adding employees back in");
store.addEmployee(temp1);
store.addEmployee(temp2);
System.out.println(store.findEmployee("123-45-6789"));
System.out.println(store.findEmployee("323-23-5666"));
System.out.println();
System.out.println("Testing return output for updateHours method in departmentStore class");
System.out.println(store.updateHours("212-33-6978", 0));
System.out.println(store.updateHours("222-22-2222", 20));
System.out.println();
System.out.println("Testing hourly hours update for updateHours method in departmentStore class");
System.out.println(store.updateHours("212-33-6978", 20));
System.out.println(store.updateHours("658-45-9234", 40));
store.printHourly();
System.out.println();
System.out.println("Testing awardBonus method in Executive class");
((Executive)store.findEmployee("123-45-6789")).awardBonus(1000.50);
((Executive)store.findEmployee("623-34-2399")).awardBonus(1240.50);
((Executive)store.findEmployee("345-67-8934")).awardBonus(1220.50);
store.printExecutives();
}
}
}
employee:
package departmentstorepkg;
public class DepartmentStoreTest {
public static void main(String[] args) {
// StaffMemberIO empIn = new StaffMemberIO ("./employees.txt");
// StaffMember sm = empIn.getNext();
// while (sm!=null) {
//
// System.out.println(sm);
//
// if(sm instanceof Executive)
// {
// System.out.println(" Executive");
// System.out.println(sm);
// }
// else if(sm instanceof Hourly)
// {
// System.out.println(" Hourly");
// System.out.println(sm);
// }
// else if(sm instanceof Employee)
// {
// System.out.println(" Employee");
// System.out.println(sm);
// }
// System.out.println();
// sm = empIn.getNext();
//
// }
{
System.out.println();
DepartmentStore store = new DepartmentStore("Allied Service Department Store");
System.out.println();
StaffMemberIO empIn = new StaffMemberIO ("employees.txt");
//while another employee to read
while(empIn.hasNext()){
store.addEmployee(empIn.next());
}
System.out.println("My store: " + store.toString());
System.out.println("Here are the people in the store: ");
System.out.println("Testing print method in department store class");
store.print();
System.out.println();
System.out.println("Testing printEmployees method in departmentStore class");
store.printEmployees();
System.out.println();
System.out.println("Testing printExecutives method in departmentStore class");
store.printExecutives();
System.out.println();
System.out.println("Testing printHourly method in departmentStore class");
store.printHourly();
System.out.println();
System.out.println("Testing findEmployee method in departmentStore class");
System.out.println(store.findEmployee("222-22-2222"));
System.out.println(store.findEmployee("123-45-6789"));
System.out.println();
System.out.println("Testing removeEmployee method in departmentStore class");
System.out.println("using both removeEmployee(String ssn) and removeEmployee(Employee emp)");
Employee temp1 = store.findEmployee("123-45-6789");
store.removeEmployee("123-45-6789");
System.out.println(store.findEmployee("123-45-6789"));
// using removeEmployee(Employee emp)
Employee temp2 = store.findEmployee("323-23-5666");
store.removeEmployee(store.findEmployee("323-23-5666"));
System.out.println(store.findEmployee("323-23-5666"));
System.out.println("adding employees back in");
store.addEmployee(temp1);
store.addEmployee(temp2);
System.out.println(store.findEmployee("123-45-6789"));
System.out.println(store.findEmployee("323-23-5666"));
System.out.println();
System.out.println("Testing return output for updateHours method in departmentStore class");
System.out.println(store.updateHours("212-33-6978", 0));
System.out.println(store.updateHours("222-22-2222", 20));
System.out.println();
System.out.println("Testing hourly hours update for updateHours method in departmentStore class");
System.out.println(store.updateHours("212-33-6978", 20));
System.out.println(store.updateHours("658-45-9234", 40));
store.printHourly();
System.out.println();
System.out.println("Testing awardBonus method in Executive class");
((Executive)store.findEmployee("123-45-6789")).awardBonus(1000.50);
((Executive)store.findEmployee("623-34-2399")).awardBonus(1240.50);
((Executive)store.findEmployee("345-67-8934")).awardBonus(1220.50);
store.printExecutives();
}
}
}
executive:
package departmentstorepkg;
//********************************************************************
// Executive.java Author: Lewis and Loftus
//
// Represents an executive staff member, who can earn a bonus.
//********************************************************************
class Executive extends Employee{
private double bonus;
// Sets up an executive with the specified information.
public Executive(String name, String address, String phone,
String socialSecurityNumber, double payRate) {
super(name, address, phone, socialSecurityNumber, payRate);
bonus = 0; // bonus has yet to be awarded
}
// Awards the specified bonus to this executive.
public void awardBonus(double execBonus) {
bonus = execBonus;
}
// Computes and returns the pay for an executive, which is the
// regular employee payment plus a one-time bonus.
public double pay() {
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
public String toString() {
String result = super.toString();
result += " Bonus: " + bonus;
return result;
}
}
staffmember:
package departmentstorepkg;
//********************************************************************
// StaffMember.java Author: Lewis and Loftus
//
// Represents a generic staff member.
//********************************************************************
abstract class StaffMember
{
/**
* staff member name
*/
protected String name;
/**
* staff member address
*/
protected String address;
/**
* staff member telephone number
*/
protected String phone;
/**
* initializes a newly-created StaffMember object
*/
public StaffMember(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
/**
* returns a String representation of this object
*/
public String toString() {
String result = getClass().getName()+ " Name: " + name + " ";
result += "Address: " + address + " ";
result += "Phone: " + phone;
return result;
}
/**
* abstract method representing the rate of pay for this staff member
*/
public abstract double pay();
public Object getSocialSecurityNumber() {
// TODO Auto-generated method stub
return null;
}
}
hourly:
package departmentstorepkg;
//********************************************************************
// Hourly.java Author: Lewis and Loftus
//
// Represents an employee that gets paid by the hour.
//********************************************************************
class Hourly extends Employee
{
private int hoursWorked;
// Sets up this hourly employee using the specified information.
public Hourly(String name, String address, String phone,
String socialSecurityNumber, double payRate) {
super(name, address, phone, socialSecurityNumber, payRate);
hoursWorked = 0;
}
// Adds the specified number of hours to this employee's
// accumulated hours.
public void addHours(int moreHours) {
hoursWorked += moreHours;
}
// Computes and returns the pay for this hourly employee.
public double pay() {
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
// Returns information about this hourly employee as a string.
public String toString() {
String result = super.toString();
result += " Current hours: " + hoursWorked;
return result;
}
}
staffmemberIO:
package departmentstorepkg;
import java.io.*;
import java.util.*;
public class StaffMemberIO
{
private String fileName;
private BufferedReader infile;
private Employee nextOne;
public StaffMemberIO(String filename) {
fileName = filename;
openFile();
nextOne = getNext();
}
/**
* returns true if there is another employee to be read from the file
*/
public boolean hasNext() {
return nextOne != null;
}
/**
* returns an Employee (or one of its subclasses) constructed from the
* information about the next employee in the file
*/
public Employee next() {
if (nextOne == null)
throw new NoSuchElementException("No more staff members");
Employee temp = nextOne;
nextOne = getNext();
return temp;
}
/**
* returns a String representation of EmployeeIO
*/
public String toString() {
return "[" + fileName + "]";
}
Employee getNext() {
if (infile == null)
return null;
try {
String typeString = infile.readLine().trim();
char type = typeString.charAt(0);
String name = infile.readLine().trim();
String address = infile.readLine();
String phone = infile.readLine().trim();
String socialSecurity = infile.readLine().trim();
double pay = Double.valueOf(infile.readLine().trim()).doubleValue();
switch (type) {
case 'e':
case 'E':
return new Employee(name, address, phone, socialSecurity, pay);
case 'x':
case 'X':
return new Executive(name, address, phone, socialSecurity, pay);
case 'h':
case 'H':
return new Hourly(name, address, phone, socialSecurity, pay);
}
} catch (Exception e) { }
closeFile();
return null;
}
private void closeFile() {
if (infile == null)
return;
try {
infile.close();
} catch (IOException e) {}
}
private void openFile() {
infile = null;
try {
FileReader fr = new FileReader(fileName);
infile = new BufferedReader(fr);
} catch (IOException e) {System.err.println("Didn't open " + fileName);
}
}
}
I'm trying to run it but it's not working main is departmentstore,, JUST UPDATES IT AND INCLUDED ALL THE CLASSES!!
Explanation / Answer
I have remove the errors in your code :
DepartmentStore.java
import java.util.ArrayList;
public class DepartmentStore {
private static final int DEFAULT_SIZE = 10;
private StaffMember[] myEmployees;
private int myNumberEmployees;
private String myFileName;
private StaffMember[] employee;
public DepartmentStore (String filename){
myFileName = filename;
myEmployees = employee;
}
public String toString(){
return this.getClass().toString() + ": " + myFileName;
}
public void addEmployee(Employee emp){
}
/**
* prints out all the employees in the array list held in this class
*/
public void print(){
for(int i = 0; i<myNumberEmployees;i++){
System.out.println(this.myEmployees[i]);
System.out.println("");
}
}
/**
*
* prints only instances of Employee objects
*
*/
public void printEmployees(){
//instanceof (think of it as an operator) comparing what kind of obj you are
//finish the work on your own!!
for (int i = 0; i < myNumberEmployees; i ++){
StaffMember sm = myEmployees[i];
if(sm instanceof Hourly ){
}
else if (sm instanceof Executive){
}
else{
System.out.println(sm.toString());
}
}
}
/**
* prints only instances of Executive objects
*
*/
public void printExecutives(){
//instanceof (think of it as an operator) comparing what kind of obj you are
//finish the work on your own!!
for(int i = 0; i< myNumberEmployees;i++){
StaffMember sm = myEmployees[i];
if(sm instanceof Executive ){
System.out.println(sm.toString());
}
}
}
/**
*
* prints only instances of Hourly objects
*/
public void printHourly(){
//instanceof (think of it as an operator) comparing what kind of obj you are
//finish the work on your own!!
for (int i = 0; i < myNumberEmployees; i ++){
StaffMember sm = myEmployees[i];
if(sm instanceof Hourly){
System.out.println(sm.toString());
}
}
}
public Employee findEmployee(String ssn){
for (int i = 0; i < myNumberEmployees; i ++){
Employee sm = (Employee)myEmployees[i];
if(sm.getSocialSecurityNumber().equals(ssn)){
return sm ;
}
}
return null;
}
public void removeEmployee(String ssn){
for(int i = 0; i< myNumberEmployees;i++){
Employee sm = (Employee) myEmployees[i];
if(sm.getSocialSecurityNumber().equals(ssn)){
}
}
}
public void removeEmployee(Employee emp){
for(int i = 0; i< myNumberEmployees;i++){
Employee sm = (Employee) myEmployees[i];
if(sm.equals(emp)){
}
}
}
public boolean updateHours(String ssn, int hours){
boolean r = false;
if(findEmployee(ssn) instanceof Hourly)
{
((Hourly)findEmployee(ssn) ).addHours(hours);
r = true;
}
if (r){
return true;
}
else{
return false;
}
}
}
DepartmentStoreTest.java
public class DepartmentStoreTest {
public static void main(String[] args) {
System.out.println();
DepartmentStore store = new DepartmentStore("Allied Service Department Store");
System.out.println();
StaffMemberIO empIn = new StaffMemberIO("employees.txt");
StaffMember sm = empIn.getNext();
//while another employee to read
while (sm!=null) {
//System.out.println(sm);
if(sm instanceof Executive)
{
System.out.println("Executive");
System.out.println(sm);
}
else if(sm instanceof Hourly)
{
System.out.println("Hourly");
System.out.println(sm);
}
else if(sm instanceof Employee)
{
System.out.println("Employee");
System.out.println(sm);
}
System.out.println();
sm = empIn.getNext();
}
System.out.println("My store: " + store.toString());
System.out.println("Here are the people in the store: ");
System.out.println("Testing print method in department store class");
store.print();
System.out.println();
System.out.println("Testing printEmployees method in departmentStore class");
store.printEmployees();
System.out.println();
System.out.println("Testing printExecutives method in departmentStore class");
store.printExecutives();
System.out.println();
System.out.println("Testing printHourly method in departmentStore class");
store.printHourly();
System.out.println();
System.out.println("Testing findEmployee method in departmentStore class");
System.out.println(store.findEmployee("222-22-2222"));
System.out.println(store.findEmployee("123-45-6789"));
System.out.println();
System.out.println("Testing removeEmployee method in departmentStore class");
System.out.println("using both removeEmployee(String ssn) and removeEmployee(Employee emp)");
Employee temp1 = store.findEmployee("123-45-6789");
store.removeEmployee("123-45-6789");
System.out.println(store.findEmployee("123-45-6789"));
// using removeEmployee(Employee emp)
Employee temp2 = store.findEmployee("323-23-5666");
store.removeEmployee(store.findEmployee("323-23-5666"));
System.out.println(store.findEmployee("323-23-5666"));
System.out.println("adding employees back in");
store.addEmployee(temp1);
store.addEmployee(temp2);
System.out.println(store.findEmployee("123-45-6789"));
System.out.println(store.findEmployee("323-23-5666"));
System.out.println();
System.out.println("Testing return output for updateHours method in departmentStore class");
System.out.println(store.updateHours("212-33-6978", 0));
System.out.println(store.updateHours("222-22-2222", 20));
System.out.println();
System.out.println("Testing hourly hours update for updateHours method in departmentStore class");
System.out.println(store.updateHours("212-33-6978", 20));
System.out.println(store.updateHours("658-45-9234", 40));
store.printHourly();
System.out.println();
System.out.println("Testing awardBonus method in Executive class");
((Executive)store.findEmployee("123-45-6789")).awardBonus(1000.50);
((Executive)store.findEmployee("623-34-2399")).awardBonus(1240.50);
((Executive)store.findEmployee("345-67-8934")).awardBonus(1220.50);
store.printExecutives();
}
}
StaffMemberIO.java
import java.io.*;
import java.util.*;
public class StaffMemberIO
{
private String myFileName;
private BufferedReader myInfile;
private Scanner scan;
public StaffMemberIO(String filename)
{
myFileName = filename;
openFile();
scan = new Scanner (myInfile);
}
public String toString( )
{
return "[" + myFileName + "]";
}
public StaffMember getNext()
{
if (scan == null)
return null;
try
{
String typeStr = scan.nextLine();
char type = typeStr.charAt(0);
String name = scan.nextLine();
String address = scan.nextLine();
String phone = scan.nextLine();
String socialSecurity = scan.nextLine();
double pay = scan.nextDouble();
scan.nextLine();
switch (type)
{
case 'e': case 'E':
return new Employee(name, address, phone, socialSecurity, pay);
case 'x': case 'X':
return new Executive(name, address, phone, socialSecurity, pay);
case 'h': case 'H':
return new Hourly(name, address, phone, socialSecurity, pay);
}
return null;
}
catch (Exception e){
closeFile( );
return null;
}
}
private void closeFile()
{
if (myInfile == null)
return;
try
{
myInfile.close();
}
catch (IOException e) { }
}
private void openFile()
{
myInfile = null;
try
{
FileReader fr = new FileReader(myFileName);
myInfile = new BufferedReader(fr);
}
catch (IOException e) {System.err.println("Didn't open " + myFileName);}
}
}
StaffMember.java
abstract class StaffMember
{
private String name;
private String address;
private String phone;
//-----------------------------------------------------------------
// Sets up a staff member using the specified information.
//-----------------------------------------------------------------
public StaffMember (String name, String address, String phone)
{
this.name = name;
this.address = address;
this.phone = phone;
}
//-----------------------------------------------------------------
// Returns a string including the basic employee information.
//-----------------------------------------------------------------
public String toString ()
{
String result = "Name: " + name + " ";
result += "Address: " + address + " ";
result += "Phone: " + phone;
return result;
}
//-----------------------------------------------------------------
// Derived classes must define the pay method for each employee
// type.
//-----------------------------------------------------------------
public abstract double pay();
// getters and setters (mutators) for the private data members of this abstract class
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Employee.java
class Employee extends StaffMember
{
protected String socialSecurityNumber;
protected double payRate;
//-----------------------------------------------------------------
// Sets up an employee with the specified information.
//-----------------------------------------------------------------
public Employee (String name, String address, String phone,
String socialSecurityNumber, double payRate)
{
super (name, address, phone);
this.socialSecurityNumber = socialSecurityNumber;
this.payRate = payRate;
}
//-----------------------------------------------------------------
// Returns information about an employee as a string.
//-----------------------------------------------------------------
public String toString ()
{
String result = super.toString();
result += " Social Security Number: " + socialSecurityNumber;
return result;
}
//-----------------------------------------------------------------
// Returns the pay rate for this employee.
//-----------------------------------------------------------------
public double pay ()
{
return payRate;
}
// getter and setter for local private data member
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
public void setSocialSecurityNumber(String socialSecurityNumber) {
this.socialSecurityNumber = socialSecurityNumber;
}
}
Hourly.java
class Hourly extends Employee
{
private int hoursWorked;
//-----------------------------------------------------------------
// Sets up this hourly employee using the specified information.
//-----------------------------------------------------------------
public Hourly (String name, String address, String phone,
String socialSecurityNumber, double payRate)
{
super (name, address, phone, socialSecurityNumber, payRate);
hoursWorked = 0;
}
//-----------------------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours.
//-----------------------------------------------------------------
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//-----------------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//-----------------------------------------------------------------
public double pay ()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
//-----------------------------------------------------------------
// Returns information about this hourly employee as a string.
//-----------------------------------------------------------------
public String toString ()
{
String result = super.toString();
result += " Current hours: " + hoursWorked;
return result;
}
}
Executive.java
class Executive extends Employee
{
private double bonus;
//-----------------------------------------------------------------
// Sets up an executive with the specified information.
//-----------------------------------------------------------------
public Executive (String name, String address, String phone,
String socialSecurityNumber, double payRate)
{
super (name, address, phone, socialSecurityNumber, payRate);
bonus = 0; // bonus has yet to be awarded
}
//-----------------------------------------------------------------
// Awards the specified bonus to this executive.
//-----------------------------------------------------------------
public void awardBonus (double execBonus)
{
bonus = execBonus;
}
//-----------------------------------------------------------------
// Computes and returns the pay for an executive, which is the
// regular employee payment plus a one-time bonus.
//-----------------------------------------------------------------
public double pay ()
{
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
}
You have not provided employees.txt. You should provide the same for better result.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.