-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession.java
More file actions
152 lines (131 loc) · 4.55 KB
/
Session.java
File metadata and controls
152 lines (131 loc) · 4.55 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
package Moblima;
import java.math.BigInteger;
public class Session {
private Time time;
private Cinema cinema;
private Seat[][] seat;
private int numOfEmptySeat;
public Session(Time time, Cinema cinema) {
this.cinema = cinema;
this.time = time;
seat = new Seat[cinema.getRow()][cinema.getColumn()];
for (int i = 0; i < cinema.getRow(); i++) {
for (int j = 0; j < cinema.getColumn(); j++) {
seat[i][j] = new Seat(i, j);
}
}
setNumOfEmptySeat(cinema.getRow() * cinema.getColumn());
}
public Session(Company company, String s) {
String[] temp = s.split("##");
this.time = new Time(temp[0]);
for (int i = 0; i < company.size(); i++) {
for (Cinema cine : company.get(i).get()) {
if (Integer.parseInt(temp[1]) == cine.getCinemaID()) {
this.cinema = cine;
break;
}
}
}
this.setNumOfEmptySeat(this.cinema.getColumn() * this.cinema.getRow());
BigInteger seatInfo = new BigInteger(temp[2]);
String seatB = seatInfo.toString(2);
while (seatB.length() < cinema.getColumn() * cinema.getRow()) {
seatB = "0" + seatB;
}
seat = new Seat[this.cinema.getRow()][this.cinema.getColumn()];
for (int i = 0; i < cinema.getRow(); i++) {
for (int j = 0; j < cinema.getColumn(); j++) {
seat[i][j] = new Seat(i, j);
seat[i][j].unAssign();
if ((seatB.charAt(cinema.getColumn() * i + j)+"").compareTo("1") == 0) {
seat[i][j].assign();
this.numOfEmptySeat--;
}
}
}
}
public String toString() {
String seatStr = "";
for (int i = 0; i < cinema.getRow(); i++) {
for (int j = 0; j < cinema.getColumn(); j++) {
seatStr = seatStr + (seat[i][j].getAssign() ? "1" : "0");
}
}
BigInteger seatInfo = new BigInteger(seatStr, 2);
return time.toString() + "##" + cinema.getCinemaID() + "##" + seatInfo.toString();
}
public Time getTime() {
return this.time;
}
public Cinema getCinema() {
return this.cinema;
}
public void presentSeat() {
System.out.println("@Cinema : " + this.cinema.getCinemaID());
if (numOfEmptySeat == 0) {
System.out.println("Sorry. Tickets all sold out.");
return ;
}
System.out.print(" ");
for (int j = 0; j < cinema.getColumn() - 1; j++) {
if (j == cinema.getColumn()/2)
System.out.print(" ||");
System.out.format(" %2d", j+1);
}
System.out.println(" " + cinema.getColumn());
for (int j = 0; j < cinema.getColumn() + 1; j++) {
if (j == cinema.getColumn()/2)
System.out.print("__");
System.out.print(" __");
}
for (int i = 0; i < cinema.getRow(); i++) {
System.out.format("\n%2d|", i + 1);
for (int j = 0; j < cinema.getColumn(); j++) {
if (j == cinema.getColumn()/2)
System.out.print("||");
if (seat[i][j].getAssign()) {
System.out.print(" x ");
} else {
System.out.print(" 0 ");
}
}
}
System.out.println();
}
public boolean assignSeat(int row, int column) {
if (this.seat[row][column] == null) {
return false;
}
if (this.seat[row][column].getAssign()) {
System.out.println("Seat already assgined to a customer.");
return false;
}
this.seat[row][column].assign();
setNumOfEmptySeat(getNumOfEmptySeat() - 1);
System.out.println("Seat Assigned!");
return true;
}
public boolean unAssignSeat(Integer row, Integer column) {
if (this.seat[row][column] == null) {
return false;
}
seat[row][column].unAssign();
setNumOfEmptySeat(getNumOfEmptySeat() + 1);
System.out.println("Seat Unassigned!");
return true;
}
public boolean seatAvailable(Integer x, Integer y) {
if ((x >= 0 && x < cinema.getRow()) && (y >=0 && y < cinema.getColumn())) {
return (!seat[x][y].getAssign());
} else {
return false;
}
}
public int getNumOfEmptySeat() {
return numOfEmptySeat;
}
public void setNumOfEmptySeat(int numOfEmptySeat) {
this.numOfEmptySeat = numOfEmptySeat;
}
}