Car Dealership ArrayList Lab IN JAVA You are the new owner
Car Dealership ArrayList Lab ( IN JAVA )
You are the new owner of Paradise Cars, a start-up car dealership. Your lot is small – it’s only large enough to hold a maximum of 5 cars right now. However, your business has been very busy, and you need a computer program to track the cars moving off and on your lot.
Write a Java program to manage the cars on your lot. Your assignments are:
Create a subdirectory called “Car Dealership”. Be sure to save all of your work for this lab in the “Car Dealership” directory.
Given the following UML diagram, write the Car class.
Car
- vin : String
- year : int
- make : String
- model : String
- price : double
+ Car(vin: String)
+ Car(vin: String, year : int, make: String, model : String, price: double)
+ setPrice(newPrice : double)
+ getVin() : String
+ getYear() : int
+ getMake() : String
+ getModel() : String
+ getPrice() : double
+ toString() : String
Write the CarDealership class. This class should allow the user to buy and sell cars, as well as print out the current car inventory.
Declare an array of Car objects to represent the 5-car capacity lot.
Declare an attribute of type int to keep track of how many cars are currently on the lot.
Declare an attribute of type int to hold the maximum capacity of the lot. We hope that our business grows someday and our current capacity of 5 increases.
Provide a constructor accepts the maximum capacity of our dealership and initializes all the attributes declared in steps a-c.
Write a method called setMaxCapacity that allows us to change the maximum number of cars our lot can hold.
Write a method called buyCar that accepts a Car object as a parameter. This method needs to check to make sure there is room on the lot for another car. If space is available then add the car to the inventory list in the first available space and increment the number of cars on the lot. If there is no room on the lot, then print out a message indicating that the lot is full.
Write a method call sellCar that accepts the vehicle identification number (vin) of the car being sold. Search the array for the appropriate car. If it is found, then remove it from the inventory by setting the reference in the array to null and decrement the number of cars on the lot. If the car is not found, then print out an error message.
Write a method called printInventory that prints out the information for each car on the lot.
Write the RunDealership class. This class will be used to test the CarDealership class.
Declare a main method.
In the main method perform the following tasks:
Create the CarDealership object and initialize the capacity to 3.
Purchase a new car with the following detais:
VIN: A1B2C3
Year: 2010
Make: Honda
Model: Accord
Price: $18899
Purchase a new car with the following detais:
1.VIN: XYZ123
Year: 2013
Make: Volkswagen
Model: Passat
Price: $19998
Purchase a new car with the following detais:
VIN: JNL246
Year: 2014
Make: Ford
Model: Fusion
Price: $16905
Print out the inventory list. Verify that all 3 cars are listed.
Try to sell car “ABCDEF” that does not exist.
Try to sell car “XYZ123” that does exist.
Print out the inventory again. Verify that only car “A1B2C3” and car “JNL246” are listed.
Feel free to do any additional testing.
| Car |
| - vin : String - year : int - make : String - model : String - price : double |
| + Car(vin: String) + Car(vin: String, year : int, make: String, model : String, price: double) |
| + setPrice(newPrice : double) + getVin() : String + getYear() : int + getMake() : String + getModel() : String + getPrice() : double + toString() : String |
Solution
Here is the java code for your requirements. Please check it and if happy , kindly rate the answer.
=============================================================================
Car.Java
============
public class Car {
protected String vin;
protected int year;
protected String make;
protected String model;
protected double price;
public Car(String v)
{
vin=v;
}
public Car(String v,int y,String mk,String md,double p)
{
vin=v;
year=y;
make=mk;
model=md;
price=p;
}
public void setPrice(double newPrice)
{
price=newPrice;
}
public String getVin()
{
return vin;
}
public int getYear()
{
return year;
}
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public double getPrice()
{
return price;
}
public String toString()
{
return \"VIN:\"+vin+\"\\tYear:\"+year
+\"\\tMake:\"+make+\"\\tModel:\"+model+\"\\tPrice:$\"+price;
}
}
==================================================
CarDealership.java
==============
public class CarDealership {
protected int capacity; //current capacity
protected Car cars[];
protected int count; //how many cars are there
public CarDealership(int cap)
{
capacity=cap;
cars=new Car[capacity];
}
public void setMaxCapacity(int cap)
{
capacity=cap;
//allocate enough space to hold the cars
if(cars==null || cars.length<capacity)
cars=new Car[capacity];
}
public void buyCar(Car c)
{
if(count<capacity)
{
for(int i=0;i<capacity;i++)
{
if(cars[i]==null) //search for first available space
{
cars[i]=c;
count++;
System.out.println(\"Car added successfully :\"+ c.getVin());
return;
}
}
}
else
{
System.out.println(\"Not enough space ! Can\'t add.\");
}
}
public void sellCar(String vin)
{
boolean found=false;
for(int i=0;i<count && !found;i++)
{
if(cars[i].getVin().compareToIgnoreCase(vin)==0) //ignore case when comparing
{
found=true;
cars[i]=null;
count--;
}
}
if(found)
System.out.println(\"Removed car from inventory: \"+vin);
else
System.out.println(\"Could not find car : \"+vin);
}
public void printInventory()
{
System.out.println(\"Details of cars in inventory \");
System.out.println(\"------------------------------\");
for(int i=0;i<capacity;i++)
if(cars[i]!=null)
System.out.println((i+1)+\". \"+cars[i].toString());
System.out.println(\"------------------------------\");
}
}
==============================
RunDealership.java
===============
package com.paradise;
public class RunCarDealership {
public static void main(String []s)
{
CarDealership dealer=new CarDealership(3);
Car c;
c=new Car(\"A1B2C3\",2010,\"Honda\",\"Accord\",18899);
dealer.buyCar(c);
c=new Car(\"XYZ123\",2013,\"Voldswagen\",\"Passat\",19998);
dealer.buyCar(c);
c=new Car(\"JNL246\",2014,\"Ford\",\"Fusion\",16905);
dealer.buyCar(c);
dealer.printInventory();
dealer.sellCar(\"ABCDEF\");//does not exist
dealer.sellCar(\"XYZ123\");
dealer.printInventory();
c=new Car(\"GGGGG\",2014,\"Ford\",\"Fusion\",22222);
dealer.buyCar(c);
//no more space cant add cars.
c=new Car(\"HHHHH\",2014,\"Ford\",\"Fusion\",22111);
dealer.buyCar(c);
c=new Car(\"MMMM\",2014,\"Ford\",\"Fusion\",16905);
dealer.buyCar(c);
dealer.printInventory();
}
}
========================





