This program starts off in the FleetInterface class by asking the user for a file (run()). The buildFleet() method reads the file then calls the Vehicle to the Fleet class by calling the addVehicle() method and in that method, it sets the new Vehicle object into the vehicle array.
After all that’s done, a user menu comes up asking if they would like to 1.) Add a Vehicle, 2.) Delete a Vehicle.
Let’s say they add a new Vehicle. The requirement is to have the user manually enter data about that vehicle (All the same info that was in the file in the beginning). The problem is that this option also calls addVehicle(). If I program in the addVehicle() method some statements like “Please enter the model of your vehicle:”, that will also show up when the program first starts and calls addVehicle().
The tricky part – I am not allowed to create any new public methods (only private), and I cannot add any new class level data.
My Fleet class has 2 constructors: 1 is blank (Not allowed to set anything here.) And 1 has a parameter value of File (Used for reading the original file).
So to sum it up, I need a way for the program to start by reading the values in a file, calling addVehicle(), then also allow the user to enter in a vehicle manually via Scanner.. while also calling addVehicle()
Here is my code:
FleetInterfaceApp:
public void run() throws FileNotFoundException
{
File file = new File(getFile());
fleet = new Fleet(file);
buildFleet(file);
}
private void buildFleet(File file) throws FileNotFoundException
{
fleet = new Fleet(file);
fleet.addVehicle(Honda);
userMenu(file, fleet);
}
private void userMenu(File file, Fleet fleet) throws FileNotFoundException
{
int choice = 0;
Scanner input = new Scanner(System.in);
this.createMenu();
choice = this.menu.getChoice();
switch(choice)
{
case 1:
fleet.addVehicle(Honda);
break;
}
}
Fleet:
Class Level data (cannot change):
Vehicle[] vehicles = new Vehicle[4];
File file;
addVehicle:
public void addVehicle(Vehicle Honda[]) throws FileNotFoundException
{
Scanner reader = new Scanner(file);
if(canAddVehicle() == true)
{
for(int i = 0; i < vehicles.length; i++)
{
if(vehicles[i] == null)
{
Honda[i] = new Vehicle();
Honda[i].readRecord(reader);
vehicles[i] = Honda[i];
reader.close();
break;
}
}
System.out.println("Vehicle Added!");
}
else
{
System.out.println("You can not add more than 4 vehicles.");
}
}
You can write out the user input to a temp file and then set the file attribute in your fleet object to the temp file before you call addVehicle. The file attribute is accessible to other classes because it is scoped package private by default. This means that any classes in the same package can access it. If FleetInterfaceApp is in the same package then it can already do this.
Here is some example code based off of the code provided in the question. This needs extra work before it will run.