Hello everyone, im writing this post because im in need of some help or guidelines. My programs function is to be able to store cars, with this i mean being able to: add, delete and view the list of cars added.
Everything is supposed to be stored in a txt file.

This is the code i have so far, unfortunately i havent been able to get it to work.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Car {

String make;
String model;
String year;

public Car(Scanner s) {
System.out.println("Enter make ");
make = s.nextLine();
System.out.println("Enter model ");
model = s.nextLine();
System.out.println("Enter the year ");
year = s.nextLine();
}

}

class CarList {

private List<Car> list = new ArrayList<Car>();



public void addCar(Scanner s) {
list.add(new Car(s));
}

}

class AddEntry {
public static void main(String[] args) {
CarList carlist = new CarList();
Scanner s = new Scanner(System.in);
int choice;

System.out.println("\t Welcome to the car  ");

do {
System.out.println("Make a selection ");
System.out.println("1. Add a Car ");
System.out.println("2. View car list ");
System.out.println("3. Delete a car ");
System.out.println("4. Quit ");
System.out.print("Enter your choice plz: ");
choice = s.nextInt();
s.nextLine(); // Discard the rest of the line
if (choice == 1) {
carlist.addCar(s);
}
} while (choice != 4);

}
}

Regards

Recommended Answers

All 5 Replies

What problems are you having? Your question is very vague. You just posted some unfinished code without any further explanation. Also what you did with the Car class is not the right way to do things:

public class Car {
String make = null;
String model = null;
String year = null;
  
public Car(String make, String model, String year) {
   this.make = make;
   this.model = model;
   this.year = year;
}

// ADD GET/SET METHODS
}
class CarList {

private List<Car> list = new ArrayList<Car>();

public void addCar(Car c) {
   list.add(c);
}

public int size() {
  return list.size();
}

public Car getCar(int i) {
  return list.get(i);
}

}
class AddEntry {
public static void main(String[] args) {
CarList carlist = new CarList();
Scanner s = new Scanner(System.in);
int choice;

System.out.println("\t Welcome to the car  ");

do {
System.out.println("Make a selection ");
System.out.println("1. Add a Car ");
System.out.println("2. View car list ");
System.out.println("3. Delete a car ");
System.out.println("4. Quit ");

System.out.print("Enter your choice plz: ");
choice = s.nextInt();
s.nextLine(); // Discard the rest of the line

if (choice == 1) {
   // use the Scanner to read the data from the keyboard and add the Car
  System.out.print("Enter the make");
   String make = s.nextLine();
   ..
   ..

    carlist.add( new Car(make, ..., ...) );
}
} while (choice != 4);

}
}

Thanks for help so far, i really appreciate it.

In my first post i described what i want the program to do, what it currently does is: Launch the interface in the output in the IDE, and through that i get to go through the car creation process.

If i didnt mention it in the first post im really new to java and programming overall. Because of that my ability to appreciate if the code is working/complete is close to zero. Because of this i have problems implenting code from the outside.

Did i make myself a little bit more clear?

Regards

The code that I saw runs ok. All you need to do is add the changes I recommended and proceed with the rest of the choices.

For the view car list,
just have a for loop (use the size() method of the CarList in order to see how many cars the list has)
then iterate the list and use the get method to take each car.
In the for loop, print the values of the attributes of the car

public class Car {
String make = null;
String model = null;
String year = null;
  
public Car(String make, String model, String year) {
   this.make = make;
   this.model = model;
   this.year = year;
}

// ADD GET/SET METHODS
// example:
public void setMake(String make) {
   this.make = make;
}
public String getMake() {
   return this.make;
}
// USE THE GET METHOD TO GET THE VALUE AND PRINT IT.
}

Hey again and thanks for the great help so far!

With the help i got earler i have managed to add the get and set methods to the program, Thanks alot!


Currently im having three problems with the program
I got this comment at row80 "// use the Scanner to read the data from the keyboard and add the Car"

What i dont know is how to implement this, if someone could provide with me some help or hints i would be really thankful. The example you wrote was great, i was able to actually understand:)

This is the error im currently getting when trying to compile/run the program as it is right now:
Exception in thread "main" java.lang.NoClassDefFoundError:/Car$AddEntry

On row 92:
carlist.add( new Car(make, model, year) );
I cant seem to get the add function to work and im not able to determine why.

Here is the code which think has gotten a little bit better. I still dont know how i should make all the stored values get written to a text file so i can view them. If someone could provide with something helpful on this one i would really appreciate it.

import java.io.*;
import java.util.*;

public class Car {
String make = null;
String model = null;
String year = null;
  
public Car(String make, String model, String year) {
   this.make = make;
   this.model = model;
   this.year = year;
}

// ADD GET/SET METHODS
// example:
public void setMake(String make) {
   this.make = make;
}
public String getMake() {
   return this.make;
}
public void setModel(String model){
    this.model = model;
}
public String getModel(){
    return this.model;
}
public void setYear(String year) {
    this.year = year;
}
public String getYear(){
    return this.year;
}
}

// USE THE GET METHOD TO GET THE VALUE AND PRINT IT. -
// Im pretty sure ive managed to do that now.

class CarList {

private List<Car> list = new ArrayList<Car>();

public void addCar(Car c) {
   list.add(c);
}

public int size() {
  return list.size();
}

public Car getCar(int i) {
  return list.get(i);
}

}

class AddEntry {
public static void main(String[] args) {
CarList carlist = new CarList();
Scanner s = new Scanner(System.in);
int choice;

System.out.println("\t Welcome to the car  ");

do {
System.out.println("Make a selection ");
System.out.println("1. Add a Car ");
System.out.println("2. View car list ");
System.out.println("3. Delete a car ");
System.out.println("4. Quit ");

System.out.print("Enter your choice plz: ");
choice = s.nextInt();
s.nextLine(); // Discard the rest of the line

if (choice == 1) {
   // use the Scanner to read the data from the keyboard and add the Car
    // How am i supposed to do this?
 System.out.print("Enter make");
	   String make = s.nextLine();
	   System.out.print("What model?");
	   String model = s.nextLine();
	   System.out.print("What year was the car manufactured?");
	   String year = s.nextLine();




    carlist.add( new Car(make, model, year) );
}
} while (choice != 4);

}
}

Regards

Are you having problems compiling or running?
Because your program seems to be good to be compiled, but can you run it?
If not how are you trying to run it? With what command? And from where you run that command. Are you at the folder where the .class files where generated?

If it doesn't compile what errors do you get?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.