Witam, tworzę mini projekt dotyczący pojazdów. Jestem już praktycznie na finiszu aczkolwiek nie mogę wywołać, nie wiem jak, metod dla obiektów w mojej tablicy. Posiadam klase Vehicle, która zawiera podklase Car oraz Bicycle. Po utworzeniu tablicy, chciałbym wyświetlić interfejs dotyczący ilości kół(jeżeli jest to samochód, ma wyskoczyc że ma 4 koła, jak rower to 2), oraz metodę void fuel, która wyświetla zuzycie paliwa, tylko i wyłącznie jeżeli jest to obiekt dotyczący samochodu. Oto mój kod.
Klasa vehicle:
public abstract class Vehicle implements HasWheels
{
String brand;
int weigth;
int price;
public Vehicle(String brand, int weight, int price)
{
this.brand = brand;
this.weigth = weight;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getWeigth() {
return weigth;
}
public void setWeigth(int weigth) {
this.weigth = weigth;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String toString() {
return "Vehicle brand=" + brand + ", weigth=" + weigth + ", price=" + price + "";
}
public int CompareTo(Vehicle other)//porowywanie cen
{
if (this.price < other.price) return -1;
if (this.price == other.price) return 0;
else
return 1;
}
public boolean isBrandEqual(Vehicle x, Vehicle y)//porownywanie marki
{
if (x.brand == y.brand) return true;
else
return false;
}
}
Klasa Car:
import java.lang.System;
public class Car extends Vehicle implements UseFuel, HasWheels {
double fuelConsumption;
double maxSpeed;
String fuelType;
public Car(String brand, int weight, int price, String fuelType, double fuelConsumption, double maxSpeed){
super(brand,weight,price);
fuelConsumption = 8.5;
fuelType = "Petrol";
maxSpeed = 210;
}
public double getFuelConsumption() {
return fuelConsumption;
}
public void setFuelConsumption(double fuelConsumption) {
this.fuelConsumption = fuelConsumption;
}
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
public double getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(double maxSpeed) {
this.maxSpeed = maxSpeed;
}
public String toString() {
return "Car brand=" + brand + ", weigth=" + weigth + ", price=" + price + ", fuelConsumption=" + fuelConsumption + ", fuelType=" + fuelType + ", maxSpeed=" + maxSpeed + "]";
}
public void fuel(){
System.out.println("Type of fuel: " + fuelType);
System.out.println("Fuel Consumption(per100km): " + fuelConsumption);
}
public int wheels() {
return 4;
}
}
Klasa Bicycle:
public class Bicycle extends Vehicle implements HasWheels{
String kind;
public Bicycle(String brand, int weight, int price, String kind){
super(brand,weight,price);
kind = "city";
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String toString() {
return "Bicycle brand=" + brand + ", weigth=" + weigth + ", price=" + price + ", kind=" + kind +"";
}
public int wheels() {
return 2;
}
}
Interface HasWheels:
public interface HasWheels {
public int wheels();
}
Interface UseFuel:
public interface UseFuel {
public void fuel();
}
Oraz main:
import java.util.*;
public class Program {
public static void main(String[] args) {
List<Vehicle> list=new ArrayList<Vehicle>();
list.add(new Car("Infinity", 1700, 170000, "Petrol", 7.3, 245));
list.add(new Car("Infinity", 1850, 220000, "Petrol", 9.1, 271));
list.add(new Bicycle("Cube", 10, 8000, "Mountain"));
list.add(new Bicycle("Romet", 15, 3500, "Road"));
for(Vehicle e:list)
System.out.println(e);
/*for( Vehicle e: list){
e.print();
System.out.println("Liczba kół="+e.hasWheels());
if(e instanceof Car)
System.out.println("Informacja o paliwie"+ ((Car)e).useFuel());*/ // nie działa
}
}
}
Czy jest ktoś w stanie mi pomóc to napisać?