-
Notifications
You must be signed in to change notification settings - Fork 0
/
FleetOfVehicles.java
143 lines (138 loc) · 3.98 KB
/
FleetOfVehicles.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.Scanner;
//A class that keeps a fleet of different types of vehicles
public class FleetOfVehicles {
//An array of vehicles
private Vehicle[] fleet;
public static final int MAX_VEHICLES = 100;
public FleetOfVehicles()
{
fleet = new Vehicle[MAX_VEHICLES];
}
public Vehicle[] getFleet()
{
return this.fleet;
}
//Adds a new vehicle to the first empty spot in the fleet array
public void addVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] == null)
{
fleet[i] = aV;
return;
}
}
//If it reaches here the array is full
System.out.println("The fleet of vehicles is full!");
}
//Removes a vehicle. It searches through the array for a vehicle that is equal to
//the parameter and once it is found that value is set to null
public void removeVehicle(Vehicle aV)
{
for(int i=0;i<fleet.length;i++)
{
if(fleet[i] != null && fleet[i].equals(aV))
{
fleet[i] = null;
return;
}
}
//If it reaches here then the vehicle was not found
System.out.println("The vehicle was not found");
}
//A static keyboard to be used throughout the frontend
static Scanner keyboard;
//Entry point of the program
public static void main(String[] args)
{
keyboard = new Scanner(System.in);//Construct the keyboard
System.out.println("Welcome to the fleet manager");
FleetOfVehicles fOfV = new FleetOfVehicles();//Creates a new instance of the FleetOfVehicles to be used
boolean quit = false;
while(!quit)//Runs until the user quits
{
printOptions();
int pick = keyboard.nextInt();
keyboard.nextLine();
switch(pick)
{
case 1: //Add vehicle
fOfV.addVehicle(makeAVehicleDialog());
break;
case 2: //Remove vehicle
fOfV.removeVehicle(makeAVehicleDialog());
break;
case 9:
quit = true;
break;
default:
System.out.println("Invalid input");
}
System.out.println("The Fleet currently");
printFleet(fOfV);
}
System.out.println("Goodbye");
}
//Displays the options to the user
public static void printOptions()
{
System.out.println("Enter 1: to add a Vehicle\nEnter 2: to remove a Vehicle\nEnter 9 to quit");
}
//Returns an instance of a vehicle based on user input
public static Vehicle makeAVehicleDialog()
{
Vehicle retV;
int pick = 0;
System.out.println("Enter 1: if it is a car\nEnter 2: if it is a truck\nEnter 3: if it is unclassified");
pick = keyboard.nextInt();
keyboard.nextLine();
while(pick != 1 && pick != 2 && pick != 3)
{
System.out.println("Invalid choice pick again");
pick = keyboard.nextInt();
keyboard.nextLine();
}
System.out.println("Enter the manufacturer's name");
String manuName = keyboard.nextLine();
System.out.println("Enter the number of cylinders");
int cylinders = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter the owner's name");
String ownersName = keyboard.nextLine();
switch(pick)
{
case 1://Constructing a car
System.out.println("Enter the car's gas mileage");
double mileage = keyboard.nextDouble();
keyboard.nextLine();
System.out.println("Enter the number of passengers");
int passengers = keyboard.nextInt();
keyboard.nextLine();
retV = new Car(manuName,cylinders,ownersName,mileage,passengers);
break;
case 2://Constructing a truck
System.out.println("Enter the truck's load capacity");
double loadCap = keyboard.nextDouble();
keyboard.nextLine();
System.out.println("Enter the truck's towing capacity");
double towCap = keyboard.nextDouble();
keyboard.nextLine();
retV = new Truck(manuName,cylinders,ownersName,loadCap,towCap);
break;
default:
retV = new Vehicle(manuName,cylinders,ownersName);
}
return retV;
}
public static void printFleet(FleetOfVehicles fV)
{
for(Vehicle v : fV.getFleet())
{
if(v == null)
continue;
System.out.println(v);
System.out.println();
}
}
}