Skip to content

Instantly share code, notes, and snippets.

@mohsenk
Created June 28, 2020 19:26
Show Gist options
  • Save mohsenk/0d7ea7423898a2905239abaaa8d50e2c to your computer and use it in GitHub Desktop.
Save mohsenk/0d7ea7423898a2905239abaaa8d50e2c to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class App {
public static void main(String[] args) throws Exception {
var ddd = new ParkingGarageManager(new Config(5, 5, 5));
var ticketIds = new ArrayList<String>();
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 1, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 1, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 2, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 3, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Bagher", 4, 2, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 1, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 1, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 2, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Mohsen", 3, 0, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Hadi", 2, 1, 1)));
ticketIds.add(ddd.RequestParkingSlot(new Requester("Bagher", 4, 2, 1)));
for (String ticketId : ticketIds) {
ddd.LeaveParkingGarage(ticketId);
}
System.out.println(ddd.mediumLots);
System.out.println(ddd.smallLots);
System.out.println(ddd.largeLots);
}
public static class ParkingGarageManager {
Config config;
Requester[] smallLots;
Requester[] mediumLots;
Requester[] largeLots;
public ParkingGarageManager(Config config) {
this.config = config;
this.smallLots = new Requester[config.CapacitySmall];
this.mediumLots = new Requester[config.CapacityMedium];
this.largeLots = new Requester[config.CapacityLarge];
}
public String RequestParkingSlot(Requester requester) throws Exception {
Requester[] currentLots;
String ticketId = null;
if (requester.Size == 0) { // small
currentLots = smallLots;
ticketId = "S";
} else if (requester.Size == 1) { // Medium
currentLots = mediumLots;
ticketId = "M";
} else if (requester.Size == 2) { // Large
currentLots = largeLots;
ticketId = "L";
} else {
throw new Exception("Invaldi size");
}
for (Integer i = 0; i < currentLots.length; i++) {
if (currentLots[i] == null && !String.valueOf(i + 1).contains(requester.UnluckyNumber.toString())) {
currentLots[i] = requester;
ticketId += i + 1;
return ticketId;
}
}
return null;
}
public Boolean LeaveParkingGarage(String ticket) throws Exception {
var type = ticket.substring(0, 1);
var number = Integer.parseInt(ticket.substring(1)) - 1;
Requester[] currentLots;
if (type.equalsIgnoreCase("S")) {
currentLots = smallLots;
} else if (type.equalsIgnoreCase("M")) {
currentLots = mediumLots;
} else if (type.equalsIgnoreCase("L")) {
currentLots = largeLots;
} else {
throw new Exception("Invaldi size");
}
if (currentLots[number] == null) {
return false;
} else {
currentLots[number] = null;
return true;
}
}
}
public static class Requester {
public String Id;
public Integer UnluckyNumber;
public Integer Size;
public Integer MaxVehicles;
public Requester(String id, int unluckyNumber, int size, int maxVehicles) {
Id = id;
UnluckyNumber = unluckyNumber;
Size = size;
MaxVehicles = maxVehicles;
}
}
public static class Config {
Integer CapacitySmall;
Integer CapacityMedium;
Integer CapacityLarge;
Integer Capacity;
public Config(int capacitySmall, int capacityMedium, int capacityLarge) {
CapacitySmall = capacitySmall;
CapacityMedium = capacityMedium;
CapacityLarge = capacityLarge;
this.Capacity = capacityLarge + capacityMedium + capacitySmall;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment