Hi I'm working on a java program that allows user to input their information such as name, car model, capacity etc, later they can see their information in a list:
Car Registration Listing
Reg No. Name IC No. Plate No. Color Year Make Model Capacity
1001 John Wayne 111111111 ABC123 Blue 2010 Toyota Vios 1.5
1002 Bea Arthur 222222222 WEA888 Red 2010 Nissan Teana 2.0
1003 Meg Ryan 333333333 PBL168 Black 2011 Honda City 1.6
1004 Jane Doe 444444444 BBB777 White 2011 Nissan Teana 2.0
1005 Al Johnson 555555555 CAT118 Green 2012 Toyota Vios 1.5
1006 Ned Beatty 666666666 TV798 Blue 2012 Toyota Vios 1.5
Below is the code:
public class CarRegistrationListing {
int regNo;
String name;
int icNo;
String plateNo;
String color;
int year;
String make;
String model;
double capacity;
public CarRegistrationListing(int regNo, String name, int icNo, String plateNo, String color, int year, String make, String model, double capacity) {
this.regNo = regNo;
this.name = name;
this.icNo = icNo;
this.plateNo = plateNo;
this.color = color;
this.year = year;
this.make = make;
this.model = model;
this.capacity = capacity;
}
public int getRegNo() {
regNo++;
return regNo;
}
public String getName(){
return name;
}
public int getIcNo(){
return icNo;
}
public String getPlateNo(){
return plateNo;
}
public String getColor(){
return color;
}
public int getYear(){
return year;
}
public String getMake(){
return make;
}
public String getModel(){
return model;
}
public double getCapacity(){
return capacity;
}
public void setName(String name){
this.name = name;
}
public void setIcNo(int icNo){
this.icNo = icNo;
}
public void setPlateNo(String plateNo){
this.plateNo = plateNo;
}
public void setColor(String color){
this.color = color;
}
public void setYear(int year){
this.year = year;
}
public void setMake(String make){
this.make = make;
}
public void setModel(String model){
this.model = model;
}
public void setCapacity(double capacity){
this.capacity = capacity;
}
@Override
public String toString() {
return "CarRegistrationListing{" + "regNo=" + regNo + ", name=" + name + ", icNo=" + icNo + ", plateNo=" + plateNo + ", color=" + color + ", year=" + year + ", make=" + make + ", model=" + model + ", capacity=" + capacity + '}';
}
}
This is for test run:
import java.util.Scanner;
public class TestCarReg {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("How many records do you want to register: ");
int record = scan.nextInt();
CarRegistrationListing car = new CarRegistrationListing(1000, "", 0, "", "", 0, "", "", 0.0);
for (int i = 0; i < record; i++) {
String[] carType = {"Toyota Vios", "Nissan Teana", "Honda City"};
System.out.println("Car Type:");
for (String carType1 : carType) {
System.out.println(carType1);
}
System.out.print("Choose your car type(1 to 3): ");
int type = scan.nextInt();
String make;
String model;
switch (type) {
case 1:
make = "Toyota";
model = "Vios";
break;
case 2:
make = "Nissan";
model = "Teana";
break;
default:
make = "Honda";
model = "City";
break;
}
System.out.print("Enter name: ");
String name = scan.nextLine();
car.setName(name);
scan.nextLine();
System.out.print("Enter IC number: ");
int icNo = scan.nextInt();
car.setIcNo(icNo);
System.out.print("Enter plate number: ");
String plateNo = scan.next();
car.setPlateNo(plateNo);
System.out.print("Enter color: ");
String color = scan.next();
car.setColor(color);
System.out.print("Enter year: ");
int year = scan.nextInt();
car.setYear(year);
car.setMake(make);
car.setModel(model);
System.out.print("Enter capacity: ");
double capacity = scan.nextDouble();
car.setCapacity(capacity);
}
System.out.println("Car Registration Listing");
for (int i = 0; i < record; i++) {
System.out.println("Reg No.\tName\t\tIC No.\tPlate No.\tColor\tYear\tMake\tModel\tCapacity");
System.out.println(car.getRegNo() + "\t" + car.getName() + "\t\t" + car.getIcNo() + "\t" + car.getPlateNo() + "\t\t" + car.getColor() + "\t" + car.getYear() + "\t" + car.getMake() + "\t" + car.getModel() + "\t" + car.getCapacity());
}
}
}
I tried printing the output according to the sample shown above, but I got this in my output:
debug: How many records do you want to register: 2
Car Type: Toyota Vios Nissan Teana Honda City
Choose your car type(1 to 3): 1
Enter name: william Sebastian
Enter IC number: 1111
Enter plate number:AC1212
Enter color: Red
Enter year: 2010
Enter capacity: 1.0
Car Type: Toyota Vios Nissan Teana Honda City
Choose your car type(1 to 3): 1
Enter name: wong ang soon
Enter IC number: 2222
Enter plate number: AR1234
Enter color: White
Enter year: 2013
Enter capacity: 2.0
Reg No. Name IC No. Plate No. Color Year Make Model Capacity
1001 2222 AR1234 White 2013 Honda City 2.0
Reg No. Name IC No. Plate No. Color Year Make Model Capacity
1002 2222 AR1234 White 2013 Honda City 2.0
The name can't be printed and the variables for first person were disappeared, I tried to use array for CarRegistrationListing but it caused error. I would appreciate if someone can point out my mistakes. Thank you so much!