package prac;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
interface Rentable {
double calculateRent(int days);
}
abstract class Vehicle implements Rentable {
String id, name;
double rentPerDay;
boolean available = true;
Vehicle(String id, String name, double rentPerDay) {
[Link] = id; [Link] = name; [Link] = rentPerDay;
}
@Override
public String toString() {
return name + " (ID: " + id + ") - $" + rentPerDay + "/day";
}
}
class Car extends Vehicle {
Car(String id, String name, double rentPerDay) { super(id, name,
rentPerDay); }
public double calculateRent(int days) { return days * rentPerDay * 1.1;
}
}
class Bike extends Vehicle {
Bike(String id, String name, double rentPerDay) { super(id, name,
rentPerDay); }
public double calculateRent(int days) { return days * rentPerDay * 0.9;
}
}
class Truck extends Vehicle {
Truck(String id, String name, double rentPerDay) { super(id, name,
rentPerDay); }
public double calculateRent(int days) { return days * rentPerDay * 1.5;
}
}
class Customer {
String id, name, contact;
int loyaltyPoints;
Customer(String id, String name, String contact) { [Link] = id;
[Link] = name; [Link] = contact; }
Customer() { this("TEMP", "Anonymous", "N/A"); }
void addPoints(int p) { loyaltyPoints += p; }
}
class VehicleNotAvailableException extends Exception {
VehicleNotAvailableException(String msg) { super(msg); }
}
class InvalidRentalPeriodException extends Exception {
InvalidRentalPeriodException(String msg) { super(msg); }
}
class Rental {
Customer customer;
Vehicle vehicle;
int days;
double cost;
Rental(Customer c, Vehicle v, int days) throws
VehicleNotAvailableException, InvalidRentalPeriodException {
if (![Link]) throw new VehicleNotAvailableException("Vehicle not
available");
if (days <= 0) throw new InvalidRentalPeriodException("Invalid rental
days");
[Link] = c; [Link] = v; [Link] = days;
[Link] = [Link](days);
[Link] = false;
[Link]((int)cost / 10);
}
}
public class LibrarySys extends JFrame {
JComboBox<String> vehicleType = new JComboBox<>(new String[]
{"Car","Bike","Truck"});
JTextField nameField = new JTextField(15), daysField = new
JTextField(5);
JTextArea outputArea = new JTextArea(10,30);
JButton rentButton = new JButton("Rent");
List<Vehicle> vehicles = [Link](
new Car("C1","Honda Civic",50),
new Bike("B1","Yamaha",20),
new Truck("T1","Volvo",100)
);
LibrarySys () {
setTitle("Smart Vehicle Rental");
setLayout(new FlowLayout());
add(new JLabel("Vehicle Type:")); add(vehicleType);
add(new JLabel("Customer Name:")); add(nameField);
add(new JLabel("Days:")); add(daysField);
add(rentButton); add(new JScrollPane(outputArea));
[Link](e -> processRental());
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
void processRental() {
try {
String type = (String) [Link]();
int days = [Link]([Link]());
String cname = [Link]().trim();
Vehicle v = [Link]()
.filter(x -> [Link]().getSimpleName().equals(type) &&
[Link])
.findFirst()
.orElseThrow(() -> new VehicleNotAvailableException("No
available " + type));
Customer c = [Link]() ? new Customer() : new
Customer([Link]().toString(), cname, "N/A");
Rental r = new Rental(c, v, days);
[Link]("Rental Success!\nVehicle: " + v + "\
nCustomer: " + [Link] + "\nDays: " + days + "\nCost: $" +
[Link]("%.2f", [Link]));
} catch (Exception ex) {
[Link]("Error: " + [Link]());
}
}
public static void main(String[] args) {
[Link](LibrarySys ::new);
}
}
output