Repository navigation
Expand file tree
/
Copy pathVehicleTester.java
More file actions
52 lines (36 loc) · 1.41 KB
/
Copy pathVehicleTester.java
File metadata and controls
52 lines (36 loc) · 1.41 KB
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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class VehicleTester {
public static void main(String[] args) {
List<Vehicle> vehicles = new ArrayList<>();
vehicles.add(new Car("Dodge", "Charger", 50, 4));
vehicles.add(new Motorcycle("Harley-Davidson", "Street 750", 30, true));
vehicles.add(new Truck("Jeep", "Gladiator", 100, 0.86));
Scanner scanner = new Scanner(System.in);
System.out.println("Available Vehicles for Rent:");
for (int i = 0; i < vehicles.size(); i++) {
System.out.printf("%d. %s%n", i + 1, vehicles.get(i));
System.out.println();
}
System.out.print("Select a vehicle by number (1-" + vehicles.size() + "): ");
int vehicleChoice = scanner.nextInt();
if (vehicleChoice < 1 || vehicleChoice > vehicles.size()) {
System.out.println("Error.. invalid choice.");
return;
}
Vehicle selectedVehicle = vehicles.get(vehicleChoice - 1);
System.out.print("Enter the number of the days you want to rent the vehicle: ");
int rentalDays = scanner.nextInt();
if (rentalDays <= 0) {
System.out.println("Error.. Input a valid amount of days. ");
return;
}
int totalCost = selectedVehicle.getrentalRate() * rentalDays;
System.out.println("\nRental Summary:");
System.out.println(selectedVehicle);
System.out.printf("Number of Days: %d%n", rentalDays);
System.out.printf("Total Cost: $%d%n", totalCost);
scanner.close();
}
}