简体   繁体   中英

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)

在此处输入图片说明

  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,
  1. When you change your getters to not print the value and change order of getters and setters execution you can achive this:

    在此处输入图片说明

     //other code public String getModel () { System.out.print("Model: "); return model; } //other code public static void main(String[] args) { //other code System.out.println("Car1: "); car1.getMake(); car1.setMake(in); car1.getModel(); car1.setModel(in); car1.getYear(); car1.setYear(in); //other code } 
  2. When it comes to second question you could create array of Car object just as any other array and easily access array element field:

     public static void main(String[] args) { //other code Car [] cars = new Main [5]; cars[0] = car1; System.out.println(cars[0].getMake()); System.out.println(cars[0].getModel()); } 

You create array of cars the same way as you would create an array of String.

Car carArray[] = new Car[sizeofarray];
carArray[i] = car1; 

Same goes for reading:

for(int i = 0; i < carArray.length; i++){
     carArray[i].getModel(); // do something here
}

Additional it's not a good practice to pass Scanner as argument to setter. Setter should take argument of the same type as the field is. As per your example how about instead of you doing this:

car1.setMake(in);

you do this:

car1.setMake(in.nextLine());

For nicer print just print pretext first, then wait for user input and don't reprint the input.

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 + "]";
    }

You should use the constructor of the Car class to set the model,year,etc. Take the Car info from the user save it and then pass it to the constructor.

Like this:

public  Car( String make, String model, int year) {
     this.make = make;
     //set the values here
    }

In you main method:

    String make = in.nextLine();
   // You can prompt the user for the car info and then assign the value respectively. Then pass it to the constructor 

   Car car1 = new Car(make,model,year);

If you want to make an array of a car object make a while-loop

      Car cars= new Car[5];
       int i=0;
       While(i<5){
     //ask user for input 
    .
    .
    .
    cars[i] = new Car(make,model,year);
   }

Use System.out.println(); for the getters instead of .print() to make it look nicer. To add cars to the cars[] you uses cars[x]=car.x+1;

This is an interesting question. It seems that there are many different ways to attack this problem. What @NimitPatel suggested is a great idea. You can also try something like this, keeping your current design intact.

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 [] cars = new Car [5];  // Creates a 5 car array / parking lot       

        for(int i = 0; i < 5; i++)
        {
            Car myCar = new Car();
            System.out.println("Enter information for Car" + (i + 1) + ":");
            System.out.println();       
            System.out.print("Car" + (i + 1) + ": ");
            myCar.setMake(in);        
            System.out.print("Model: ");
            myCar.setModel(in);        
            System.out.print("Year: ");
            in.nextLine();           
            System.out.println();
            cars[i] = myCar;           
        }    
    }
}

I would highly suggest perhaps changing the design if you can. In the short run, this may seem unnecessary, and for small projects, it may very well be, but if you plan on using your cars[] array filled with Car objects further down the road, a small redesign may make your code easier to read.

One main suggestion would be to create a stand alone Car class. This class would just contain logic about cars, how to set information about cars and how to get information from cars. Perhaps, something like this:

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

    // Constructor
    public  Car()
    {
    }

    // SETS and GETS the type of the Car
    public void setMake (String make) 
    {
        this.make = make; 
    }

    public String getMake ()
    {        
        return make;
    }

    // SETS and GETS the model of the Car
    public void setModel (String model) 
    {
        this.model = model;
    }

    public String getModel ()
    {        
        return model;
    }

    // SETS and GETS the year of fabrication
    public void setYear(int year) 
    {
        this.year = year;
    }

    public int getYear () 
    {        
        return year;
    }

    // SETS and GETS the plateNumber;
    public void setPlateNumber (String plateNumber)
    {
        this.plateNumber = plateNumber;
    }

    public String getPlateNumber ()
    {       
        return plateNumber;
    }   
}

And then your main class would be responsible for getting information from the user, using that information to create new cars, and putting these cars into an array or arrayList. Something like this maybe:

import java.util.Scanner;

public class Main 
{   
    public static void main(String[] args)
    {
        String make, model;
        int year;    
        Car cars[] = new Car[3];

        // Get information about cars from the user
        // Use this information to create a car
        // Store this car in the array
        for(int i = 0; i < cars.length; i++)
        {   
            System.out.println("Enter information for car " + (i + 1));

            make = getMakeFromUser();
            model = getModelFromUser();
            year = getYearFromUser();

            Car myCurrentCar = new Car();
            myCurrentCar.setMake(make);
            myCurrentCar.setModel(model);
            myCurrentCar.setYear(year);

            cars[i] = myCurrentCar;              
            System.out.println();
        }

        // Display information about cars from the array        
        for(int i = 0; i < cars.length; i++)
        {
            System.out.println("Information about car " + (i + 1));
            System.out.println("Make: " + cars[i].getMake());
            System.out.println("Model: " + cars[i].getModel());
            System.out.println("Year: " + cars[i].getYear());
            System.out.println();
        }
    }

    static String getMakeFromUser()
    {
        String make;
        Scanner in = new Scanner(System.in);        
        System.out.print("Make: ");
        make = in.nextLine();
        return make;
    }

    static int getYearFromUser()
    {
        int year;
        Scanner in = new Scanner(System.in);        
        System.out.print("Year: ");
        year = in.nextInt();
        in.nextLine();
        return year;
    }

    static String getModelFromUser()
    {
        String model;
        Scanner in = new Scanner(System.in);        
        System.out.print("Model: ");
        model = in.nextLine();        
        return model;
    }
}

The idea behind this has to do with the Single Responsibility Principle. If you are writing code for Cars, would you like your Car code to know how to ask the user for input, or would you like your Car code to only know how to keep track of information about Cars. A good video about this principle can be found here . Hope this doesn't complicate things more. Best of luck.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM