Как взять из ArrayList переменную и назначить её в аргумент?
Задание считать с файла информацию о автомобиле и потом когда считали с файла информацию назначить всё что мы считали в аргументы,считать с файла я смог а вот назначить аргументы не смог: Считывание с файла:
import java.io.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line;
ArrayList<String> list=new ArrayList<String>();
while((line=reader.readLine())!=null){
if(!line.isEmpty()){
list.add(line);
System.out.println(line);
}}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Информация в файле:
Lamborghini Huracan 2014 1.000.000
BMW M5 2012 16.000
Bugatti Chiron 2016 3.300.000
VAZ 21099 2007 1.000
Класс автомобиль:
public class Vehicle {
private String make;
private String model;
private int yearOfManufacture;
private int cost;
public void setCost(int cost) {
this.cost = cost;
if (cost <= 0) {
throw new IllegalArgumentException("Поле вартості автомобіля дорівнює нулю або менше нуля.");
}
}
public void setMake(String make) {
this.make = make;
if (make.isEmpty()) {
throw new IllegalArgumentException("Пусте поле виробника.");
}
}
public void setModel(String model) {
this.model = model;
if (model.isEmpty()) {
throw new IllegalArgumentException("Пусте поле моделі.");
}
}
public void setYearOfManufacture(int yearOfManufacture) {
this.yearOfManufacture = yearOfManufacture;
if (yearOfManufacture<=0) {
throw new IllegalArgumentException("Поле дати виготовлення дорівнює нулю або менше нулю.");
}
}
public int getCost() {
return cost;
}
public int getYearOfManufacture() {
return yearOfManufacture;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
Vehicle(String make, String model, int cost, int yearOfManufacture) {
if (make.isEmpty()) {
throw new IllegalArgumentException("Пусте поле виробника.");
}
if (model.isEmpty()) {
throw new IllegalArgumentException("Пусте поле моделі.");
}
if (yearOfManufacture<=0) {
throw new IllegalArgumentException("Поле дати виготовлення дорівнює нулю або менше нулю.");
}
}
}
Источник: Stack Overflow на русском