Witajcie!
Klasa Car_Zad1, z której są tworzone obiekty.
package Lab5;
public class Car_Zad1 {
private String make;
private String model;
private double cubicCapacity;
private String type;
private String engineType;
private int productionDate;
private int firstRegistrationDate;
private double price;
// Settery
public void setMake(String make) {
this.make=make;
}
public void setModel(String model) {
this.model=model;
}
public void setCubicCapacity(double cubicCapacity) {
this.cubicCapacity=cubicCapacity;
}
public void setType(String type) {
this.type=type;
}
public void setEngineType(String engineType) {
this.engineType=engineType;
}
public void setProductionDate(int productionDate) {
this.productionDate=productionDate;
}
public void setFirstRegistrationDate(int firstRegistrationDate) {
this.firstRegistrationDate=firstRegistrationDate;
}
public void setPrice(double price) {
this.price=price;
}
// Gettery
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
public double getCubicCapacity() {
return this.cubicCapacity;
}
public String getType() {
return this.type;
}
public String getEngineType() {
return this.engineType;
}
public int getProductionDate() {
return this.productionDate;
}
public int getFirstRegistrationDate(){
return this.firstRegistrationDate;
}
public double getPrice() {
return this.price;
}
//czy posiada jeszcze gwarancje ZAD2
public String carHaveGuarantee() {
if((2018-firstRegistrationDate)>3) {
return "Auto nie ma gwarancji!";
} else {
return "Auto ma gwarancję!";
}
}
//konstruktor ZAD3
public Car_Zad1(String make, String model, double cubicCapacity, String type, String engineType, int productionDate, int firstRegistrationDate, double price) {
setMake(make);
setModel(model);
setCubicCapacity(cubicCapacity);
setType(type);
setEngineType(engineType);
setProductionDate(productionDate);
setFirstRegistrationDate(firstRegistrationDate);
setPrice(price);
/*
System.out.println(this.getMake());
System.out.println(this.getModel());
System.out.println(this.getCubicCapacity());
System.out.println(this.getType());
System.out.println(this.getEngineType());
System.out.println(this.getProductionDate());
System.out.println(this.getFirstRegistrationDate());
System.out.println(this.getPrice());
System.out.println(this.carHaveGuarantee());*/
}
public Car_Zad1() {
}
}
Klasa Autokomis, w której tworzone są obiekty klasy Car_Zad1
package Lab5;
import java.util.ArrayList;
public class AutoKomis {
//stworzyc liste obiektow klasy samochod ;; ArrayList<Samochod> listaSamochodow = new Samochod;
//dodanie elementu do listy ;; listaSamochodów(new Samochod("Opel", "Astra", ...));
private ArrayList<Car_Zad1> carList;
public AutoKomis() {
carList = new ArrayList<Car_Zad1>();
carList.add(new Car_Zad1("Renault", "Clio", 1.6, "Hatchback", "PB + LPG", 2006, 2006, 10000));
//System.out.println("\r\n");
carList.add(new Car_Zad1("Volkswagen", "Golf", 1.9, "Kombi", "ON", 2008, 2008, 14000));
//System.out.println("\r\n");
carList.add(new Car_Zad1("Ford", "Focus", 2.0, "Hatchback", "PB", 2005, 2006, 9000));
//System.out.println("\r\n");
carList.add(new Car_Zad1("Seat", "Leon", 1.6, "Hatchback", "PB + LPG", 2005, 2006, 9000));
//System.out.println("\r\n");
}
public ArrayList<Car_Zad1> getList() {
return this.carList;
}
public Car_Zad1 getSpecificElement(int number) {
return carList.get(number);
}
}
Klasa Main, w której próbuję zwrócić ArrayList z klasy Autokomis
package Lab5;
public class Main {
public static void main(String args[]) {
AutoKomis car = new AutoKomis();
System.out.println(car.getSpecificElement(0));
}
}
Chcę otrzymać obiekt 0 ArrayList (czyli obiekt klasy Car_Zad1), lecz zamiast tego otrzymuję Lab5.Car_Zad1@15db9742, czyli adres w pamięci, gdzie jest przetrzymywana moja Renówka.
Jak poprawnie zwrócić za pomocą ArrayList przykładowo samą markę, model lub cały obiekt? Uprzedzając pytania, Pan prowadzący zajęcia zażyczył sobie ArrayList. Ktoś przy okazji mógłby wytłumaczyć dlaczego powinienem skorzystać z ArrayList?