import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


public class MyCarRental implements CarRental{
  final Set<Car> cars = new HashSet<Car>();
  final Map<Car,Contract> rentedCars =new HashMap<Car,Contract>();
  final Set<Customer> customers = new HashSet<Customer>();
  final Map<Customer,Set<Contract>> currentCustomers = new HashMap<Customer,Set<Contract>>();
  final Set<Contract> contracts = new HashSet<Contract>();
  public void addCar(Car c){
   cars.add(c);
  }
  public void addCustomer(Customer c){
    customers.add(c);
  }
  
  public void addContract(Contract c) throws InvalidOperationException {
    if (rentedCars.containsKey(((AbstractContract) c).getCar())) {
      throw new InvalidOperationException();
    }
    rentedCars.put(((AbstractContract) c).getCar(),c);
    contracts.add(c);
    Set<Contract> set = currentCustomers.get(((AbstractContract) c).getCustomer());
    if (set==null){
      set = new HashSet<Contract>();
      currentCustomers.put(((AbstractContract) c).getCustomer(),set);
    }
    set.add(c);
  }
  
  public String contractsToString(){
    StringBuilder sb = new StringBuilder();
    for (Contract c : contracts){
      sb.append(c.toString());
    }
    return sb.toString();
  }
  
}
