I am working on a lesson of a Car Garage using user input stored in an array and/or arraylist. Those were atleast the specific instructions provided. I am new to programming and lack some technical language. Thanks for your help in advance.
Current issue: I can accept user input/entry but the last entered car will be the only one saved and outputted.
public class Car {
String make;
String model;
int year;
double price;
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class CarApp {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char userCon = 'Y';
System.out.println("Welcome to the Garage!");
while(userCon =='Y') {
System.out.println("How many cars will you be entering?");
int carAmount = scan.nextInt();
ArrayList<Car> car = new ArrayList<Car>(carAmount);
Car carMake = new Car();
Car carYear = new Car();
Car carModel = new Car();
Car carPrice = new Car();
for(int i =0;i<carAmount;i++) {
System.out.println("Enter the make of the car " + (i + 1) + ":");
carMake.make = scan.next();
System.out.println("Enter the model of the car: ");
carModel.model = scan.next();
System.out.println("Enter the year: ");
carYear.year = scan.nextInt();
System.out.println("Enter the price: ");
carPrice.price = scan.nextDouble();
System.out.println("Thank you, Car " + (i + 1) + " is set!");
}
System.out.println("The garage is holding " + carAmount + " cars.");
for(int n =0;n<carAmount;n++) {
System.out.println("Make: " + carMake.getMake());
System.out.println("Model: " + carModel.getModel());
System.out.println("Year: " + carYear.getYear());
System.out.println("Price: " + carModel.getPrice());
}
System.out.println("Would you like to try again? Y/N");
String word = scan.next();
word = word.toUpperCase();
userCon = word.charAt(0);
}
scan.close();
}
}
public class CarApp {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char userCon = 'Y';
System.out.println("Welcome to the Garage!");
while(userCon =='Y') {
System.out.println("How many cars will you be entering?");
int carAmount = scan.nextInt();
ArrayList<Car> carList = new ArrayList<Car>(carAmount);
for(int i =0;i<carAmount;i++) {
Car car = new Car();
System.out.println("Enter the make of the car " + (i + 1) + ":");
car.make = scan.next();
System.out.println("Enter the model of the car: ");
car.model = scan.next();
System.out.println("Enter the year: ");
car.year = scan.nextInt();
System.out.println("Enter the price: ");
car.price = scan.nextDouble();
System.out.println("Thank you, Car " + (i + 1) + " is set!");
carList.add(car);
}
System.out.println("The garage is holding " + carAmount + " cars.");
for (Car car : carList) {
System.out.println("Make: " + car.getMake());
System.out.println("Model: " + car.getModel());
System.out.println("Year: " + car.getYear());
System.out.println("Price: " + car.getPrice());
}
System.out.println("Would you like to try again? Y/N");
String word = scan.next();
word = word.toUpperCase();
userCon = word.charAt(0);
}
scan.close();
}
You need to create a Car object, and need to assign the values to that particular object for a car. You do this in a loop and add it to carList. After the loop is done, carList contains a list of cars. You need to iterate that list to print your data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.