如何将主类中的对象添加到另一个类中的ArrayList中,Java。

huangapple go评论79阅读模式
英文:

how to add the objects from main class to ArrayList in other class Java

问题

我有这个类,它应该有3个包含其他类对象的列表。
现在,我该如何使用以下方法从主类中填充列表?

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

public class FcomCarRentalSystem {

    public List<Customer> customers = new ArrayList<Customer>();
    public List<Car> cars = new ArrayList<Car>();
    public List<Rental> rental = new ArrayList<Rental>();

    // 添加车辆
    public String addCar(Car tcar) {
        cars.add(tcar);
        String state = "添加车辆成功";
        return state;
    }
}

注意,CarCustomersRental 都是类,它们没有问题。
这是主类。当我添加以下车辆时,应该显示消息"添加成功"。但是在我的主类中没有显示任何内容。
我在这里做错了什么?

public class App {
    public static void main(String[] args) {
        FcomCarRentalSystem company = new FcomCarRentalSystem();
        Car nii1 = new Car("QA33145", "Nissan", CarType.SEDAN, true);
        company.addCar(nii1);
    }
}

任何帮助将不胜感激。
您可以在这些图片中看到类图中所需的内容。

--
如何将主类中的对象添加到另一个类中的ArrayList中,Java。

如何将主类中的对象添加到另一个类中的ArrayList中,Java。

英文:

I have this class which is supposed to have 3 lists of objects of other classes.
Now, how can I fill the list from the main class by using the following method
from the class?

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

public class FcomCarRentalSystem {

	public List&lt;Customer&gt; customers = new ArrayList&lt;Customer&gt;();
	public List&lt;Car&gt; cars = new ArrayList&lt;Car&gt;();
	public List&lt;Rental&gt; rental = new ArrayList&lt;Rental&gt;();


	// Add Car
	public String addCar(Car tcar) {
		cars.add(tcar);
		String state =  &quot;added car successfully&quot;;
		return state;
	}

Note that Car, Customers, Rental are classes and they have no problem.
Here is the main class. When I add the following car it is supposed to show the message "added successfully". But nothing shows in my main class.
What did I do wrong here?

    public class App {
    	public static void main(String[] args) {
    		FcomCarRentalSystem company = new FcomCarRentalSystem ();
    		 Car nii1 = new Car(&quot;QA33145&quot;,&quot;Nissan&quot;,CarType.SEDAN,true);
    		company.addCar(nii1);
        }
    }

Any help would be appreciated.
You can see in these images what is required with the class diagram.

--
如何将主类中的对象添加到另一个类中的ArrayList中,Java。

如何将主类中的对象添加到另一个类中的ArrayList中,Java。

答案1

得分: 2

public static void main(String[] args) {
    FcomCarRentalSystem company = new FcomCarRentalSystem ();
    System.out.println(company.addCar(new Car()));
    System.out.println(company.addCustomer(new Customer()));
    System.out.println(company.addRental(new Rental()));
}
英文:
public static void main(String[] args) {
    FcomCarRentalSystem company = new FcomCarRentalSystem ();
    System.out.println(company.addCar(new Car()));
    System.out.println(company.addCustomer(new Customer()));
    System.out.println(company.addRental(new Rental()));
}

huangapple
  • 本文由 发表于 2020年10月23日 17:58:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64497852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定