*In Java, and simple to understand Use the classes Q3 and Car provided, and writ
ID: 3906683 • Letter: #
Question
*In Java, and simple to understand
Use the classes Q3 and Car provided, and write a class called CarDealership which adheres to the following class description.
-Each dealership has an array of Cars called “cars”
-Each dealership has a number of cars called “numberOfCars”
-Each dealership has a “dealershipID”, and the ID for the first CarDealership made is zero and increments by one every time a dealership is made (as in, when CarDealership’s constructor is called – this requires use of a static variable to hold the next dealership ID AND a non-static variable to hold the assigned ID for each dealership)
-Each dealership has a maximum number of cars it can hold on the lot, called “maxNumberOfCars”
-Each dealership has a name, called “dealerName”
-CarDealership has a single constructor that accepts the maximum number of cars on the lot and the name of the dealer (it must also assign the dealershipID and initialize the cars array)
-Each dealership has a method called addNewCar, which accepts the colour and make of the car as strings, as well as the year and the price of the car, as ints
-if the car was successfully added to the lot, it should print “Added ” followed by the toString of the car, followed by “ to ” followed by the name of the dealer
-if it was not successfully added, it should print “Cannot add ” followed by the toString of the car, followed by “ to ” followed by the name of the dealer, followed by “ – Lot is full!”
-Each dealership has a sellCar method which accepts a car, and this method returns a boolean (true if the car was found on the lot and sold successfully, false otherwise)
-A car is successfully sold if it is found on the lot, and sellCar is unsuccessful if the car does not exist on the lot (as in, it was not found in the cars array)
-This must traverse the cars array, and if it finds a match for the supplied car, it must remove the car from the array and shift all other elements of the array over so there are no gaps (as you’ve done in a previous assignment)
-Of course, this must also decrease the number of cars
-Finally, CarDealership must have a toString method that prints “Dealer: ” followed by the dealer name, followed by “ID: ” and the ID of the dealership on a new line, followed by “Number of Cars on Lot: ” and the number of cars, followed by “/” and the max number of cars for that dealership on a separate line, followed by “Car Inventory:”, followed by all of the toStrings of all of the cars on the lot, all on separate lines
-See the following output to further understand what is expected
Sample Output:
Added red 2016 Lambo Aventador worth $400000 to John's Cool Cars!
Added yellow 2014 Ferrari Enzo worth $500000 to John's Cool Cars!
Added blue 1968 Chevy Camaro worth $120000 to John's Cool Cars!
Added white 2016 Nissan GTR Nismo worth $200000 to John's Cool Cars!
Added blue 2017 Mustang Shelby worth $120000 to John's Cool Cars!
Cannot add black 2016 Nissan GTR worth $180000 to John's Cool Cars - Lot is full!
Added yellow 2012 Toyota Echo worth $6000 to World's Smallest Car Dealership!
Added red 2017 Mini Cooper worth $40000 to World's Smallest Car Dealership!
Added silver 2017 Dodge Ram worth $60000 to Jerry's Truck Emporium!
Added black 2017 Ford Explorer worth $60000 to Jerry's Truck Emporium!
Added white 2017 GMC Sierra worth $60000 to Jerry's Truck Emporium!
Dealer: John's Cool Cars
ID: 0
Number of Cars on Lot: 5/5
Car Inventory:
red 2016 Lambo Aventador worth $400000
yellow 2014 Ferrari Enzo worth $500000
blue 1968 Chevy Camaro worth $120000
white 2016 Nissan GTR Nismo worth $200000
blue 2017 Mustang Shelby worth $120000
Dealer: World's Smallest Car Dealership
ID: 1
Number of Cars on Lot: 2/2
Car Inventory:
yellow 2012 Toyota Echo worth $6000
red 2017 Mini Cooper worth $40000
Dealer: Jerry's Truck Emporium
ID: 2
Number of Cars on Lot: 3/6
Car Inventory:
silver 2017 Dodge Ram worth $60000
black 2017 Ford Explorer worth $60000
white 2017 GMC Sierra worth $60000
Sold a red Lambo Aventador! (you got it right)
Looks like they are out of stock! (you probably got it right)
They don't have this car in stock! (you probably got it right)
Sold a yellow Ferrari! (you got it right)
Dealer: John's Cool Cars
ID: 0
Number of Cars on Lot: 3/5
Car Inventory:
blue 1968 Chevy Camaro worth $120000
white 2016 Nissan GTR Nismo worth $200000
blue 2017 Mustang Shelby worth $120000
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Car {
private String colour;
private String make;
private int year;
private int price;
public Car(String colour, String make, int year, int price){
this.colour = colour;
this.make = make;
this.year = year;
this.price = price;
}
public String toString(){
return colour + " " + year + " " + make + " worth $" + price;
}
public void setPrice(int p){
price = p;
}
public boolean equals(Car other){
if (other != null){
return (other.colour == this.colour && other.make == this.make && other.year == this.year);
}
return false;
}
public boolean isThisCarOnSale(Car other){
return price < other.price;
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Q3 {
public static void main(String args[]){
CarDealership c1 = new CarDealership(5, "John's Cool Cars");
CarDealership c2 = new CarDealership(2, "World's Smallest Car Dealership");
CarDealership c3 = new CarDealership(6, "Jerry's Truck Emporium");
c1.addNewCar("red", "Lambo Aventador", 2016, 400000);
c1.addNewCar("yellow", "Ferrari Enzo", 2014, 500000);
c1.addNewCar("blue", "Chevy Camaro", 1968, 120000);
c1.addNewCar("white", "Nissan GTR Nismo", 2016, 200000);
c1.addNewCar("blue", "Mustang Shelby", 2017, 120000);
c1.addNewCar("black", "Nissan GTR", 2016, 180000);
System.out.println(" ");
c2.addNewCar("yellow", "Toyota Echo", 2012, 6000);
c2.addNewCar("red", "Mini Cooper", 2017, 40000);
System.out.println(" ");
c3.addNewCar("silver", "Dodge Ram", 2017, 60000);
c3.addNewCar("black", "Ford Explorer", 2017, 60000);
c3.addNewCar("white", "GMC Sierra", 2017, 60000);
System.out.println(" ");
System.out.println(c1.toString());
System.out.println(c2.toString());
System.out.println(c3.toString());
boolean sold = c1.sellCar(new Car("red", "Lambo Aventador", 2016, 400000));
if (sold) System.out.println("Sold a red Lambo Aventador! (you got it right)");
else System.out.println("Why can't I sell this car?! :(");
sold = c1.sellCar(new Car("red", "Lambo Aventador", 2016, 400000));
if (sold) System.out.println("That's strange, we just sold our only Lambo Aventador so how can we sell another?");
else System.out.println("Looks like they are out of stock! (you probably got it right)");
sold = c1.sellCar(new Car("red", "Ferrari Enzo", 2014, 500000));
if (sold) System.out.println("It's a miracle! They sold a car they don't have!");
else System.out.println("They don't have this car in stock! (you probably got it right)");
sold = c1.sellCar(new Car("yellow", "Ferrari Enzo", 2014, 500000));
if (sold) System.out.println("Sold a yellow Ferrari! (you got it right)");
else System.out.println("Why can't I have the yellow Ferrari?! :(");
System.out.println(" ");
System.out.println(c1.toString());
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
Hi. please find the answer above.. In case of any doubts, please ask in comments. If the answer helps you, please upvote. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.