Question

In: Computer Science

Write a Java program for a car dealership. The dealer has 10 cars in the showroom...

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.

Solutions

Expert Solution

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");
       }
   }
  
         
   }
}


Related Solutions

in java pls Write a Car Dealership management program. This program will allow the employees of...
in java pls Write a Car Dealership management program. This program will allow the employees of the Dealership to add cars to their database and sell them. You will be implementing two classes: Car and CarDealership. Car will store data associated with the cars and CarDealership will be the management program. Note: 5 points of your grade is based on Coding Style. You will need to update the Starter Code to follow the standards described here. Use the "Run" button...
Write the introduction for the car showroom project?
Write the introduction for the car showroom project?
You own a car dealership and are trying to decide how to configure the showroom floor....
You own a car dealership and are trying to decide how to configure the showroom floor. The floor has 2000 square feet of usable space. You have hired an analyst and asked her to estimate the NPV of putting a particular model on the floor and how much space each model​ requires: Model NPV Space Requirement​ (sq. ft.) MB345 $ 2 comma 500$2,500 200 MC237 $ 5 comma 000$5,000 250 MY456 $ 4 comma 000$4,000 240 MG231 $ 1 comma...
Suppose that you own a car dealership and purchase (randomly select) 10 cars of a certain...
Suppose that you own a car dealership and purchase (randomly select) 10 cars of a certain make from a production run of 200 cars. Of the 200 cars, 160 are destined to last at least five years without needing a major repair. Using the hypergeometric distribution what is the probability that at least 6 of your 10 cars will last at least five years without needing a major repair.
Needs to be in JAVA. Write a Java program that accepts the total amount of cars...
Needs to be in JAVA. Write a Java program that accepts the total amount of cars sold and total sales amount of a car salesperson for a given month. The salesperson’s paycheck is computed as follows: a. Every sales person gets 10% (commission) of total sales b. Sales totals greater than $50,000 get 5% of total sales amount c. 8 or more cars sold earns the salesperson an extra 3% Please remove 30% (taxes) of the gross pay and list...
One of the cars sold by Walt's car dealership is a very popular subcompact car called...
One of the cars sold by Walt's car dealership is a very popular subcompact car called the Rhino. The final sale price of the basic model of this car varies from customer to customer depending on the negotiating skills and persistence of the customer. Assume that these sale prices of this car are normally distributed with a mean of $19900 and a standard deviation of $360. Round your answers to 2 decimal places. a. Dolores paid $19525 for her Rhino....
A car dealership has 6 red, 11 silver and 5 black cars on the lot. Ten...
A car dealership has 6 red, 11 silver and 5 black cars on the lot. Ten cars are randomly chosen to be displayed in front of the dealership. Complete parts (a) through (c) below. Please write clearly. Thank you! A: find the probability that 4 cars are red and the rest are silver. (round to four decimal places as needed) B: Find the probability that 5 cars are red and 5 are black. (round to six decimal places as needed)...
Write the following java program. Desc: The program computes the cost of parking a car in...
Write the following java program. Desc: The program computes the cost of parking a car in a public garage at the rate $5.00/hour. The client will always be charged for whole hours. For example, if a car parked for 2 hours and 1 minute, the client will be charged for 3 hours. Input: User inputs the entry time and exit time in 24-hr clock format (hh:mm) Output: The enter and exit times, the length of time the car is parked...
You have been hired by a used-car dealership to modify the price of cars that are...
You have been hired by a used-car dealership to modify the price of cars that are up for sale. You will get the information about a car, and then change its price tag depending on a number of factors. Write a program (a script named: 'used_cars.m' and a function named 'car_adjust.m').The script passes the file name 'cars.dat' to the function. The following information is stored inside the file: Ford, 2010 with 40,000 miles and no accident at marked price of...
Write a program that displays all the cars in the given file (no sorting). Each car...
Write a program that displays all the cars in the given file (no sorting). Each car property is separated by a tab character: \t Given file: cars.txt make model year Ford Expedition 2003 Mazda B-Series 1989 Ford Freestar 2003 Hyundai Elantra 2001 Hyundai Entourage 2008 Chevrolet Camaro 2011 Chevrolet Monte Carlo 2006 Chevrolet Blazer 1996 Chevrolet Aveo 2005 Chevrolet Corvette 1999 Mercedes-Benz E-Class 2006 Dodge Avenger 1995 Pontiac Grand Prix 1973 Mitsubishi Outlander 2011 MINI Clubman 2011 Suzuki Aerio 2007...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT