Home > database >  How to add elements to a list and display it?in a separate class via methods
How to add elements to a list and display it?in a separate class via methods

Time:05-11

How to implement a class for storing valid cars with a method for adding a new car and a method for listing cars? I understand how to implement this in the main, but how to make add and output methods in a separate class, and data input from main?


public class Storage {
    List<Object> Car = new ArrayList<>();

    public void add(Car car) {
        add(car);
    }

    public void print() {
        System.out.println(Car);
    }
}

CodePudding user response:

To have a starting point let's create one file called Storage.java:

import java.util.*;

class Car {
    private String model; // attribute of a car
    private String manufacturer; // attribute of a car

    // constructor for an instance
    Car(String model, String manufacturer) {
        this.model = model;
        this.manufacturer = manufacturer;
    }

    // transform a car into its string representation
    public String toString() {
        return this.model   " - "   this.manufacturer;
    }   
}

// only public class in this file, contains main method
public class Storage {
    // car list
    private List<Car> cars;
    
    // constructor
    public Storage() {
        this.cars = new ArrayList<Car>();
    }

    // adding a car
    public void addCar(Car car) {
        cars.add(car);
    }

    // listing all cars
    public void listCars() {
        for (Car c : cars) {
            System.out.println(c); // uses c.toString()
        }
    }

    public static void main(String[] args) {
        // create a car instance
        Car mini = new Car("Mini", "BMW");
        // create another car instance
        Car mustang = new Car("Mustang", "Ford");
        // create a place to store cars
        Storage garage = new Storage();
        // add the instance to the list in Storage            
        garage.addCar(mini);
        // add the other instance to the list in Storage
        garage.addCar(mustang);
        // list all cars
        garage.listCars();            
    }
}
$ javac Storage.java
$ java Storage      
Mini - BMW
Mustang - Ford
$ 

In Java a file is named after the public class it contains. It can contain several classes but only one public class. Having both classes in one file is not a good practice. It is here only used so that the code can be copied easily and get modified.

Both classes are in the same package, in the default package here. Therefore, Storage does not need to import Car. Good practice would be to use a proper package name.

CodePudding user response:

I think this code might help you with your problem.

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

public class Main {

    public static void main(String[] args) {
        Car car1 = new Car("Bmw");
        Car car2 = new Car("Audi");

        Storage storage = new Storage();
        storage.add(car1);
        storage.add(car2);

        storage.print();

    }


}


class Storage {
    private List<Object> carList = new ArrayList<>();

    public void add(Car car) {
        carList.add(car);
    }

    public void print() {
        for (var car : carList) {
            System.out.println(car);
        }
    }
}


class Car {
    private String carName;

    public Car(String carName) {
        this.carName = carName;
    }

    public String getCarName() {
        return carName;
    }



    public void setCarName(String carName) {
        this.carName = carName;
    }

    @Override
    public String toString() {
        return "Car{"  
                "carName='"   carName   '\''  
                '}';
    }
}

Also there are some problems with your class:

  1. It is better to make fields of the class to be private and have getters and setters for them.

  2. Name of the variable should start with a lowercase.

  3. Read more about lists and operations on them in Java.

  •  Tags:  
  • java
  • Related