-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParkingLotSystem.java
More file actions
179 lines (126 loc) · 3.39 KB
/
Copy pathParkingLotSystem.java
File metadata and controls
179 lines (126 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package Test;
import java.util.*;
public class ParkingLotSystem {
/*
* 1. at the entrance get ticket
* 2. find an available parking lot
* 3. if parking lot is full monitor it outside of the parking lot
* 4. pay at the exit , or at the payment machine
* */
public enum SpotType{
SMALL(1), MEDIUM(2), LARGE(3);
int value;
private SpotType(int i) {
this.value = i;
}
public int getValue() {
return value;
}
}
public class Spot{
public SpotType spotType;
public int available =1; // available at the initial state if it reserved turns -1
public Floor floor;
public Spot(SpotType st,Floor floor) {
this.spotType = st;
this.floor = floor;
}
public void updateState() {
available*=-1;
}
public Floor getFloor() {
return floor;
}
public SpotType getSpot() {
return spotType;
}
}
public class Floor {// if the place is open spaced then the floor can not be greater than 0
int floor;
List<Spot> spots;
int totalCapacity;
int availableSpotNumber;
public void addSpot(Spot spot) {
spots.add(spot);
}
List<Observer> observers = new ArrayList<Observer>();
public void attach(Observer o) {
observers.add(o);
}
public void updateState(Spot spot) {
spot.updateState();
notifyObservers();
}
private void notifyObservers() {
for(Observer o : observers) {
o.update(this);
}
}
public int getFloor() {
return floor;
}
}
public class Building{
Adress adress;
List<Floor> floors = new ArrayList<Floor>();
public Building(Floor f) {
floors.add(f);
}
}
public class Adress{
String city;
String street;
String avaenue;
}
public class Reservation{
public List<Spot> availableSpots(SearchSpot search){
return search.getSpots();
}
public ParkingTicket bookSpot(SpotType type) throws ParkingFullException {
SearchSpot s = new NearestSpotsToTheEntrance();// to initialization we can use factory pattern
List<Spot> spots = availableSpots(s);
for(Spot spot : spots) {
synchronized(spot){
if(spot.spotType.getValue() >= type.getValue()) {
return new ParkingTicket(spot,new Date());
}
}
}
throw new ParkingFullException();
}
}
public class ParkingFullException extends Exception{
private static final long serialVersionUID = 1L;
}
public class ParkingTicket{
Floor floor;
Spot spot;
Date startDate;
public ParkingTicket(Spot spot, Date startDate) {
this.spot = spot;
this.startDate = startDate;
}
}
public interface SearchSpot{
List<Spot> getSpots();
}
public class NearestSpotsToTheEntrance implements SearchSpot{//Strategy Patern for various searching methods
@Override
public List<Spot> getSpots() {
return new ArrayList<Spot>();
}
}
public abstract class Observer{
protected Spot spotActivity;
public abstract void update(Floor floor);
}
public class ParkingDisplayBoard extends Observer{
public ParkingDisplayBoard(Spot spot) {
spotActivity = spot;
}
@Override
public void update(Floor floor) {
System.out.println("Floor = "+floor.getFloor()+" Spot:"+ spotActivity.spotType);
}
}
}