In: Computer Science
Write a Java program for a car dealership. The dealer has 10 cars in the showroom and space for 50 cars. Each car has a unique plate number, model, color, company, and a year of manufacture. Your program should have two classes: the "Car" class has the car information and the "info" method that displays the car information. The other class is an array class "CarArray" that has four methods:" display", "insert", "find", and the "delete" method. When the program is executed, the following menu should appear. The insert, search, and delete should ask the user to enter the plate number of the car. If there is more than one car of the same plate number, all of them should be deleted.
1- Diplay
2- Insert
3- Search
4- Delete
Enter your choice:
Enter the plate #:
Extra Points (10 points): Add "findByColor" and "findByModel" methods that should return an array of car with all the information.
I have used comments to describe certain lines of code
First of all we will create a Car class which holds details about the car as given in the question
class Car
{
String plate_num; //stores number plate
String model; //stores model of the car
String color; // stores color of the car
String company; // stores company of the car
int y_o_m; // stores year of manufacture of car
Car(String plate_num,String model,String color,String
company,int y_o_m) // constructor to create a car object
{
this.plate_num=plate_num;
this.model=model;
this.color=color;
this.company=company;
this.y_o_m=y_o_m;
}
void info() // prints the details of the class
{
System.out.println("car plate number = "+plate_num);
System.out.println("car model = "+model);
System.out.println("car color = "+color);
System.out.println("car company = "+company);
System.out.println("car year of manufacture = "+y_o_m);
}
}
Now we will need to create the CarArray class
import java.util.*;
public class CarArray
{
static Car carArray[]=new Car[50]; // as given in the question,
showroom can store 50 cars
static int index=-1; // stores the current count of cars in
showroom
static void display() // function to display all cars
{
if(index==-1)
System.out.println("No cars in the showroom");
for(int i=0;i<=index;i++)
{
carArray[i].info();
System.out.println("----*****------");
}
}
static void insert() // function to store new car details
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Vehicle Number");
String pl_num=sc.nextLine();
if(index==50)
System.out.println("no room for new cars");
System.out.println("Give the car model");
String new_model=sc.nextLine();
System.out.println("Give the car color");
String new_color=sc.nextLine();
System.out.println("Give the car company");
String new_company=sc.nextLine();
System.out.println("Give the car year of manufacture");
int new_y_o_m=sc.nextInt();
index++;
carArray[index]=new
Car(pl_num,new_model,new_color,new_company,new_y_o_m); // using
constructor to create new object of car
}
static void search() // function to search using nameplate
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Vehicle Number");
String pl_num=sc.nextLine();
for(int i=0;i<=index;i++)
{
if(pl_num.equalsIgnoreCase(carArray[i].plate_num))
carArray[i].info();
}
}
static void delete() // delete using nameplate
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Vehicle Number");
String pl_num=sc.nextLine();
int indexrem=-1;
for(int i=0;i<=index;i++)
{
if(pl_num.equalsIgnoreCase(carArray[i].plate_num))
{ indexrem=i;
break;
}
}
if(indexrem==-1)
System.out.println("No car found to be delted");
else
{
for(int i=indexrem;i<index;i++)
{
carArray[i]=carArray[i+1];
}
index--;
}
}
public static void main (String[] args) throws
java.lang.Exception
{
while(true)
{
System.out.println("1-Display");
System.out.println("2-Insert");
System.out.println("3-Search");
System.out.println("4-Delete");
System.out.println("5-Exit");
System.out.println("Enter Your
Choice");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
switch(input)
{
case 1: display();
break;
case 2:insert();
break;
case 3:search();
break;
case 4:delete();
break;
case 5: System.exit();
default: System.out.println("wrong
choice");
}
}
}
}