import java.io.*;
//=================================================================================================
public class FileAllCarDealer implements Serializable {
//-------------------------------------------------------------------------------------------------
//----These can be static and every object will know
    private static final int MAX_CARS = 10;
    private static final double MARK_UP = 0.3;

    private String name;
    private FileCar[] carsOnLot;
//-------------------------------------------------------------------------------------------------
    public FileAllCarDealer() {

        int index;

        name = null;
        carsOnLot = new FileCar[MAX_CARS];
    }
//-------------------------------------------------------------------------------------------------
    public FileAllCarDealer(String newName) {

        this();
        name = newName;
    }
//-------------------------------------------------------------------------------------------------
    public void display() {

        int index;

        System.out.println(name);
        for (index = 0; index < carsOnLot.length; index++) {
            if (carsOnLot[index] != null) {
                System.out.print("   "+ (index+1) + ": ");
                System.out.println(carsOnLot[index]);
            }
        }
    }
//-------------------------------------------------------------------------------------------------
    public void buyCar(String makeAndModel,double price) {

        int index;

        index = 0;
        while (index < carsOnLot.length && carsOnLot[index] != null) {
            index++;
        }
        if (index < carsOnLot.length) {
            carsOnLot[index] = new FileCar(makeAndModel,price,
            price * (1.0 + MARK_UP));
        } else {
            System.out.println("ERROR: No more space on the lot");
        }
    }
//-------------------------------------------------------------------------------------------------
    public double sellCar(int carIndex,double actualSalePrice) {

        double profit;

        carIndex--;
        if (carsOnLot[carIndex] == null) {
            System.out.println("ERROR: No car there");
            return(0.0);
        } else {
            profit = actualSalePrice - carsOnLot[carIndex].getPurchasePrice();
            carsOnLot[carIndex] = null;
            return(profit);
        }
    }
//-------------------------------------------------------------------------------------------------
    public static boolean saveCarDealer(String fileName,FileAllCarDealer dealer) {

        ObjectOutputStream toStream = null;

        try {
            toStream = new ObjectOutputStream(new FileOutputStream(fileName));
            toStream.writeObject(dealer);
            return(true);
        } catch (IOException e) {
            System.out.println("ERROR saving " + e.getMessage());
            return(false);
        } finally {
            if (toStream != null) {
                try {
                    toStream.close();
                } catch (IOException e) {
                    System.out.println("ERROR closing " + e.getMessage());
                    return(false);
                }
            }
        }
    }
//-------------------------------------------------------------------------------------------------
    public static FileAllCarDealer loadCarDealer(String fileName) {

        ObjectInputStream fromStream = null;
        FileAllCarDealer local;

        try {
            fromStream = new ObjectInputStream(new FileInputStream(fileName));
            local = (FileAllCarDealer)fromStream.readObject();
        } catch (IOException e) {
            System.out.println("ERROR loading " + e.getMessage());
            return(null);
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
            return(null);
        } finally {
            if (fromStream != null) {
                try {
                    fromStream.close();
                } catch (IOException e) {
                    System.out.println("ERROR closing " + e.getMessage());
                    return(null);
                }
            }
        }
        return(local);
    }
//-------------------------------------------------------------------------------------------------
}
//=================================================================================================
