We are to make a program about a car dealership using arrays. I got the code to display all cars in a list, so I'm good with that. What I'm stuck at is how to make it so when a user inputs s for search, it allows them to search the vehicle. Here is what I have so far
public class Car {
String color;
String model;
String year;
String company;
String plate;
Car(String clr, String
mdl, String yr, String com, String pla){
this.color = clr;
this.model = mdl;
this.year = yr;
this.company = com;
this.plate = pla;
}
public void
introduceSelf(){
System.out.println("This is a"+ " " + this.color + " " + this.year
+ " " + this.company + " " + this.model + " with plate " +
this.plate);
}
public
static void main(String[] args){
String arrModel[]
= {"Corolla", "Mustang", "Cavalier", "LaSabre", "Civic",
"Accord", "Avalon", "Escalade", "XTS", "A220", "Crown Victoria"};
// Car models
String arrPlate[]
= {"11111", "22222", "33333", "44444", "55555",
"66666", "77777", "88888", "99999", "00000"}; // car plates
String
arrCompany[] = {"Toyota", "Ford", "Cheverolet", "Buick",
"Honda",
"Honda", "Toyota", "Cadillac", "Cadillac", "Mercedes Benz",
"Ford"}; // car company
String arrColor[]
= {"Red", "White", "White", "Green", "Black",
"Green", "Blue", "Black", "Orange", "Brown", "Blue"}; // car
color
String arrYear[] =
{"2015", "2019", "1987", "2020", "2020",
"2001", "2004", "2016", "2010", "1991"}; // car year
Car
arrCar[] = new Car[50];
for
(int i = 0; i < 10; i++){
Car c = new Car(arrColor[i], arrYear[i], arrCompany[i],
arrModel[i], arrPlate[i]);
arrCar[i] = c;
}
for(int i = 0; i < 10; i++){
arrCar[i].introduceSelf();
}
}
}
I read the book, and my teachers notes are too vague. All she says
to do is the following for the input part
public static in getInt() throws
IOException{
String str = getString();
return Integer.parseInt(str);
}
public static string getString() throws
IOException{
InputStreamReader
isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String
str = br.readline();
return s;
}
Now what I'm thinking is I could do the following; 1- display, 2-
insert, 3- search, 4-delete and use the scanner method
Scanner scan = new Scanner(System.in)
System.out.println("Choose what you
would like to do: ");
int option =
scan.nextInt();
arrOpt = new
int[option];
Then I'm thinking of using a while statement, then if/else/if else
for search, dipslay, insert, delete. Would that work or is there an
easier method? I'm still doing research on how to do this, but in
the mean time am posting here for some possible guidance as
well.
Here is what I'm thinking of putting in the while statement
for(int i=0; i < option; i++){
array.del[i] = scan.nextInt(); //this
would delete the car
}
Right there I'm not sure how I can have the user choose which car to delete.
//solution is creating a user operative version.
//where user chooses the option to search , add,delete and
print
//i have added few functionality and it also handles the invalid
input.
//------------ Car.java--------------
import java.util.Scanner;
public class Car
{
String color;
String model;
String year;
String company;
String plate;
Car(String clr, String mdl, String yr, String com,
String pla){
this.color = clr;
this.model = mdl;
this.year = yr;
this.company = com;
this.plate = pla;
}
public void introduceSelf()
{
System.out.println("This is a"+ " "
+ this.color + " " + this.year + " " + this.company + " " +
this.model + " with plate " + this.plate);
}
public static void printMenu()
{
System.out.println("\n---------
Menu ------------\n");
System.out.println("1.
Display");
System.out.println("2.
Insert");
System.out.println("3.
Search");
System.out.println("4.
Delete");
System.out.println("0.
Exit");
System.out.print("Enter your
choice: ");
}
public static int getChoice()
{
int choice=0;
boolean isValid = false;
//create Scanner object to take
input from user.
Scanner in = new
Scanner(System.in);
//while user enters valid
choice.
while(!isValid)
{
printMenu();
try
{
choice = in.nextInt();
//if entered number is integer
//then check if it is in the bounds of valid
choices given in menu.
if(choice <0 || choice >4)
{
System.out.println("\nError:
Invalid choice entered");
}
else
{
//if valid then set isValid
to true. to break from loop
isValid = true;
}
}
//if invalid
text entered as input
catch(Exception
e)
{
//then catch that ioexception and print error
message
System.out.println("\nError: Invalid choice
entered");
}
}
return choice;
}
public static boolean insert(Car cars[],Car car,int
carsInserted)
{
if(carsInserted<cars.length)
{
int ind =
search(cars,car.plate,carsInserted);
if(ind ==
-1)
{
cars[carsInserted] = car;
return true;
}
else
{
System.out.println("\nError: Duplicate car, Car
with plate number already existed\n");
return false;
}
}
else
{
System.out.println("\nError: Array is filled");
return
false;
}
}
public static boolean delete(Car cars[],String
plate,int carsInserted)
{
if(carsInserted == 0)
{
System.out.println("\nError: There are no cars to delete");
return
false;
}
else
{
int ind =
search(cars,plate,carsInserted);
if(ind ==
-1)
{
return false;
}
else
{
for(int j = ind;j<carsInserted-1;j++)
{
cars[j] = cars[j+1];
}
return true;
}
}
}
public static int search(Car cars[],String plate,int
carsInserted)
{
if(carsInserted == 0)
{
return -1;
}
else
{
for(int i =
0;i<carsInserted;i++)
{
if(cars[i].plate.equals(plate))
{
return i;
}
}
return -1;
}
}
public static void display(Car cars[],int
carsInserted)
{
if(carsInserted == 0)
{
System.out.println("\nError: There are no cars to Display");
}
else
{
System.out.println("\n---------- Total Cars : "+carsInserted+"
------------\n");
for(int i =
0;i<carsInserted;i++)
{
cars[i].introduceSelf();
}
}
}
public static Car getCar()
{
Car car = null;
String color;
String model;
String year;
String company;
String plate;
Scanner in = new
Scanner(System.in);
System.out.print("\nEnter Car
Color: ");
color = in.nextLine().trim();
if(color.equals(""))
{
System.out.println("Error: Car Color must not empty. ");
return
null;
}
System.out.print("\nEnter Car
Model: ");
model = in.nextLine().trim();
if(model.equals(""))
{
System.out.println("Error: Car Model must not empty. ");
return
null;
}
System.out.print("\nEnter Car Year:
");
year = in.nextLine().trim();
if(year.equals(""))
{
System.out.println("Error: Car Year must not empty. ");
return
null;
}
try
{
int num =
Integer.parseInt(year);
if(num<1)
{
System.out.println("\nError: Invalid year
entered");
return null;
}
}
catch(Exception e)
{
System.out.println("\nError: Invalid year entered");
return
null;
}
System.out.print("\nEnter Car
Company: ");
company =
in.nextLine().trim();
if(company.equals(""))
{
System.out.println("Error: Car Company must not empty. ");
return
null;
}
System.out.print("\nEnter Car
Plate: ");
plate = in.nextLine().trim();
if(plate.equals(""))
{
System.out.println("Error: Car Plate must not empty. ");
return
null;
}
car = new
Car(color,model,year,company,plate);
return car;
}
public static void main(String[] args)
{
String arrModel[] = {"Corolla",
"Mustang", "Cavalier", "LaSabre", "Civic",
"Accord",
"Avalon", "Escalade", "XTS", "A220", "Crown Victoria"}; // Car
models
String arrPlate[] = {"11111",
"22222", "33333", "44444", "55555",
"66666", "77777", "88888", "99999", "00000"}; //
car plates
String arrCompany[] = {"Toyota",
"Ford", "Cheverolet", "Buick", "Honda",
"Honda", "Toyota", "Cadillac", "Cadillac",
"Mercedes Benz", "Ford"}; // car company
String arrColor[] = {"Red",
"White", "White", "Green", "Black",
"Green",
"Blue", "Black", "Orange", "Brown", "Blue"}; // car color
String arrYear[] = {"2015", "2019",
"1987", "2020", "2020",
"2001", "2004", "2016", "2010", "1991"}; // car
year
Car arrCar[] = new Car[50];
Scanner in = new Scanner(System.in);
int carsInserted = 0;
//add first 10 cars.
for (int i = 0; i < 10; i++)
{
Car c = new Car(arrColor[i],
arrYear[i], arrCompany[i], arrModel[i], arrPlate[i]);
arrCar[i] = c;
carsInserted++;
}
int choice;
do
{
choice = getChoice();
switch(choice)
{
case 1:
{
display(arrCar,carsInserted);
break;
}
case 2:
{
Car car = getCar();
if(car == null)
{
System.out.println("\nSorry, Unable to insert car due to invalid
input");
}
else
{
if(insert(arrCar,car,carsInserted))
{
System.out.println("\nSuccessfully inserted
Car");
carsInserted++;
}
else
{
System.out.println("\nSorry, Unable to insert
car due to invalid input");
}
}
break;
}
case 3:
{
String plate;
System.out.print("\nEnter Car
Plate: ");
plate =
in.nextLine().trim();
if(plate.equals(""))
{
System.out.println("Error: Car Plate must not empty. ");
}
else
{
int ind =
search(arrCar,plate,carsInserted);
if(ind !=
-1)
{
System.out.println("\nSuccessfully Found
Car.Details: ");
arrCar[ind].introduceSelf();
}
else
{
System.out.println("\nUnbale to find Entered
plate");
}
}
break;
}
case 4:
{
String plate;
System.out.print("\nEnter Car
Plate: ");
plate =
in.nextLine().trim();
if(plate.equals(""))
{
System.out.println("Error: Car Plate must not empty. ");
}
else
{
if(delete(arrCar,plate,carsInserted))
{
System.out.println("\nSuccessfully Deleted
Car\n");
carsInserted--;
}
else
{
System.out.println("\nUnbale to find Entered
plate");
}
}
break;
}
case 0:
System.out.println("\nQutting...");
break;
}
}while(choice != 0);
}
}
//PLEASE LIKE THE ANSWER AND COMMENT IF YOU HAVE DOUBTS.
/*
------------- OUTPUT -1 -----------
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 1
---------- Total Cars : 10 ------------
This is a Red Toyota Corolla 2015 with plate 11111
This is a White Ford Mustang 2019 with plate 22222
This is a White Cheverolet Cavalier 1987 with plate 33333
This is a Green Buick LaSabre 2020 with plate 44444
This is a Black Honda Civic 2020 with plate 55555
This is a Green Honda Accord 2001 with plate 66666
This is a Blue Toyota Avalon 2004 with plate 77777
This is a Black Cadillac Escalade 2016 with plate 88888
This is a Orange Cadillac XTS 2010 with plate 99999
This is a Brown Mercedes Benz A220 1991 with plate 00000
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 2
Enter Car Color: red
Enter Car Model: toyota
Enter Car Year: k
Error: Invalid year entered
Sorry, Unable to insert car due to invalid input
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 1
---------- Total Cars : 10 ------------
This is a Red Toyota Corolla 2015 with plate 11111
This is a White Ford Mustang 2019 with plate 22222
This is a White Cheverolet Cavalier 1987 with plate 33333
This is a Green Buick LaSabre 2020 with plate 44444
This is a Black Honda Civic 2020 with plate 55555
This is a Green Honda Accord 2001 with plate 66666
This is a Blue Toyota Avalon 2004 with plate 77777
This is a Black Cadillac Escalade 2016 with plate 88888
This is a Orange Cadillac XTS 2010 with plate 99999
This is a Brown Mercedes Benz A220 1991 with plate 00000
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 2
Enter Car Color: red
Enter Car Model: toyota
Enter Car Year: 2010
Enter Car Company: corolla
Enter Car Plate: 11111
Error: Duplicate car, Car with plate number already existed
Sorry, Unable to insert car due to invalid input
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 2
Enter Car Color: red
Enter Car Model: toyota
Enter Car Year: 2019
Enter Car Company: corolla
Enter Car Plate: 12345
Successfully inserted Car
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 1
---------- Total Cars : 11 ------------
This is a Red Toyota Corolla 2015 with plate 11111
This is a White Ford Mustang 2019 with plate 22222
This is a White Cheverolet Cavalier 1987 with plate 33333
This is a Green Buick LaSabre 2020 with plate 44444
This is a Black Honda Civic 2020 with plate 55555
This is a Green Honda Accord 2001 with plate 66666
This is a Blue Toyota Avalon 2004 with plate 77777
This is a Black Cadillac Escalade 2016 with plate 88888
This is a Orange Cadillac XTS 2010 with plate 99999
This is a Brown Mercedes Benz A220 1991 with plate 00000
This is a red 2019 corolla toyota with plate 12345
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 3
Enter Car Plate: 12345
Successfully Found Car.Details:
This is a red 2019 corolla toyota with plate 12345
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 3
Enter Car Plate: 2s
Unbale to find Entered plate
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 3
Enter Car Plate: 88888
Successfully Found Car.Details:
This is a Black Cadillac Escalade 2016 with plate 88888
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 4
Enter Car Plate: 111112
Unbale to find Entered plate
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 4
Enter Car Plate: 11111
Successfully Deleted Car
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 1
---------- Total Cars : 10 ------------
This is a White Ford Mustang 2019 with plate 22222
This is a White Cheverolet Cavalier 1987 with plate 33333
This is a Green Buick LaSabre 2020 with plate 44444
This is a Black Honda Civic 2020 with plate 55555
This is a Green Honda Accord 2001 with plate 66666
This is a Blue Toyota Avalon 2004 with plate 77777
This is a Black Cadillac Escalade 2016 with plate 88888
This is a Orange Cadillac XTS 2010 with plate 99999
This is a Brown Mercedes Benz A220 1991 with plate 00000
This is a red 2019 corolla toyota with plate 12345
--------- Menu ------------
1. Display
2. Insert
3. Search
4. Delete
0. Exit
Enter your choice: 0
Quitting...
*/
We are to make a program about a car dealership using arrays. I got the code...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car { private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...
Java - Car Dealership Hey, so i am having a little trouble with my code, so in my dealer class each of the setName for the salesman have errors and im not sure why. Any and all help is greatly appreciated. package app5; import java.util.Scanner; class Salesman { private int ID; private String name; private double commRate; private double totalComm; private int numberOfSales; } class Car { static int count = 0; public String year; public String model; public String...
Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...
I have this code done in two methods, but it has to be done in one, and I can not figure out how to have it work and run correctly in one method. Chess Board: You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it; i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc. The user and the computer can take any number of pieces from one lane. The...
1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...
I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...
Java Write a pair of serialization / deserialization apps that create a user’s Car list, w/ a class named Car, which includes model, VINumber (int), and CarMake (an enum, with valid values FORD, GM, TOYOTA, and HONDA) as fields. Use an array similar to the way the SerializeObjects did to handle several BankAccounts. Your app must include appropriate Exception Handling via try catch blocks (what if the file is not found, or the user inputs characters instead of digits for...