Witam, robie projekt wypożyczalni pojazdów i mam problem z metodą wiświetlająca wszystkie pojazdy (displayVehicles), to znaczy po wywołaniu jej nic się nie dzieje dodam, że dopiero zaczynam przygodę z programowaniem. Z góry dziękuję za pomoc.
Klasa Vehicle.java
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
public abstract class Vehicle {
String make, model, vin;
double rate;
boolean reservation;
double dialyCost, weeklyCost, monthlyCost;
public Vehicle(String make, String model, double rate, String vin, boolean reservation){
this.make = make;
this.model = model;
this.rate = rate;
this.vin = vin;
this.reservation = reservation = false;
}
Vehicle(){
}
public double getDialyCost() {
return dialyCost;
}
public void setDialyCost(double dialyCost) {
this.dialyCost = dialyCost;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getMonthlyCost() {
return monthlyCost;
}
public void setMonthlyCost(double monthlyCost) {
this.monthlyCost = monthlyCost;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public boolean isReservation() {
return reservation;
}
public void setReservation(boolean reservation) {
this.reservation = reservation;
}
public String getVin() {
return vin;
}
public void setVin(String vin) {
this.vin = vin;
}
public double getWeeklyCost() {
return weeklyCost;
}
public void setWeeklyCost(double weeklyCost) {
this.weeklyCost = weeklyCost;
}
@Override
public String toString() {
return make + " " + model + " " + rate + " " + vin + " " + reservation;
}
public void save(){
try{
Vehicle vehicle = new Vehicle() {};
File file = new File("Vehicles.txt");
if(!file.exists()){
file.createNewFile();
}
try(PrintWriter output = new PrintWriter(new FileWriter("Vehicle.txt", true))){
output.printf("%s\r\n", vehicle);
}catch(Exception e){}
}catch (Exception e){
e.printStackTrace();
}
}
}
przykładowo klasa Car.java
public class Car extends Vehicle{
public Car(String make, String model, double rate, String vin, boolean reservation){
super(make, model, rate, vin, reservation);
reservation = false;
}
public void setDialyCost(){
dialyCost = 80;
}
public void setWeeklyCost(){
weeklyCost = 480;
}
public void setMonthlyCost(){
monthlyCost = 1600;
}
}
Klasa TestApi.java
public class TestApi {
public static void main(String[] args){
ArrayList<Vehicle> vehiclelist = new ArrayList<>();
vehiclelist.add(new Car("BMW", "550xi", 2.8, "TES445433242", false));
vehiclelist.add(new Motorcycle("Yamaha", "R1", 3, "RTS232132", false));
vehiclelist.add(new Bike("Giant", "Cross", 1.6, "RT43232232", false));
Scanner input = new Scanner(System.in);
int choice;
do{
displayMenu();
choice = input.nextInt();
if(choice >= 1 && choice <=6){
run(choice);
}else{
System.out.println("Error bad type, please reenter");
}
}while(choice != 6);
}
public static void displayMenu(){
System.out.println("******MENU******");
System.out.println("1 - Add Vehicle");
System.out.println("2 - Display Vehicles");
System.out.println("3 - Display vehicle rates");
System.out.println("4 - Make reservation");
System.out.println("5 - Cancel reservation");
System.out.println("6 - Exit");
}
public static void run(int choice){
Scanner input = new Scanner(System.in);
switch(choice){
case 1:
addVehicle();
break;
case 2:
displayVehicles();
break;
}
}
public static void addVehicle(){
Scanner input = new Scanner(System.in);
ArrayList<Vehicle> vehiclelist = new ArrayList<>();
Vehicle vehicle = new Vehicle() {};
System.out.println("Enter vehicle type: Car (1), Motorcycle (2) or Bike (3)");
int type = input.nextInt();
switch(type){
case 1:
System.out.println("Enter make: ");
vehicle.make = input.next();
System.out.println("Enter model: ");
vehicle.model = input.next();
System.out.println("Enter car rate: ");
vehicle.rate = input.nextDouble();
System.out.println("Enter Vin number: ");
vehicle.vin = input.next();
vehiclelist.add(new Car(vehicle.make,vehicle.model, vehicle.rate, vehicle.vin, vehicle.reservation));
break;
case 2:
System.out.println("Enter make: ");
vehicle.make = input.next();
System.out.println("Enter model: ");
vehicle.model = input.next();
System.out.println("Enter motorcycle rate: ");
vehicle.rate = input.nextDouble();
System.out.println("Enter Vin number: ");
vehicle.vin = input.next();
vehiclelist.add(new Motorcycle(vehicle.make,vehicle.model, vehicle.rate, vehicle.vin, vehicle.reservation));
break;
case 3:
System.out.println("Enter make: ");
vehicle.make = input.next();
System.out.println("Enter model: ");
vehicle.model = input.next();
System.out.println("Enter bike rate: ");
vehicle.rate = input.nextDouble();
System.out.println("Enter Vin number: ");
vehicle.vin = input.next();
vehiclelist.add(new Bike(vehicle.make,vehicle.model, vehicle.rate, vehicle.vin, vehicle.reservation));
break;
}
System.out.println("Vehicle added");
try{
vehicle.save();
}catch(Exception e){
e.printStackTrace();
}
}
public static void displayVehicles(){
ArrayList<Vehicle> vehiclelist = new ArrayList<>();
for(Vehicle v : vehiclelist){
System.out.println(v);
}
}
}