Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeMap;
- class ParkingSector{
- private String name;
- private int broj_mesta;
- private ArrayList<String> avtomobili;
- public ParkingSector( String name, int br_mesta){
- this.name=name;
- this.broj_mesta=br_mesta;
- avtomobili= new ArrayList<>();
- for(int i=0;i<br_mesta;i++){
- avtomobili.add(null);
- }
- }
- public void addAvtomobil(String registration, int place) throws InvalidSpotNumberException, SpotTakenException{
- if(place< 0 || place > broj_mesta ) throw new InvalidSpotNumberException("Invalid spot number");
- if(avtomobili.get(place-1) != null) throw new SpotTakenException("Spot is taken already!");
- avtomobili.set(place-1, registration);
- }
- public String getName() {
- return name;
- }
- public int getBroj_mesta() {
- return broj_mesta;
- }
- public int getZafateniMesta(){
- int brojac=0;
- for(String a: avtomobili){
- if(a!=null){
- brojac++;
- }
- }
- return brojac;
- }
- }
- class CityParking{
- private String grad;
- private TreeMap<String, ParkingSector> sectors;
- private HashMap<String,String> regIme;
- private HashMap<String,Integer> regPoz;
- public CityParking(String name){
- grad=name;
- sectors= new TreeMap<>();
- regIme= new HashMap<>();
- regPoz= new HashMap<>();
- }
- public void createSectors(String sectorNames[], int [] counts){
- for(int i=0;i<sectorNames.length;i++){
- ParkingSector p = new ParkingSector(sectorNames[i],counts[i]);
- sectors.put(sectorNames[i], p);
- }
- }
- public void addCar(String sectorName, int spotNumber, String registration) throws NoSuchSectorException, InvalidSpotNumberException, SpotTakenException{
- if(!sectors.containsKey(sectorName)) throw new NoSuchSectorException("No sector with name "+ sectorName);
- sectors.get(sectorName).addAvtomobil(registration, spotNumber);
- regIme.put(registration, sectorName);
- regPoz.put(registration, spotNumber);
- }
- public void findCar(String registration) throws CarNotFoundException{
- if(!regIme.containsKey(registration)) throw new CarNotFoundException("Car with RN " + registration + " not found");
- System.out.println(regIme.get(registration) +" : " + regPoz.get(registration));
- }
- public String toString(){
- StringBuilder str= new StringBuilder();
- str.append(grad +"\n");
- Set<String> set= sectors.keySet();
- Iterator<String> it= set.iterator();
- while(it.hasNext()){
- ParkingSector p= sectors.get(it.next());
- str.append(p.getName() + " : " + p.getZafateniMesta()+"/"+p.getBroj_mesta()+"\n");
- }
- return str.toString();
- }
- }
- class CarNotFoundException extends Exception{
- public CarNotFoundException(String s){
- super(s);
- }
- }
- class NoSuchSectorException extends Exception{
- public NoSuchSectorException(String s){
- super(s);
- }
- }
- class InvalidSpotNumberException extends Exception{
- public InvalidSpotNumberException(String s){
- super(s);
- }
- }
- class SpotTakenException extends Exception{
- public SpotTakenException(String s){
- super(s);
- }
- }
- public class ParkingTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- scanner.nextLine();
- String[] sectorNames = new String[n];
- int[] counts = new int[n];
- for (int i = 0; i < n; ++i) {
- String[] parts = scanner.nextLine().split(" ");
- sectorNames[i] = parts[0];
- counts[i] = Integer.parseInt(parts[1]);
- }
- String name = scanner.nextLine();
- CityParking cityParking = new CityParking(name);
- cityParking.createSectors(sectorNames, counts);
- n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String[] parts = scanner.nextLine().split(" ");
- String sectorName = parts[0];
- int spotNumber = Integer.parseInt(parts[1]);
- String registrationNumber = parts[2];
- try {
- cityParking.addCar(sectorName, spotNumber, registrationNumber);
- } catch (InvalidSpotNumberException e) {
- System.out.println(e.getMessage());
- } catch (SpotTakenException e) {
- System.out.println(e.getMessage());
- } catch (NoSuchSectorException e) {
- System.out.println(e.getMessage());
- }
- }
- n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String registrationNumber = scanner.nextLine();
- try {
- cityParking.findCar(registrationNumber);
- } catch (CarNotFoundException e) {
- System.out.println(e.getMessage());
- }
- }
- System.out.println(cityParking);
- }
- }
- // Vasiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment