In: Computer Science
In the previous lab you created a Car class and a Dealership
class. Now, in this activity change the
design of the Dealership class, in which the list of the cars
inside a dealership will be stored in an ArrayList.
Then, provide required getter and setter methods to keep the
records of all its cars. In your tester class, test
your class and also printout the list of all cars of a given
dealership object.
// Java program
import java.util.ArrayList;
class Car {
String make;
String model;
int year;
double transmission;
int seats;
int maxSpeed;
int wheels;
String type;
public Car() {
}
public Car(String make, String model, int year, double
transmission, int seats, int maxSpeed, int wheels, String type)
{
this.make = make;
this.model = model;
this.year = year;
this.transmission = transmission;
this.seats = seats;
this.maxSpeed = maxSpeed;
this.wheels = wheels;
this.type = type;
}
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
public void setTransmission(double transmission) {
this.transmission = transmission;
}
public void setSeats(int seats) {
this.seats = seats;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public void setType(String type) {
this.type = type;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getTransmission() {
return transmission;
}
public int getSeats() {
return seats;
}
public int getMaxSpeed() {
return maxSpeed;
}
public int getWheels() {
return wheels;
}
public String getType() {
return type;
}
public String toString() {
return '\n' + "Make: " + make + '\n' +
"Model: " + model + '\n' +
"Year: " + year + '\n' +
"Transmission: " + transmission + '\n' +
"Seats: " + seats + '\n' +
"Maximum Speed: " + maxSpeed + '\n' +
"Wheels: " + wheels + '\n' +
"Type: " + type;
}
}
class Dealership {
String name;
String location;
ArrayList managers = new ArrayList<>();
ArrayList employees = new ArrayList<>();
ArrayList cars = new ArrayList<>();
public Dealership(String name, String location) {
this.name = name;
this.location = location;
}
public void setName(String name) {
this.name = name;
}
public void setLocation(String location) {
this.location = location;
}
public void setManagers(ArrayList managers) {
this.managers = managers;
}
public void setEmployees(ArrayList employees) {
this.employees = employees;
}
public void setCars(ArrayList cars) {
this.cars = cars;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public ArrayList getManagers() {
return managers;
}
public ArrayList getEmployees() {
return employees;
}
public ArrayList getCars() {
return cars;
}
public void addCar(Car c) {
this.cars.add(c);
}
public void addEmployee(String e) {
this.employees.add(e);
}
public void addManager(String m) {
this.managers.add(m);
}
}
public class TestDealer {
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Camry", 2019, 18.25, 5, 250, 4,
"SUV");
Car car2 = new Car();
car2.setMake("Nissan");
car2.setModel("Altima");
car2.setMaxSpeed(275);
car2.setSeats(5);
car2.setYear(2018);
car2.setTransmission(16.25);
car2.setType("Sedan");
car2.setWheels(4);
Dealership d1 = new Dealership("Auto Dealer", "Woodland");
ArrayList<String> e = new ArrayList<>();
e.add("John");
e.add("Sam");
ArrayList<String> m = new ArrayList<>();
m.add("Adam");
m.add("Bob");
d1.setEmployees(e);
d1.setManagers(m);
d1.addCar(car1);
d1.addCar(car2);
System.out.println("Dealership Information:");
System.out.println("Name: "+d1.getName());
System.out.println("Location: "+d1.getLocation());
System.out.println("Employees: "+d1.getEmployees());
System.out.println("Managers: "+d1.getManagers());
System.out.println("Cars: "+d1.getCars());
}
}
// Need to change the design of the Dealership class, in which
the list of the cars inside a dealership will be stored in an
ArrayList.
Then, provide required getter and setter methods to keep the
records of all its cars.
/**********************************
File Name : - Testdealer.java
***********************************/
import java.util.ArrayList;
import java.util.*;
class Car {
String make;
String model;
int year;
double transmission;
int seats;
int maxSpeed;
int wheels;
String type;
Car()
{
;
}
Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) {
this.make = make;
this.model = model;
this.year = year;
this.transmission = transmission;
this.seats = seats;
this.maxSpeed = maxSpeed;
this.wheels = wheels;
this.type = type;
}
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
public void setTransmission(double transmission) {
this.transmission = transmission;
}
public void setSeats(int seats) {
this.seats = seats;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
public void setType(String type) {
this.type = type;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getTransmission() {
return transmission;
}
public int getSeats() {
return seats;
}
public int getMaxSpeed() {
return maxSpeed;
}
public int getWheels() {
return wheels;
}
public String getType() {
return type;
}
}
class Dealership {
String name;
String location;
ArrayList<String> Manager = new ArrayList<String>();
ArrayList<String> Employees = new ArrayList<String>();
ArrayList<String> Cars = new ArrayList<String>();
Dealership()
{;}
Dealership(String name, String location) {
this.name = name;
this.location = location;
}
public void setName(String name) {
this.name = name;
}
public void setLocation(String location) {
this.location = location;
}
public void setManager(ArrayList<String> mng)
{
for(int i = 0; i < mng.size(); i++) {
Manager.add(mng.get(i));
}
}
public ArrayList<String> getmanagers()
{
return (Manager);
}
public void setEmployees(ArrayList<String> employees)
{
for(int i = 0; i < employees.size(); i++) {
Employees.add(employees.get(i));
}
}
public void setCars(ArrayList<String> cars)
{
for(int i = 0; i < cars.size(); i++) {
Cars.add(cars.get(i));
}
}
public String getName()
{
return name;
}
public String getLocation()
{
return location;
}
public ArrayList<String> getCars()
{
return (Cars);
}
public ArrayList<String> getemployees()
{
return (Employees);
}
}
public class Testdealer {
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Camry", 2019, 18.25, 5, 250, 4, "SUV");
Car car2 = new Car();
car2.setMake("Nissan");
car2.setModel("Altima");
car2.setMaxSpeed(275);
car2.setSeats(5);
car2.setYear(2018);
car2.setTransmission(16.25);
car2.setType("Sedan");
car2.setWheels(4);
System.out.println("--------------------");
System.out.println("Getting Car1 Details : ");
System.out.println("--------------------");
System.out.println(car1.getMake());
System.out.println(car1.getModel());
System.out.println(car1.getMaxSpeed());
System.out.println(car1.getSeats());
System.out.println(car1.getYear());
System.out.println(car1.getTransmission());
System.out.println(car1.getType());
System.out.println(car1.getWheels());
System.out.println("--------------------");
System.out.println("Getting Car2 Details : ");
System.out.println("--------------------");
System.out.println(car2.getMake());
System.out.println(car2.getModel());
System.out.println(car2.getMaxSpeed());
System.out.println(car2.getSeats());
System.out.println(car2.getYear());
System.out.println(car2.getTransmission());
System.out.println(car2.getType());
System.out.println(car2.getWheels());
Dealership d1=new Dealership("Dadda","Newyork");
ArrayList<String> manager = new ArrayList<String>();
ArrayList<String> employees = new ArrayList<String>();
ArrayList<String> cars = new ArrayList<String>();
manager.add("John");
manager.add("Smith");
manager.add("Devd");
employees.add("Gayle");
employees.add("Holder");
employees.add("Jason");
cars.add("BMW");
cars.add("Toyota");
cars.add("Ferrari");
d1.setManager(manager);
d1.setCars(cars);
d1.setEmployees(employees);
System.out.println("--------------------");
System.out.println("Getting Dealership Details : ");
System.out.println("--------------------");
System.out.println(d1.getName());
System.out.println(d1.getLocation());
System.out.println(d1.getmanagers());
System.out.println(d1.getemployees());
System.out.println(d1.getCars());
}}
some modification has been done on the return type of arraylist in get and set method go throuh it and check the details .
output_screen