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!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

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

Note

🏆 MVP Contest 2: The #1 ranked player on our global leaderboard by February 28th will receive a free Salesforce $200 certification voucher! Contest runs Feb 15-28. Track your position and compete for the prize! Points are only calculated for questions created and completed during the contest period.