Implement a car rental system that manages car rentals and returns. The system should track car availability, handle rental requests, and calculate rental costs. Return a map containing the rental status and cost. Cars cannot be rented if already rented or under maintenance.
Examples:
Input: operations = [
'addCar ABC123 Economy 50.00', // returns {'status': 'success', 'message': 'Car added'}
'rentCar ABC123 2024-03-01 3', // returns {'status': 'success', 'cost': 150.00}
'rentCar ABC123 2024-03-04 1', // returns {'status': 'error', 'message': 'Car not available'}
'returnCar ABC123 2024-03-03', // returns {'status': 'success', 'message': 'Car returned'}
'rentCar ABC123 2024-03-04 1' // returns {'status': 'success', 'cost': 50.00}
]
Output: [
{'status': 'success', 'message': 'Car added'},
{'status': 'success', 'cost': 150.00},
{'status': 'error', 'message': 'Car not available'},
{'status': 'success', 'message': 'Car returned'},
{'status': 'success', 'cost': 50.00}
]
Explanation: System tracks car availability and handles rental operations
Input: operations = [
'addCar XYZ789 Luxury 100.00', // returns {'status': 'success', 'message': 'Car added'}
'setMaintenance XYZ789 true', // returns {'status': 'success', 'message': 'Maintenance updated'}
'rentCar XYZ789 2024-03-01 1' // returns {'status': 'error', 'message': 'Car under maintenance'}
]
Output: [
{'status': 'success', 'message': 'Car added'},
{'status': 'success', 'message': 'Maintenance updated'},
{'status': 'error', 'message': 'Car under maintenance'}
]
Explanation: System prevents renting cars under maintenance
Method used to process your code
public static List<Map<String, Object>> processRentalOperations(List<String> operations) {
RentalSystem rentalSystem = new RentalSystem();
List<Map<String, Object>> results = new List<Map<String, Object>>();
for (String operation : operations) {
List<String> parts = operation.split(' ');
String command = parts[0];
if (command == 'addCar') {
results.add(rentalSystem.addCar(parts[1], parts[2], Decimal.valueOf(parts[3])));
} else if (command == 'rentCar') {
results.add(rentalSystem.rentCar(parts[1], parts[2], Integer.valueOf(parts[3])));
} else if (command == 'returnCar') {
results.add(rentalSystem.returnCar(parts[1], parts[2]));
} else if (command == 'setMaintenance') {
results.add(rentalSystem.setMaintenance(parts[1], Boolean.valueOf(parts[2])));
}
}
return results;
}
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.