Task - TypeScript - Interfaces, Arrow functions, Array methods (push, findIndex), and Class concepts - Car Rental System #151
Replies: 3 comments
-
|
CODE interface Car {
id: number;
make: string;
model: string;
year: number;
isAvailable: boolean;
reserve(user: User, rentalDays: number): void;
extendReservation(user: User, additionalDays: number): void;
returnCar(user: User): void;
}
class User {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
class CarRental implements Car {
id: number;
make: string;
model: string;
year: number;
isAvailable: boolean;
reservations: { car: Car; user: User; rentalDays: number }[];
constructor(id: number, make: string, model: string, year: number) {
this.id = id;
this.make = make;
this.model = model;
this.year = year;
this.isAvailable = true;
this.reservations = [];
}
reserve(user: User, rentalDays: number): void {
if (this.isAvailable) {
this.isAvailable = false;
this.reservations.push({ car: this, user, rentalDays });
console.log(`Car ${this.id} has been reserved by ${user.name} for ${rentalDays} days.`);
} else {
console.log(`Car ${this.id} is not available for reservation.`);
}
}
extendReservation(user: User, additionalDays: number): void {
const reservation = this.reservations.find(
(r) => r.car === this && r.user === user && r.rentalDays > 0
);
if (reservation) {
reservation.rentalDays += additionalDays;
console.log(
`Reservation for car ${this.id} has been extended by ${additionalDays} days for ${user.name}.`
);
} else {
console.log(`No reservation found for car ${this.id} and user ${user.name}.`);
}
}
returnCar(user: User): void {
const reservationIndex = this.reservations.findIndex(
(r) => r.car === this && r.user === user && r.rentalDays > 0
);
if (reservationIndex !== -1) {
this.isAvailable = true;
const reservation = this.reservations[reservationIndex];
const rentalDays = reservation.rentalDays;
this.reservations.splice(reservationIndex, 1);
console.log(`Car ${this.id} has been returned by ${user.name} after ${rentalDays} days.`);
} else {
console.log(`No active reservation found for car ${this.id} and user ${user.name}.`);
}
}
getReservationDetails(user: User): { car: Car; rentalDays: number } | null {
const reservation = this.reservations.find((r) => r.car === this && r.user === user);
if (reservation) {
return { car: reservation.car, rentalDays: reservation.rentalDays };
} else {
return null;
}
}
static cars: CarRental[] = [
new CarRental(1, 'Toyota', 'Corolla', 2019),
new CarRental(2, 'Honda', 'Civic', 2020),
new CarRental(3, 'Nissan', 'Altima', 2021),
new CarRental(4, 'Ford', 'Mustang', 2022),
new CarRental(6, 'Chevrolet', 'Camaro', 2022),
];
static getAvailableCars(): Car[] {
return this.cars.filter((car) => car.isAvailable);
}
}
// Example usage
const user1 = new User(1, 'John');
const user2 = new User(2, 'Jane');
const availableCars = CarRental.getAvailableCars();
console.log('Available cars:', availableCars);
if (availableCars.length > 0) {
const car = availableCars[0];
car.reserve(user1, 3);
car.extendReservation(user1, 2);
car.returnCar(user1);
car.reserve(user2, 5);
console.log('Reservation details:', car.getReservationDetails(user2));
}
const car3 = CarRental.cars.find((car) => car.id === 3);
console.log('Car 3:', car3);
Output
Available cars: [
CarRental { id: 1, make: 'Toyota', model: 'Corolla', year: 2019, isAvailable: true, reservations: [] },
CarRental { id: 2, make: 'Honda', model: 'Civic', year: 2020, isAvailable: true, reservations: [] },
CarRental { id: 3, make: 'Nissan', model: 'Altima', year: 2021, isAvailable: true, reservations: [] },
CarRental { id: 4, make: 'Ford', model: 'Mustang', year: 2022, isAvailable: true, reservations: [] },
CarRental { id: 6, make: 'Chevrolet', model: 'Camaro', year: 2022, isAvailable: true, reservations: [] }
]
Car 1 has been reserved by John for 3 days.
Reservation for car 1 has been extended by 2 days for John.
Car 1 has been returned by John after 5 days.
Car 3 is not available for reservation.
Reservation details: { car: CarRental { id: 2, make: 'Honda', model: 'Civic', year: 2020, isAvailable: false, reservations: [ [Object] ] }, rentalDays: 5 }
Car 3: CarRental {
id: 3,
make: 'Nissan',
model: 'Altima',
year: 2021,
isAvailable: true,
reservations: [ { car: [Circular], user: [User], rentalDays: 5 } ]
}
|
Beta Was this translation helpful? Give feedback.
-
CODEinterface Car {
id: number;
make: string;
model: string;
year: number;
isAvailable: boolean;
reserve(user: User, rentalDays: number): void;
extendReservation(user: User, additionalDays: number): void;
returnCar(user: User): void;
}
class User1 {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
class CarRental implements Car {
id: number;
make: string;
model: string;
year: number;
isAvailable: boolean;
reservations: { user: User, rentalDays: number }[];
constructor(id: number, make: string, model: string, year: number, isAvailable: boolean) {
this.id = id;
this.make = make;
this.model = model;
this.year = year;
this.isAvailable = isAvailable;
this.reservations = [];
}
reserve(user: User1, rentalDays: number): void {
if (!this.isAvailable) {
console.log(`Car with ID ${this.id} is not available for reservation.`);
return;
}
this.isAvailable = false;
this.reservations.push({ user, rentalDays });
console.log(`Car with ID ${this.id} has been reserved for ${rentalDays} days by user with ID ${user.id}.`);
}
extendReservation(user: User1, additionalDays: number): void {
const reservationIndex = this.reservations.findIndex(r => r.user.id === user.id);
if (reservationIndex === -1) {
console.log(`User with ID ${user.id} has not reserved this car.`);
return;
}
this.reservations[reservationIndex].rentalDays += additionalDays;
console.log(`User with ID ${user.id} has extended the reservation of car with ID ${this.id} by ${additionalDays} days.`);
}
returnCar(user: User1): void {
const reservationIndex = this.reservations.findIndex(r => r.user.id === user.id);
if (reservationIndex === -1) {
console.log(`User with ID ${user.id} has not reserved this car.`);
return;
}
const rentalDays = this.reservations[reservationIndex].rentalDays;
const rentalCost = rentalDays * 50; // $50 per day rental fee
this.isAvailable = true;
this.reservations.splice(reservationIndex, 1);
console.log(`User with ID ${user.id} has returned car with ID ${this.id}. Rental cost: $${rentalCost}.`);
}
getReservationDetails(user: User1): { car: Car, rentalDays: number } | null {
const reservation = this.reservations.find(r => r.user.id === user.id);
if (!reservation) {
return null;
}
return { car: this, rentalDays: reservation.rentalDays };
}
static availableCars(cars: Car[]): Car[] {
return cars.filter(c => c.isAvailable);
}
}
const user12: User1 = { id: 1, name: "John" };
const user22: User1 = { id: 2, name: "Mary" };
const car1 = new CarRental(1, "Toyota", "Corolla", 2021, true);
const car2 = new CarRental(2, "Honda", "Civic", 2022, false);
const car3 = new CarRental(3, "Nissan", "Sentra", 2020, true);
car1.reserve(user12, 3);
car1.extendReservation(user12, 2);SCREENSHORT |
Beta Was this translation helpful? Give feedback.
-
|
interface Car { class User { constructor(id: number, name: string) { class CarRental implements Car { constructor(id: number, make: string, model: string, year: number) { reserve(user: User, rentalDays: number): void { extendReservation(user: User, additionalDays: number): void { returnCar(user: User): void { getReservationDetails(user: User): { car: Car; rentalDays: number } | null { static availableCars(cars: Car[]): Car[] { static reserveCar(cars: Car[], user: User, rentalDays: number): void { |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
You have been asked to develop a car rental management system that handles car reservations, rental periods, and returns. The system should allow users to reserve cars, extend the rental period, and return the cars.
Create an interface Car with the following specifications:
Properties:
Methods:
Create a class User with the following properties:
Properties:
Implement the Car interface in a class CarRental and add the following methods:
Methods:
Beta Was this translation helpful? Give feedback.
All reactions