JAVA PROGRAMMING You have seen car Dealership in street. What if you are ask to
ID: 3858408 • Letter: J
Question
JAVA PROGRAMMING
You have seen car Dealership in street.
What if you are ask to write java Program to design Dealership class.
Think what dealership has, Cars, SalePersons, Manager and more objects.
We are only concerned for Car, SalesPerson, Manager and Dealership.
Write a class for Car and have the following private fields or members:
make , price, year and vinNumber
Write a class for SalesPerson and have the following private fields or members
First name, last name, Bonus, address, date of hiring.
Write a class for Manager and have the following private fields or members:
First name, last name, address, date of hiring.
Write a class for Dealership and have the following private fields or members:
Array of SalesPerson, Array of Car, and one Manager.
Provide constructor and methods for all of above classes. You should have
setters and getters, and method toString() that returns states of objects that
is what it knows about object (example: First name, last…).
Dealership class should have methods addSalesPerson, removeSalesPerson,
carSold , removeCar, addCar, and toString().
toString() should print state of Dealership: manager , SalesPersons, Cars,
and number of car sold.
The Following shows one possible layout of Dearship class:
public class Dealership {
private ArrayList <SalesPerson> sp = new ArrayList <SalesPerson> ();
private ArrayList <Car> cars= new ArrayList <Car> ();
private Manager mg;
public Dealership()
{
}
public void addCar(Car another)
{
}
public void addSalesPerson(SalesPerson s)
{
}
public void removeSalesPerson(SalesPerson s)
{
}
public void carSold (SalesPerson s)
{
}
public void removeCar( Car aCar)
{
}
public SalesPerson getPersonOfTheMonth()
{
}
public String toString()
{
}
Note: thats why we use ArrayList class because we have no clue of how many cars,or SalesPerson Dealership has. ArrayList has very useful methods that make life easy.
Dealership must only created when associate a Manager with it no other way
Method getPersonOfTheMonth()
Should return SalesPerson that makes highest bonus.
Feel free to add any other
Methods that you think is good to have for Dealership
Your Driver class that you must use:
public class TestDealership
{
public static void main(String [] args)
{
// Create one manager
// Create at least 5 SalesPerson
// Create at Least 10 Cars
// Create one Dealership and associates manager with it.
// Use Dealership to add SalesPerson and Cars
// now test your program for all the method that was given in Dealership
// Example carSold, reamoveCar, reamoveSalesPerson….
// Note if the Car was sold you should not sell again
System.exit(0);
}
}
Explanation / Answer
public class Car {
private String make;
private double price;
private int year;
private int vinNumber;
Car(String m,double p,int y,int v)
{
make=m;
price=p;
year=y;
vinNumber=v;
}
public String getMake()
{
return make;
}
public double getPrice()
{
return price;
}
public int getYear()
{
return year;
}
public int getVinNumber()
{
return vinNumber;
}
public void setPrice(double p)
{
price=p;
}
public void setYear(int y)
{
year=y;
}
public void setVinNumber(int v)
{
vinNumber=v;
}
public void setMake(String m)
{
make=m;
}
public String toString()
{
return String.format("(%s,%f,%d,%d)", make,price,year,vinNumber);
}
}
--------------------------
public class SalesPerson {
private String firstName;
private String lastName;
private double bonus;
private int dateOfHiring;
private String address;
SalesPerson(String f,String l ,double b,int h,String a)
{
firstName=f;
lastName=l;
bonus=b;
dateOfHiring=h;
address=a;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public double getBonus()
{
return bonus;
}
public int getDateOfHiring()
{
return dateOfHiring;
}
public String getAddress()
{
return address;
}
public void setFirstName(String f)
{
firstName=f;
}
//write setters here
public String toString()
{
return String.format("(%s,%s,%f,%d,%s)", firstName,lastName,bonus,dateOfHiring,address);
}
}
-----------------------------------
public class Manager {
private String firstName;
private String lastName;
private int dateOfHiring;
private String address;
Manager(String f,String l ,double b,int h,String a)
{
firstName=f;
lastName=l;
dateOfHiring=h;
address=a;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getDateOfHiring()
{
return dateOfHiring;
}
public String getAddress()
{
return address;
}
public void setFirstName(String f)
{
firstName=f;
}
//write setters for other members here
public String toString()
{
return String.format("(%s,%s,%d,%s)", firstName,lastName,dateOfHiring,address);
}
}
---------------------------------
import java.util.ArrayList;
public class Dealership {
private ArrayList <SalesPerson> sp = new ArrayList <SalesPerson> ();
private ArrayList <Car> cars= new ArrayList <Car> ();
private Manager mg;
private int carsSold=0;
public Dealership( Manager m)
{
mg=m;
}
public void addCar(Car another)
{
cars.add(another);
}
public void addSalesPerson(SalesPerson s)
{
sp.add(s);
}
public void removeSalesPerson(SalesPerson s)
{
sp.remove(s);
}
public void carSold (SalesPerson s,Car c)
{
s.setBonus(s.getBonus()+100);
removeCar(c);
}
public void removeCar( Car aCar)
{
cars.remove(aCar);
}
public SalesPerson getPersonOfTheMonth()
{
SalesPerson p = null;
double maxBonus = 0;
for(int i=0;i<sp.size();i++)
{
if(sp.get(i).getBonus()>maxBonus)
{
maxBonus=sp.get(i).getBonus();
p=sp.get(i);
}
}
return p;
}
public String toString()
{
//return string here
return String.format("%s", mg);
}
}
-------------------------
import java.util.ArrayList;
public class TestDealership
{
public static void main(String [] args)
{
// Create one manager
Manager m=new Manager("a","b",2002,"k");
// Create at least 5 SalesPerson
SalesPerson a=new SalesPerson("x","y",0,2000,"k");
SalesPerson b=new SalesPerson("x","y",0,2000,"k");
SalesPerson c=new SalesPerson("x","y",0,2000,"k");
SalesPerson d=new SalesPerson("x","y",0,2000,"k");
SalesPerson e=new SalesPerson("x","y",0,2000,"k");
// Create at Least 10 Cars
Car f=new Car("uw",5252,73,74);
Car g=new Car("uw",5252,73,74);
Car h=new Car("uw",5252,73,74);
Car i=new Car("uw",5252,73,74);
Car j=new Car("uw",5252,73,74);
Car k=new Car("uw",5252,73,74);
Car l=new Car("uw",5252,73,74);
Car o=new Car("uw",5252,73,74);
Car n=new Car("uw",5252,73,74);
// Create one Dealership and associates manager with it.
Dealership ds=new Dealership(m);
// Use Dealership to add SalesPerson and Cars
ds.addCar(f);
ds.addCar(g);
ds.addSalesPerson(e);
ds.addSalesPerson(d);
// now test your program for all the method that was given in Dealership
// Example carSold, reamoveCar, reamoveSalesPerson….
ds.removeCar(f);
ds.removeSalesPerson(e);
ds.carSold(d,g);
// Note if the Car was sold you should not sell again
System.out.println( ds.getPersonOfTheMonth());
System.exit(0);
}
}
----------------------------------
Note : at few places i have not written setters. Pls complete that.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.