1+ import Vehicle
2+ import argparse
3+ import sys
4+
5+ if sys .version_info [0 ]== 2 :
6+ input = raw_input
7+
8+ class ParkingLot :
9+ def __init__ (self ):
10+ self .capacity = 0
11+ self .slotid = 0
12+ self .numOfOccupiedSlots = 0
13+
14+ def createParkingLot (self ,capacity ):
15+ self .slots = [- 1 ]* capacity
16+ self .capacity = capacity
17+ return self .capacity
18+
19+ def getEmptySlot (self ):
20+ for i in range (len (self .slots )):
21+ if self .slots [i ]== - 1 :
22+ return i
23+
24+ def park (self ,regno ,color ):
25+ if self .numOfOccupiedSlots < self .capacity :
26+ slotid = self .getEmptySlot ()
27+ self .slots [slotid ]= Vehicle .Car (regno ,color )
28+ self .slotid = self .slotid + 1
29+ self .numOfOccupiedSlots = self .numOfOccupiedSlots + 1
30+ return slotid + 1
31+ else :
32+ return - 1
33+
34+ def leave (self ,slotid ):
35+
36+ if self .numOfOccupiedSlots > 0 and self .slots [slotid - 1 ]!= - 1 :
37+ self .slots [slotid - 1 ]= - 1
38+ self .numOfOccupiedSlots = self .numOfOccupiedSlots - 1
39+ return True
40+ else :
41+ return False
42+
43+ def status (self ):
44+ print ("Slot No.\t Registration No.\t Colour" )
45+ for i in range (len (self .slots )):
46+ if self .slots [i ]!= - 1 :
47+ print (str (i + 1 )+ "\t \t " + str (self .slots [i ].regno )+ "\t \t " + str (self .slots [i ].color ))
48+ else :
49+ continue
50+
51+ def getRegNoFromColor (self ,color ):
52+
53+ regnos = []
54+ for i in self .slots :
55+
56+ if i == - 1 :
57+ continue
58+ if i .color == color :
59+ regnos .append (i .regno )
60+ return regnos
61+
62+ def getSlotNoFromRegNo (self ,regno ):
63+
64+ for i in range (len (self .slots )):
65+ if self .slots [i ].regno == regno :
66+ return i + 1
67+ else :
68+ continue
69+ return - 1
70+
71+
72+ def getSlotNoFromColor (self ,color ):
73+
74+ slotnos = []
75+
76+ for i in range (len (self .slots )):
77+
78+ if self .slots [i ]== - 1 :
79+ continue
80+ if self .slots [i ].color == color :
81+ slotnos .append (str (i + 1 ))
82+ return slotnos
83+
84+ def show (self ,line ):
85+ if line .startswith ('create_parking_lot' ):
86+ n = int (line .split (' ' )[1 ])
87+ res = self .createParkingLot (n )
88+ print ('Created a parking lot with ' + str (res )+ ' slots' )
89+
90+ elif line .startswith ('park' ):
91+ regno = line .split (' ' )[1 ]
92+ color = line .split (' ' )[2 ]
93+ res = self .park (regno ,color )
94+ if res == - 1 :
95+ print ("Sorry, parking lot is full" )
96+ else :
97+ print ('Allocated slot number: ' + str (res ))
98+
99+ elif line .startswith ('leave' ):
100+ leave_slotid = int (line .split (' ' )[1 ])
101+ status = self .leave (leave_slotid )
102+ if status :
103+ print ('Slot number ' + str (leave_slotid )+ ' is free' )
104+
105+ elif line .startswith ('status' ):
106+ self .status ()
107+
108+ elif line .startswith ('check_car_number_with_carcolor' ):
109+ color = line .split (' ' )[1 ]
110+ regnos = self .getRegNoFromColor (color )
111+ print (', ' .join (regnos ))
112+
113+ elif line .startswith ('check_slot_number_with_carcolor' ):
114+ color = line .split (' ' )[1 ]
115+ slotnos = self .getSlotNoFromColor (color )
116+ print (', ' .join (slotnos ))
117+
118+ elif line .startswith ('check_slot_number_with_carno' ):
119+ regno = line .split (' ' )[1 ]
120+ slotno = self .getSlotNoFromRegNo (regno )
121+ if slotno == - 1 :
122+ print ("Not found" )
123+ else :
124+ print (slotno )
125+ elif line .startswith ('exit' ):
126+ exit (0 )
127+
128+ def main ():
129+
130+ parkinglot = ParkingLot ()
131+ parser = argparse .ArgumentParser ()
132+ parser .add_argument ('-f' ,action = "store" ,required = False ,dest = 'src_file' ,help = "Input File" )
133+ args = parser .parse_args ()
134+
135+ if args .src_file :
136+ with open (args .src_file )as f :
137+ for line in f :
138+ line = line .rstrip ('\n ' )
139+ parkinglot .show (line )
140+ else :
141+ while True :
142+ line = input ("$ " )
143+ parkinglot .show (line )
144+
145+ if __name__ == '__main__' :
146+ print ("----------Welcome to Low Level Parking Lot System-----------------" )
147+ print ("Type following commands to perform operations on Parking system" )
148+ print ("1. create_parking_lot n (n=capacity)" )
149+ print ("2. park car_number var_color" )
150+ print ("3. status" )
151+ print ("4. leave x (x=slot number)" )
152+ print ("5. check_car_number_with_carcolor car_color" )
153+ print ("6. check_slot_number_with_carcolor car_color" )
154+ print ("7. check_slot_number_with_carno car_no" )
155+ print ("Exit" )
156+ print ("-------------------------------------------------------------------" )
157+ main ()