Model the following in a Java program: In a software engineering project

Question:

Model the following in a Java program:

In a software engineering project, we are developing software for an automobile production plant. We
have the following users in the system: Worker and Manager. Each of these is a user and every user has
name, surname, username, password and address. The worker can be AutoWorker, TruckWorker or
BusWorker. The manager can be AutoManager. TruckManager or BusManager.
Model these in an inheritance model considering that:

  1. Worker has these fields in addition to user: SSN, WagePerHour, Bonus, HoursOfWork, Salary.
  2. AutoWorker has a field in addition to Worker: NumbeOfAutosCompleted
  3. TruckWorker has a field in addition to Worker: NumberOfPartsCompleted
  4. BusWorker has a field in addition to Worker: NumberOfComponentsMounted
  5. All workers have their salary calculated based on the respective fields: WagePerHour and
    hoursOfWork.
  6. Manager has these fields in addition to user: SSN, WagePerHour, Bonus, HoursOfWork, Salary.
    NumberOfWorkerSupervised, NumberOfTeams, Bonus
  7. AutoManager has these fields in addition to manager: NumbeOfAutosCompleted. Bonus is
    computed based on this formula: NumbeOfAutosCompleted x 100.
  8. TruckManager has these fields in addition to manager: NumbeOfTrucksCompleted and
    NumberofPartsCompleted: Bonus is computed based on this formula:
    NumbeOfTrucksCompleted x 100 + NumberofPartsCompleted x 50.
  9. BusManager has these fields in addition to manager: NumbeOfBusesCompleted and
    NumberofComponentsMounted: Bonus is computed based on this formula:
    NumbeOfBusesCompleted x 100 + NumberofComponentsMounted x 50.
    Develop these methods:
  10. Two overloaded constructors for all workers and all managers, one with just name and surname
    and one with all the fields as parameters.
  11. computeBonus( ) and computeSalary( ) for all workers and managers.
  12. toString( ) method for each class.
  13. Write a class to test the program:
    a. Create two users for each type by using the appropriate constructors.
    b. Put the users in two separate data structures of Java that you have learned, one for the
    workers and one for the managers.
    c. Print the information of all the users that are stored in the data structure.

Answer:

Hi there,

Following is the answer,If you still have any queries,feel free to ask.

User.java

package productionplant;

public class User {
  
String name;
String surname;
String username;
String password;
String address;


public String toString()
{
   return "\nName : "+name+"\nSurname : "+surname+"Address : "+address;
}


}


Worker.java

package productionplant;

public class Worker extends User{

int SSN;
int WagePerHour;
int Bonus;
int HoursOfWork;
int Salary;

Worker(String name,String surname)
{
   this.name=name;
   this.surname=surname;
}

Worker(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int Bonus,int HoursOfWork)
{   this.name=name;
    this.surname=surname;
    this.username=username;
    this.password=password;
    this.address=address;
   this.SSN=SSN;
   this.WagePerHour=WagePerHour;
   this.Bonus=Bonus;
    this.HoursOfWork=HoursOfWork;
   this.Salary=this.HoursOfWork*this.WagePerHour;
  
}

public String toString()
{
   return super.toString()+"\nSSN : "+SSN+"\nWage Per Hour : "+WagePerHour+"\nHoursOfWork :"+HoursOfWork+"\nBase Salary : "+Salary;
}


  
  
}


Manager.java

package productionplant;

public class Manager extends User{

   int SSN;
   int WagePerHour;
   int Bonus;
   int HoursOfWork;
   int Salary;
   int NumberOfWorkerSupervised,NumberOfTeams;

   Manager(String name,String surname)
   {
       this.name=name;
       this.surname=surname;
   }
  
   Manager(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int HoursOfWork,int NumberOfWorkerSupervised,int NumberOfTeams)
   {   this.name=name;
        this.surname=surname;
        this.username=username;
        this.password=password;
        this.address=address;
       this.SSN=SSN;
       this.WagePerHour=WagePerHour;
      
        this.HoursOfWork=HoursOfWork;
       this.Salary=this.WagePerHour*this.HoursOfWork;
       this.NumberOfWorkerSupervised=NumberOfWorkerSupervised;
       this.NumberOfTeams=NumberOfTeams;
   }
  
