Search code examples
javaarraysobjectmethodsrecords

Simple Object class and records


I am currently learning Java and I have done several tutorials. However, each exercise was focused on one part (example learning IO, learning Strings, graphics, etc) but only a few had multiple chapters all mixed in between. This means I have difficulties in making a more complete yet still simple program.

Please see underneath my issue. I am making a "Car class" where I created 4 variables. I am giving the user the option to insert the type of car, the year and other two options. After all values were set, we get to display them. So far so (almost) good.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Car {
    String make;
    String model;
    int year;
    String plateNumber; 

    public  Car() {
    }

    // SETS and GETS the type of the Car
    public void setMake (Scanner in) {
        make = in.nextLine();   
    }

    public String getMake () {
        System.out.print("Make: " + make);
        return make;
    }

    // SETS and GETS the model of the Car
    public void setModel (Scanner in) {
        model = in.nextLine();
    }
    public String getModel () {
        System.out.print("Model: " + model);
        return model;
    }

    // SETS and GETS the year of fabrication
    public void setYear(Scanner in) {
        year = in.nextInt();
    }
    public int getYear () {
        System.out.print("Year: " + year);
        return year;
    }

    // SETS and GETS the plateNumber;
    public void setPlate (Scanner in) {
        plateNumber = in.nextLine();
    }
    public String getPlate ()
    {
        System.out.print("License: " + plateNumber);
        return plateNumber;
    }   

    public static void main(String[] args) throws IOException {

        File file = new File("");
        Scanner in = new Scanner(System.in);

        if (!file.exists()) {
            FileWriter writeFile = new FileWriter("cars.txt");
        }

        // CAR 1 ====================
        Car car1 = new Car();
        System.out.println("Car1: ");
        car1.setMake(in);
        car1.getMake();
        car1.setModel(in);
        car1.getModel();        
        car1.setYear(in);
        car1.getYear(); 

        Car [] cars = new Car [5];  // Creates a 5 car array / parking lot

    }

}

After this it should store the data in a [5] Car Array and then save them to an external txt file. The save to file I will add after I solve the underneath two points.

Now the problems:

  1. How do I make the Scanner and display more elegant. As you can see in the underneath printscreen the data is doubled (print to screen and Scanner shows the type of car twice)

enter image description here

  1. How do I store the Car variables in one Array then print only the model or only the year of fabrication, or for Car one, all the values of this car. I know how to make an Array of String, int or double. I just don't know how to store the above variables inside a Car array and get the car1 array values one by one. It puzzles me. Thanks in advance and best regards,

Solution

  • 1.You can certainly takes the input first and then show the entered data. So,

    car1.setMake(in);
    car1.setModel(in);
    car1.setYear(in);
    car1.getMake();
    car1.getModel(); 
    car1.getYear(); 
    

    and change the setters method to prompt the message to enter something,

    public void setMake (Scanner in) {
        System.out.println("Enter Model : ");
        make = in.next();   
    }
    

    2. ArrayList is a better option here than array since you might not know the number of cars. You can make a list of cars and call the method individually to get the expected result.

        ArrayList<Car> carList = new ArrayList<Car>();
        carList.add(car1);
    
        //Call the get(index) method to get the car object and then call the class method you want.
        carList.get(0).getMake();
        carList.get(0).getModel();
    
        //A class always should have a toString() method that returns the string with all the values of class instance.
        System.out.println(carList.get(0).toString());
    

    Also, use toString method for class Car to get values of all the instances of class.

     @Override
        public String toString() {
            return "Car [make=" + make + ", model=" + model + ", year=" + year + ", plateNumber=" + plateNumber + "]";
        }