-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTheatre.java
More file actions
110 lines (93 loc) · 2.65 KB
/
Theatre.java
File metadata and controls
110 lines (93 loc) · 2.65 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
package com.collections;
import java.util.*;
/**
* Created by dev on 2/12/2015.
*/
public class Theatre {
private final String theatreName;
private List<Seat> seats = new ArrayList<>();
static final Comparator<Seat> PRICE_ORDER;
static {
PRICE_ORDER = new Comparator<Seat>() {
@Override
public int compare(Seat seat1, Seat seat2) {
if (seat1.getPrice() < seat2.getPrice()) {
return -1;
} else if (seat1.getPrice() > seat2.getPrice()) {
return 1;
} else {
return 0;
}
}
};
}
public Theatre(String theatreName, int numRows, int seatsPerRow) {
this.theatreName = theatreName;
int lastRow = 'A' + (numRows - 1);
for (char row = 'A'; row <= lastRow; row++) {
for (int seatNum = 1; seatNum <= seatsPerRow; seatNum++) {
double price = 12.00;
if((row < 'D') && (seatNum >=4 && seatNum <=9)) {
price = 14.00;
} else if((row > 'F') || (seatNum < 4 || seatNum > 9)) {
price = 7.00;
}
Seat seat = new Seat(row + String.format("%02d", seatNum), price);
seats.add(seat);
}
}
}
public String getTheatreName() {
return theatreName;
}
public boolean reserveSeat(String seatNumber) {
Seat requestedSeat = new Seat(seatNumber, 0);
int foundSeat = Collections.binarySearch(seats, requestedSeat, null);
if (foundSeat >= 0) {
return seats.get(foundSeat).reserve();
} else {
System.out.println("There is no seat " + seatNumber);
return false;
}
}
public List<Seat> getSeats() {
return seats;
}
public class Seat implements Comparable<Seat> {
private final String seatNumber;
private double price;
private boolean reserved = false;
public Seat(String seatNumber, double price) {
this.seatNumber = seatNumber;
this.price = price;
}
@Override
public int compareTo(Seat seat) {
return this.seatNumber.compareToIgnoreCase(seat.getSeatNumber());
}
public boolean reserve() {
if (!this.reserved) {
this.reserved = true;
System.out.println("Seat " + seatNumber + " reserved");
return true;
} else {
return false;
}
}
public boolean cancel() {
if (this.reserved) {
this.reserved = false;
System.out.println("Reservation of seat " + seatNumber + " cancelled");
return true;
} else {
return false;
}
}
public String getSeatNumber() {
return seatNumber;
}
public double getPrice() {
return price;
}
}
}