-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCinema.java
191 lines (177 loc) · 6.3 KB
/
Cinema.java
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
180
181
182
183
184
185
186
187
188
189
190
191
package cinema;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
public class Cinema {
static int numberOfRows;
static int numberOfSeats;
static int totalNumberOfSeats;
static int ticketPrice;
static int currentIncome;
static int totalIncome;
static int rowBooked;
static int seatBooked;
static String[][] seatPlan;
static int userInput;
static boolean stopProgram = false;
static boolean taken = true;
static int numberOfPurchasedTicket;
static double percentage;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
numberOfRows = scanner.nextInt();
System.out.println("Enter the number of seats in each row: ");
numberOfSeats = scanner.nextInt();
seatPlan = new String[numberOfRows + 1][numberOfSeats + 1];
totalNumberOfSeats = numberOfRows * numberOfSeats;
initiateSeatPlan();
while (!stopProgram) {
printMenu();
proceed();
}
}
public static void initiateSeatPlan() {
seatPlan[0][0] = " ";
// assign row number
for (int i = 0; i < seatPlan.length - 1; i++) {
int rowIndex = i + 1;
String rowIndexStr = Integer.toString(rowIndex);
seatPlan[i + 1][0] = rowIndexStr;
}
// assign seat number
for (int j = 0; j < seatPlan[0].length - 1; j++) {
int seatIndex = j + 1;
String seatIndexStr = Integer.toString(seatIndex);
seatPlan[0][seatIndex] = seatIndexStr;
}
// configure seats
String empty = "S";
for (int i = 0; i < seatPlan.length - 1; i++) {
for (int j = 0; j < seatPlan[i].length - 1; j++) {
seatPlan[i + 1][j + 1] = empty;
}
}
}
public static void printSeatPlan() {
System.out.println("Cinema:");
for (int i = 0; i < seatPlan.length;i ++) {
for (int j = 0; j < seatPlan[0].length; j++) {
System.out.print(seatPlan[i][j]);
System.out.print(" ");
}
System.out.println(" ");
}
}
public static void updateSeatPlan() {
String booked = "B";
for (int i = 0; i < seatPlan.length - 1; i++) {
for (int j = 0; j < seatPlan[i].length - 1; j++) {
int rowIndex = i + 1;
int seatIndex = j + 1;
if (rowIndex == rowBooked && seatIndex == seatBooked) {
seatPlan[rowIndex][seatIndex] = booked;
}
}
}
}
public static void printMenu() {
Scanner scanner = new Scanner(System.in);
System.out.println("1. Show the seats");
System.out.println("2. Buy a ticket");
System.out.println("3. Statistics");
System.out.println("0. Exit");
userInput = scanner.nextInt();
}
public static void proceed() {
switch (userInput) {
case 1:
printSeatPlan();
break;
case 2:
buyTicket();
break;
case 3:
showStatistics();
break;
case 0:
stopProgram = true;
break;
}
}
public static void buyTicket() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a row number:");
rowBooked = scanner.nextInt();
System.out.println("Enter a seat number in that row:");
seatBooked = scanner.nextInt();
checkSeat();
while (taken) {
System.out.println("Enter a row number:");
rowBooked = scanner.nextInt();
System.out.println("Enter a seat number in that row:");
seatBooked = scanner.nextInt();
checkSeat();
}
updateSeatPlan();
calculateTicketPrice();
System.out.println("Ticket price: $" + ticketPrice);
numberOfPurchasedTicket++;
currentIncome += ticketPrice;
calculatePercentage();
}
public static void checkSeat() {
if ((rowBooked > seatPlan.length - 1) || (seatBooked > seatPlan[0].length - 1) || rowBooked < 1 || seatBooked < 1) {
System.out.println("Wrong input!");
taken = true;
} else if ("B".equals(seatPlan[rowBooked][seatBooked])) {
System.out.println("That ticket has already been purchased!");
taken = true;
} else {
taken = false;
}
}
public static void calculateIncome() {
int totalSeats = numberOfSeats * numberOfRows;
if (totalSeats <= 60) {
ticketPrice = 10;
totalIncome = ticketPrice * totalSeats;
} else {
int frontRows = numberOfRows / 2;
int BackRows = numberOfRows - frontRows;
int ticketPriceFrontRows = 10;
int ticketPriceBackRows = 8;
totalIncome = (frontRows * ticketPriceFrontRows + BackRows * ticketPriceBackRows) * numberOfSeats;
}
}
public static void calculateTicketPrice() {
int totalSeats = numberOfSeats * numberOfRows;
if (totalSeats <= 60) {
ticketPrice = 10;
} else {
int frontRows = numberOfRows / 2;
int BackRows = numberOfRows - frontRows;
int ticketPriceFrontRows = 10;
int ticketPriceBackRows = 8;
if (rowBooked <= frontRows) {
ticketPrice = ticketPriceFrontRows;
} else {
ticketPrice = ticketPriceBackRows;
}
}
}
public static void calculatePercentage() {
percentage = (double) numberOfPurchasedTicket / totalNumberOfSeats * 100;
}
public static void showStatistics() {
calculateIncome();
System.out.printf("Number of purchased tickets: %d", numberOfPurchasedTicket);
System.out.println();
System.out.printf("Percentage: %.2f%%", percentage);
System.out.println();
System.out.printf("Current income: $%d", currentIncome);
System.out.println();
System.out.printf("Total income: $%d", totalIncome);
System.out.println();
}
}