DamSi

Untitled

Aug 17th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.Scanner;
  5. import java.util.Set;
  6. import java.util.TreeMap;
  7.  
  8. class ParkingSector{
  9.     private String name;
  10.     private int broj_mesta;
  11.     private ArrayList<String> avtomobili;
  12.    
  13.     public ParkingSector( String name, int br_mesta){
  14.         this.name=name;
  15.         this.broj_mesta=br_mesta;
  16.         avtomobili= new ArrayList<>();
  17.         for(int i=0;i<br_mesta;i++){
  18.             avtomobili.add(null);
  19.         }
  20.     }
  21.     public void addAvtomobil(String registration, int place) throws InvalidSpotNumberException, SpotTakenException{
  22.         if(place< 0 || place > broj_mesta ) throw new InvalidSpotNumberException("Invalid spot number");
  23.         if(avtomobili.get(place-1) != null) throw new SpotTakenException("Spot is taken already!");
  24.         avtomobili.set(place-1, registration);
  25.     }
  26.     public String getName() {
  27.         return name;
  28.     }
  29.     public int getBroj_mesta() {
  30.         return broj_mesta;
  31.     }
  32.     public int getZafateniMesta(){
  33.         int brojac=0;
  34.         for(String a: avtomobili){
  35.             if(a!=null){
  36.                 brojac++;
  37.             }
  38.         }
  39.         return brojac;
  40.     }
  41.    
  42. }
  43.  
  44. class CityParking{
  45.     private String grad;
  46.     private TreeMap<String, ParkingSector> sectors;
  47.     private HashMap<String,String> regIme;
  48.     private HashMap<String,Integer> regPoz;
  49.    
  50.     public CityParking(String name){
  51.         grad=name;
  52.         sectors= new TreeMap<>();
  53.         regIme= new HashMap<>();
  54.         regPoz= new HashMap<>();
  55.     }
  56.     public void createSectors(String sectorNames[], int [] counts){
  57.         for(int i=0;i<sectorNames.length;i++){
  58.             ParkingSector p = new ParkingSector(sectorNames[i],counts[i]);
  59.             sectors.put(sectorNames[i], p);
  60.         }
  61.     }
  62.     public void addCar(String sectorName, int spotNumber, String registration) throws NoSuchSectorException, InvalidSpotNumberException, SpotTakenException{
  63.         if(!sectors.containsKey(sectorName)) throw new NoSuchSectorException("No sector with name "+ sectorName);
  64.         sectors.get(sectorName).addAvtomobil(registration, spotNumber);
  65.         regIme.put(registration, sectorName);
  66.         regPoz.put(registration, spotNumber);
  67.     }
  68.     public void findCar(String registration) throws CarNotFoundException{
  69.         if(!regIme.containsKey(registration)) throw new CarNotFoundException("Car with RN " + registration + " not found");
  70.         System.out.println(regIme.get(registration) +" : " + regPoz.get(registration));
  71.     }
  72.     public String toString(){
  73.         StringBuilder str= new StringBuilder();
  74.         str.append(grad +"\n");
  75.         Set<String> set= sectors.keySet();
  76.         Iterator<String> it= set.iterator();
  77.         while(it.hasNext()){
  78.             ParkingSector p= sectors.get(it.next());
  79.             str.append(p.getName() + " : " + p.getZafateniMesta()+"/"+p.getBroj_mesta()+"\n");
  80.         }
  81.         return str.toString();
  82.     }
  83. }
  84. class CarNotFoundException extends Exception{
  85.     public CarNotFoundException(String s){
  86.         super(s);
  87.     }
  88. }
  89. class NoSuchSectorException extends Exception{
  90.     public NoSuchSectorException(String s){
  91.         super(s);
  92.     }
  93. }
  94. class InvalidSpotNumberException extends Exception{
  95.     public InvalidSpotNumberException(String s){
  96.         super(s);
  97.     }
  98. }
  99. class SpotTakenException extends Exception{
  100.     public SpotTakenException(String s){
  101.         super(s);
  102.     }
  103. }
  104. public class ParkingTest {
  105.     public static void main(String[] args) {
  106.         Scanner scanner = new Scanner(System.in);
  107.         int n = scanner.nextInt();
  108.         scanner.nextLine();
  109.         String[] sectorNames = new String[n];
  110.         int[] counts = new int[n];
  111.         for (int i = 0; i < n; ++i) {
  112.             String[] parts = scanner.nextLine().split(" ");
  113.             sectorNames[i] = parts[0];
  114.             counts[i] = Integer.parseInt(parts[1]);
  115.         }
  116.         String name = scanner.nextLine();
  117.         CityParking cityParking = new CityParking(name);
  118.         cityParking.createSectors(sectorNames, counts);
  119.         n = scanner.nextInt();
  120.         scanner.nextLine();
  121.         for (int i = 0; i < n; ++i) {
  122.             String[] parts = scanner.nextLine().split(" ");
  123.             String sectorName = parts[0];
  124.             int spotNumber = Integer.parseInt(parts[1]);
  125.             String registrationNumber = parts[2];
  126.             try {
  127.                 cityParking.addCar(sectorName, spotNumber, registrationNumber);
  128.             } catch (InvalidSpotNumberException e) {
  129.                 System.out.println(e.getMessage());
  130.             } catch (SpotTakenException e) {
  131.                 System.out.println(e.getMessage());
  132.             } catch (NoSuchSectorException e) {
  133.                 System.out.println(e.getMessage());
  134.             }
  135.         }
  136.         n = scanner.nextInt();
  137.         scanner.nextLine();
  138.         for (int i = 0; i < n; ++i) {
  139.             String registrationNumber = scanner.nextLine();
  140.             try {
  141.                 cityParking.findCar(registrationNumber);
  142.             } catch (CarNotFoundException e) {
  143.                 System.out.println(e.getMessage());
  144.             }
  145.         }
  146.         System.out.println(cityParking);
  147.     }
  148. }
  149.  
  150. // Vasiot kod ovde
Advertisement
Add Comment
Please, Sign In to add comment