   public String toString()
   {
       return super.toString()+"\nSSN : "+SSN+"\nWage Per Hour : "+WagePerHour+"\nHoursOfWork :"+HoursOfWork+"\nBase Salary : "+Salary+"\nNumber Of Workers Supervised : "+NumberOfWorkerSupervised+"\nNumber Of Teams : "+NumberOfTeams;
   }

}

AutoManager.java

package productionplant;

public class AutoManager extends Manager{

   int NumbeOfAutosCompleted;
  
   AutoManager(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int HoursOfWork,int NumberOfWorkerSupervised,int NumberOfTeams,int NumbeOfAutosCompleted)
   {
       super(name,surname,username,password,address,SSN,WagePerHour,HoursOfWork,NumberOfWorkerSupervised,NumberOfTeams);
       this.NumbeOfAutosCompleted=NumbeOfAutosCompleted;
   }
  
   int computeBonus()
   {
       return NumbeOfAutosCompleted*100;
   }
  
   int computeSalary()
   {
       return this.WagePerHour*this.HoursOfWork+computeBonus();
   }
  
   public String toString()
   {
       return super.toString()+"\nType : Auto Manager"+"\nBonus : "+computeBonus()+"\nTotal Salary : "+computeSalary()+"\nNo of Autos Completed : "+NumbeOfAutosCompleted;
      
      
   }
  
}


AutoWorker.java

package productionplant;

public class AutoWorker extends Worker{

   int NumbeOfAutosCompleted;
  
   AutoWorker(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int Bonus,int HoursOfWork,int NumbeOfAutosCompleted)
   {
       super(name,surname,username,password,address,SSN,WagePerHour,Bonus,HoursOfWork);
       this.NumbeOfAutosCompleted=NumbeOfAutosCompleted;
   }
  
  
  
   int computeSalary()
   {
       return this.WagePerHour*this.HoursOfWork;
   }
  
   public String toString()
   {
       return super.toString()+"\nTotal Salary : "+this.computeSalary()+"\nType : "+"Auto Worker"+"\nSalary : "+Salary+"\nNo of Autos Completed : "+NumbeOfAutosCompleted;
   }
  
}


BusManager.java

package productionplant;

public class BusManager extends Manager {

   int NumbeOfBusesCompleted,NumberofComponentsMounted;
  
   BusManager(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int HoursOfWork,int NumberOfWorkerSupervised,int NumberOfTeams,int NumbeOfBusesCompleted, int NumberofComponentsMounted)
   {
       super(name,surname,username,password,address,SSN,WagePerHour,HoursOfWork,NumberOfWorkerSupervised,NumberOfTeams);
       this.NumbeOfBusesCompleted=NumbeOfBusesCompleted;
       this.NumberofComponentsMounted=NumberofComponentsMounted;
   }
  
   int computeBonus()
   {
       return NumbeOfBusesCompleted*100 + NumberofComponentsMounted*50;
   }
  
   int computeSalary()
   {
       return this.WagePerHour*this.HoursOfWork+computeBonus();
   }
  
   public String toString()
   {
       return super.toString()+"\nType : Bus Manager"+"\nBonus : "+computeBonus()+"\nTotal Salary : "+computeSalary()+"\nNo of Buses Completed : "+NumbeOfBusesCompleted+"\nNo of Components Mounted : "+NumberofComponentsMounted;
      
      
   }
  
}


BusWorker.java

package productionplant;

public class BusWorker extends Worker{

   int NumberOfComponentsMounted;
  
   BusWorker(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int Bonus,int HoursOfWork,int NumberOfComponentsMounted)
       {
           super(name,surname,username,password,address,SSN,WagePerHour,Bonus,HoursOfWork);
           this.NumberOfComponentsMounted=NumberOfComponentsMounted;
       }
  
   int computeSalary()
       {
           return this.WagePerHour*this.HoursOfWork;
       }
      
   public String toString()
       {
      
           return super.toString()+"\nTotal Salary : "+this.computeSalary()+"\nType : "+"Bus Worker"+"\nSalary : "+Salary+"\nNo of Components Mounted : "+NumberOfComponentsMounted;
               }
}

TruckManager.java

package productionplant;

public class TruckManager extends Manager{

   int NumbeOfTrucksCompleted,NumberofPartsCompleted;
  
   TruckManager(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int HoursOfWork,int NumberOfWorkerSupervised,int NumberOfTeams,int NumbeOfTrucksCompleted, int NumberofPartsCompleted)
   {
       super(name,surname,username,password,address,SSN,WagePerHour,HoursOfWork,NumberOfWorkerSupervised,NumberOfTeams);
       this.NumbeOfTrucksCompleted=NumbeOfTrucksCompleted;
       this.NumberofPartsCompleted=NumberofPartsCompleted;
   }
  
   int computeBonus()
   {
       return NumbeOfTrucksCompleted*100 + NumberofPartsCompleted*50;
   }
  
   int computeSalary()
   {
       return this.WagePerHour*this.HoursOfWork+computeBonus();
   }
  
   public String toString()
   {
       return super.toString()+"\nType : Truck Manager"+"\nBonus : "+computeBonus()+"\nTotal Salary : "+computeSalary()+"\nNo of Trucks Completed : "+NumbeOfTrucksCompleted+"\nNo of Parts Completed: "+NumberofPartsCompleted;
      
      
   }
}

TruckWorker.java

package productionplant;

public class TruckWorker extends Worker{

   int NumberOfPartsCompleted;

   TruckWorker(String name,String surname,String username,String password,String address,int SSN,int WagePerHour,int Bonus,int HoursOfWork, int NumberOfPartsCompleted)
   {
       super(name,surname,username,password,address,SSN,WagePerHour,Bonus,HoursOfWork);
       this.NumberOfPartsCompleted=NumberOfPartsCompleted;
   }
  
   int computeSalary()
   {
       return this.WagePerHour*this.HoursOfWork;
   }
  
   public String toString()
   {
      
       return super.toString()+"\nTotal Salary : "+this.computeSalary()+"\nType : "+"Truck Worker"+"\nNo of Parts Completed : "+NumberOfPartsCompleted;
      
       }
}

MainImplement.java

package productionplant;

import java.util.ArrayList;

public class MainImplement {

   public static void main(String args[])
   {
      
   AutoWorker aw1=new AutoWorker("John","Jonas","john123","j123","whitefield",1234,50,100,20,2);
   AutoWorker aw2=new AutoWorker("William","Jonas","will123","w123","whitefield",1235,50,100,30,4);
   BusWorker bw1=new BusWorker("Jin","Jonas","jin123","jin123","whitefare",1236,60,200,20,2);
   BusWorker bw2=new BusWorker("Will","Jonas","wil123","wi123","whitefare",1237,60,200,40,6);
   TruckWorker tw1=new TruckWorker("Milli","Cyrus","mill123","m123","whitefare",1238,70,300,20,2);
   TruckWorker tw2=new TruckWorker("Kat","Jonas","kat123","k123","whitefare",1239,70,300,40,6);
   ArrayList<Worker> arrworker=new ArrayList<>();
   arrworker.add(aw1);
   arrworker.add(aw2);
   arrworker.add(bw1);
   arrworker.add(bw2);
   arrworker.add(tw1);
   arrworker.add(tw2);

   AutoManager am1=new AutoManager("Johne","Jonas","mjohn123","mj123","whitefield",1334,150,40,10,2,2);
   AutoManager am2=new AutoManager("Williame","Jonas","mwill123","mw123","whitefield",1335,150,60,20,4,4);
   BusManager bm1=new BusManager("Jine","Jonas","mjin123","mjin123","whitefare",136,160,30,15,1,2,3);
   BusManager bm2=new BusManager("Wille","Jonas","mwil123","mwi123","whitefare",1337,160,80,6,1,3,4);
   TruckManager tm1=new TruckManager("Millie","Cyrus","mmill123","mm123","whitefare",1338,170,40,16,4,2,10);
   TruckManager tm2=new TruckManager("Kate","Jonas","mkat123","mk123","whitefare",1339,170,90,36,6,1,12);
   ArrayList<Manager> arrmanager=new ArrayList<>();
   arrmanager.add(am1);
   arrmanager.add(am2);
   arrmanager.add(bm1);
   arrmanager.add(bm2);
   arrmanager.add(tm1);
   arrmanager.add(tm2);

   System.out.println("The workers are as follows : ");
   for(Worker w:arrworker)
   {
       System.out.println("\n\n"+w);
   }


   System.out.println("\n\nThe managers are as follows : ");
   for(Manager m:arrmanager)
   {
       System.out.println("\n\n"+m);
   }








   }
}

Output is :

he workers are as follows :

Name : John
Surname : JonasAddress : whitefield
SSN : 1234
Wage Per Hour : 50
HoursOfWork :20
Base Salary : 1000
Total Salary : 1000
Type : Auto Worker
Salary : 1000
No of Autos Completed : 2

Name : William
Surname : JonasAddress : whitefield
SSN : 1235
Wage Per Hour : 50
HoursOfWork :30
Base Salary : 1500
Total Salary : 1500
Type : Auto Worker
Salary : 1500
No of Autos Completed : 4

Name : Jin
Surname : JonasAddress : whitefare
SSN : 1236
Wage Per Hour : 60
HoursOfWork :20
Base Salary : 1200
Total Salary : 1200
Type : Bus Worker
Salary : 1200
No of Components Mounted : 2

Name : Will
Surname : JonasAddress : whitefare
SSN : 1237
Wage Per Hour : 60
HoursOfWork :40
Base Salary : 2400
Total Salary : 2400
Type : Bus Worker
Salary : 2400
No of Components Mounted : 6

Name : Milli
Surname : CyrusAddress : whitefare
SSN : 1238
Wage Per Hour : 70
HoursOfWork :20
Base Salary : 1400
Total Salary : 1400
Type : Truck Worker
No of Parts Completed : 2

Name : Kat
Surname : JonasAddress : whitefare
SSN : 1239
Wage Per Hour : 70
HoursOfWork :40
Base Salary : 2800
Total Salary : 2800
Type : Truck Worker
No of Parts Completed : 6


The managers are as follows :

Name : Johne
Surname : JonasAddress : whitefield
SSN : 1334
Wage Per Hour : 150
HoursOfWork :40
Base Salary : 6000
Number Of Workers Supervised : 10
Number Of Teams : 2
Type : Auto Manager
Bonus : 200
Total Salary : 6200
No of Autos Completed : 2

Name : Williame
Surname : JonasAddress : whitefield
SSN : 1335
Wage Per Hour : 150
HoursOfWork :60
Base Salary : 9000
Number Of Workers Supervised : 20
Number Of Teams : 4
Type : Auto Manager
Bonus : 400
Total Salary : 9400
No of Autos Completed : 4

Name : Jine
Surname : JonasAddress : whitefare
SSN : 136
Wage Per Hour : 160
HoursOfWork :30
Base Salary : 4800
Number Of Workers Supervised : 15
Number Of Teams : 1
Type : Bus Manager
Bonus : 350
Total Salary : 5150
No of Buses Completed : 2
No of Components Mounted : 3

Name : Wille
Surname : JonasAddress : whitefare
SSN : 1337
Wage Per Hour : 160
HoursOfWork :80
Base Salary : 12800
Number Of Workers Supervised : 6
Number Of Teams : 1
Type : Bus Manager
Bonus : 500
Total Salary : 13300
No of Buses Completed : 3
No of Components Mounted : 4

Name : Millie
Surname : CyrusAddress : whitefare
SSN : 1338
Wage Per Hour : 170
HoursOfWork :40
Base Salary : 6800
Number Of Workers Supervised : 16
Number Of Teams : 4
Type : Truck Manager
Bonus : 700
Total Salary : 7500
No of Trucks Completed : 2
No of Parts Completed: 10

Name : Kate
Surname : JonasAddress : whitefare
SSN : 1339
Wage Per Hour : 170
HoursOfWork :90
Base Salary : 15300
Number Of Workers Supervised : 36
Number Of Teams : 6
Type : Truck Manager
Bonus : 700
Total Salary : 16000
No of Trucks Completed : 1
No of Parts Completed: 12

Leave a Comment

Your email address will not be published. Required fields are marked *