Program 1: Make Carmax program to manage cars. You can manage your inventory of
ID: 662725 • Letter: P
Question
Program 1: Make Carmax program to manage cars. You can manage your inventory of cars through this program. Your program should include at least more than three classes, Menu class, Inventory class, and Car class. Car class has many fields such as car id, brand, model, price, mileage and so on. Car class has methods to set and display fields.Inventory class has an array of Car object as a field. Inventory class has methods to add, delete, and update the array of Car object. Once Inventory object is generated, Car array is updated with inventory files by constructor. The constructor reads the inventory file(text file) and initialize Car array. To update a car infomation, user is supposed to provide the car
Explanation / Answer
Menu.java
--------------------------------
import java.util.Scanner;
public class Menu {
/**
* @param args
*/
static int carid;
static String brand;
static String model;
static int milage;
static int price;
public static void main(String[] args) {
int choice=0;
Car c1 = new Car();
do{
System.out.println("1. Add Car");
System.out.println("2. Delete Car");
System.out.println("3. Update Car");
System.out.println("4. Display Car");
System.out.println("5. Display car Sorted Price");
System.out.println("6. Exit");
System.out.println("Enter your choice : ");
Scanner in = new Scanner(System.in);
int ch = in.nextInt();
switch(ch){
case 1:
System.out.println("Enter the Car Id:");
carid = in.nextInt();
System.out.println("Enter the Brand");
brand =in.nextLine();
System.out.println("Enter the Model no");
model = in.nextLine();
System.out.println("Enter the Milage:");
milage =in.nextInt();
System.out.println("Enter the Price");
price = in.nextInt();
c1.setCarid(carid);
c1.setBrand(brand);
c1.setModel(model);
c1.setPrice(price);
c1.setMilage(milage);
break;
case 2:
System.out.println("Enter the Car Id");
carid = in.nextInt();
c1.deletecar(carid);
System.out.println("card details deleted" + carid);
break;
case 3:
System.out.println("Enter the Car Id");
//Update funcationally was not clearly mention
break;
case 4:
c1.display();
break;
case 5:
c1.display();
break;
case 6:
System.exit(0);
}
}while(choice != 6);
}
}
Car.java
-------------------------
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Car {
int carid;
String brand;
String Model;
int price;
int milage;
public int getCarid() {
return carid;
}
public void setCarid(int carid) {
this.carid = carid;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return Model;
}
public void setModel(String model) {
Model = model;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getMilage() {
return milage;
}
public void setMilage(int milage) {
this.milage = milage;
}
public void display()
{
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("ENTER THE PATH OF TEXT FILE HERE"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void deletecar(int carid)
{
BufferedReader br = null;
String delim=" ";
try {
String cardetails[];
String sCurrentLine;
br = new BufferedReader(new FileReader("ENTER THE PATH OF TEXT FILE HERE"));
while ((sCurrentLine = br.readLine()) != null) {
cardetails=sCurrentLine.split(delim);
if(cardetails[0]==Integer.toString(carid))
{
br.readLine().replace(sCurrentLine, "");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.