Skip to content

Commit 4ef1d47

Browse files
committed
06/08/2018 at 15:56
1 parent 69a6abc commit 4ef1d47

3 files changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.collections;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public class Main {
8+
9+
public static void main(String[] args) {
10+
Theatre theatre = new Theatre("Olympian", 8, 12);
11+
12+
if (theatre.reserveSeat("D12")) {
13+
System.out.println("Please pay for D12");
14+
} else {
15+
System.out.println("Seat already reserved");
16+
}
17+
18+
if (theatre.reserveSeat("D12")) {
19+
System.out.println("Please pay for D12");
20+
} else {
21+
System.out.println("Seat already reserved");
22+
}
23+
24+
if(theatre.reserveSeat("B13")) {
25+
System.out.println("Please pay for B13");
26+
} else {
27+
System.out.println("Seat already reserved");
28+
}
29+
30+
List<Theatre.Seat> reverseSeats = new ArrayList<>(theatre.getSeats());
31+
Collections.reverse(reverseSeats);
32+
printList(reverseSeats);
33+
34+
List<Theatre.Seat> priceSeats = new ArrayList<>(theatre.getSeats());
35+
priceSeats.add(theatre.new Seat("B00", 13.00));
36+
priceSeats.add(theatre.new Seat("A00", 13.00));
37+
Collections.sort(priceSeats, Theatre.PRICE_ORDER);
38+
printList(priceSeats);
39+
40+
// Collections.shuffle(seatCopy);
41+
// System.out.println("Printing seatCopy");
42+
// printList(seatCopy);
43+
// System.out.println("Printing theatre.seat");
44+
// printList(theatre.seats);
45+
//
46+
// Theatre.Seat minSeat = Collections.min(seatCopy);
47+
// Theatre.Seat maxSeat = Collections.max(seatCopy);
48+
// System.out.println("Min seat number is " + minSeat.getSeatNumber());
49+
// System.out.println("Max seat number is " + maxSeat.getSeatNumber());
50+
//
51+
// sortList(seatCopy);
52+
// System.out.println("Printing sorted seatCopy");
53+
// printList(seatCopy);
54+
//
55+
// List<Theatre.Seat> newList = new ArrayList<>(theatre.seats.size());
56+
// Collections.copy(newList, theatre.seats);
57+
}
58+
59+
public static void printList(List<Theatre.Seat> list) {
60+
for (Theatre.Seat seat : list) {
61+
System.out.print(" " + seat.getSeatNumber() + " $" + seat.getPrice());
62+
}
63+
System.out.println();
64+
System.out.println("======================================================================");
65+
}
66+
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.collections;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
System.out.println(String.format("%03d", 5));
7+
8+
}
9+
10+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.collections;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by dev on 2/12/2015.
7+
*/
8+
public class Theatre {
9+
private final String theatreName;
10+
private List<Seat> seats = new ArrayList<>();
11+
static final Comparator<Seat> PRICE_ORDER;
12+
13+
static {
14+
PRICE_ORDER = new Comparator<Seat>() {
15+
@Override
16+
public int compare(Seat seat1, Seat seat2) {
17+
if (seat1.getPrice() < seat2.getPrice()) {
18+
return -1;
19+
} else if (seat1.getPrice() > seat2.getPrice()) {
20+
return 1;
21+
} else {
22+
return 0;
23+
}
24+
}
25+
};
26+
}
27+
28+
public Theatre(String theatreName, int numRows, int seatsPerRow) {
29+
this.theatreName = theatreName;
30+
31+
int lastRow = 'A' + (numRows - 1);
32+
for (char row = 'A'; row <= lastRow; row++) {
33+
for (int seatNum = 1; seatNum <= seatsPerRow; seatNum++) {
34+
double price = 12.00;
35+
36+
if((row < 'D') && (seatNum >=4 && seatNum <=9)) {
37+
price = 14.00;
38+
} else if((row > 'F') || (seatNum < 4 || seatNum > 9)) {
39+
price = 7.00;
40+
}
41+
42+
Seat seat = new Seat(row + String.format("%02d", seatNum), price);
43+
seats.add(seat);
44+
}
45+
}
46+
}
47+
48+
public String getTheatreName() {
49+
return theatreName;
50+
}
51+
52+
public boolean reserveSeat(String seatNumber) {
53+
Seat requestedSeat = new Seat(seatNumber, 0);
54+
int foundSeat = Collections.binarySearch(seats, requestedSeat, null);
55+
if (foundSeat >= 0) {
56+
return seats.get(foundSeat).reserve();
57+
} else {
58+
System.out.println("There is no seat " + seatNumber);
59+
return false;
60+
}
61+
}
62+
63+
public List<Seat> getSeats() {
64+
return seats;
65+
}
66+
67+
public class Seat implements Comparable<Seat> {
68+
private final String seatNumber;
69+
private double price;
70+
private boolean reserved = false;
71+
72+
public Seat(String seatNumber, double price) {
73+
this.seatNumber = seatNumber;
74+
this.price = price;
75+
}
76+
77+
@Override
78+
public int compareTo(Seat seat) {
79+
return this.seatNumber.compareToIgnoreCase(seat.getSeatNumber());
80+
}
81+
82+
public boolean reserve() {
83+
if (!this.reserved) {
84+
this.reserved = true;
85+
System.out.println("Seat " + seatNumber + " reserved");
86+
return true;
87+
} else {
88+
return false;
89+
}
90+
}
91+
92+
public boolean cancel() {
93+
if (this.reserved) {
94+
this.reserved = false;
95+
System.out.println("Reservation of seat " + seatNumber + " cancelled");
96+
return true;
97+
} else {
98+
return false;
99+
}
100+
}
101+
102+
public String getSeatNumber() {
103+
return seatNumber;
104+
}
105+
106+
public double getPrice() {
107+
return price;
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)