-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCinema.kt
150 lines (134 loc) · 4 KB
/
Cinema.kt
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
package cinema
class Seat {
var state = 'S'
}
class Row(seatNum: Int) {
var seats = List(seatNum) { Seat() }
}
class RoomInfo {
var name: String = "Unnamed"
var rowNum: Int = 0
var seatNum: Int = 0
val priceFront = 10
val priceBack = 8
}
class Room(roomInfo: RoomInfo) {
var info: RoomInfo = roomInfo
var rows = listOf<Row>()
fun getSeatState(rowNum: Int, seatNum: Int): Char {
return rows[rowNum].seats[seatNum].state
}
fun changeSeatState(rowNum: Int, seatNum: Int, newState: Char) {
rows[rowNum].seats[seatNum].state = newState
}
}
class SeatCoords {
var row = 0
var seat = 0
}
fun getRoomInfo(): RoomInfo {
val roomInfo = RoomInfo()
roomInfo.name = "Cinema"
println("Enter the number of rows:")
roomInfo.rowNum = readln().toInt()
println("Enter the number of seats in each row:")
roomInfo.seatNum = readln().toInt()
return roomInfo
}
fun initRoom(roomInfo: RoomInfo): Room {
val room = Room(roomInfo)
room.info.name = roomInfo.name
room.rows = List(room.info.rowNum) { Row(room.info.seatNum) }
return room
}
fun getUserSeat(room: Room): SeatCoords {
val userInput = SeatCoords()
var validCoords: Boolean
do {
println("\nEnter a row number:")
userInput.row = readln().toInt()
println("Enter a seat number in that row:")
userInput.seat = readln().toInt()
if (userInput.row !in 1..room.info.rowNum || userInput.seat !in 1..room.info.seatNum) {
println("\nWrong input!")
validCoords = false
}
else validCoords = true
} while (!validCoords)
userInput.row--
userInput.seat--
return userInput
}
fun checkSeatPrice(room: Room, row: Int): Int {
val price: Int
if( room.info.rowNum * room.info.seatNum <= 60) price = room.info.priceFront
else {
val frontRows: Int = (room.info.rowNum) / 2
price = if( row + 1 <= frontRows ) room.info.priceFront else room.info.priceBack
}
return price
}
fun printRoom(room: Room) {
println("\n${room.info.name}:")
print(" ")
for (i in 1 .. room.rows[0].seats.size)
print("$i ")
print("\n")
for (i in room.rows.indices){
print("${i + 1} ")
for (seat in room.rows[i].seats)
print("${seat.state} ")
print("\n")
}
}
fun buySeat(room: Room) {
var bought = false
do{
val inquiry: SeatCoords = getUserSeat(room)
if (room.getSeatState(inquiry.row, inquiry.seat) == 'B') {
println("\nThat ticket has already been purchased!")
} else {
println("\nTicket price: \$${checkSeatPrice(room, inquiry.row)}")
room.changeSeatState(inquiry.row, inquiry.seat, 'B')
bought = true
}
} while (!bought)
}
fun statistics(room: Room) {
var ticketsPurchased = 0
var incomeCur = 0
var incomeTotal = 0
for (rowNum in room.rows.indices){
for (seatNum in room.rows[rowNum].seats.indices) {
val price = checkSeatPrice(room, rowNum)
incomeTotal += price
if (room.getSeatState(rowNum, seatNum) == 'B') {
ticketsPurchased++
incomeCur += price
}
}
}
val occupancy: Double = (ticketsPurchased.toDouble() / (room.info.rowNum * room.info.seatNum)) * 100
println("\nNumber of purchased tickets: $ticketsPurchased")
println("Percentage: ${"%.2f".format(occupancy)}%")
println("Current income: \$$incomeCur")
println("Total income: \$$incomeTotal")
}
fun menuLoop(room: Room) {
var menuChoice: Int
do {
do {
println("\n1. Show the seats\n2. Buy a ticket\n3. Statistics\n0. Exit")
menuChoice = readln().toInt()
} while (menuChoice !in 0..3)
when (menuChoice) {
1 -> printRoom(room)
2 -> buySeat(room)
3 -> statistics(room)
}
} while (menuChoice != 0)
}
fun main() {
val cinema = initRoom(getRoomInfo())
menuLoop(cinema)
}