I have to incorporate a class into my program and I thought the best part of it (a ticket reservation program) was to have the tickets that are booked and are to be printed, be objects of a ticket class instead of elements in a list. I however won't get it to work. In the class file, I've defined the class as:
class Ticket:
def __init__(self, seat, numSeats, seatchart, date):
self.seat = seat
self.numSeats = numSeats
self.seatchart = seatchart
self.date = date
def geSeat(self):
return self.seat
def genumSeats(self):
return self.numSeats
def geSeatChart(self):
return self.seatchart
def geDate(self):
return self.date
In the main program file, I have these two functions:
def createTicket(numberseats, chart, date_after):
ticketlist = []
for elem in numberseats:
current_ticket = Ticket(elem[0], numberseats, chart, date_after)
ticketlist.append(current_ticket)
return ticketlist
def printTickets(ticketlist, data, map, days_after):
return ticketlist.printTickets(ticketlist[0], data, map, days_after)
This just doesn't work, it says I haven't defined ticketlist or that the list doesn't have the printTickets attribute. When I just tried to create an object from every seat number inside the numberseats list (if the user for example wants three seats), it said that an integer doesn't have that attribute - I feel like I'm at a dead end. What I'm essentially trying to do is replace this function:
def printtickets(data, map, days_after):
a = 0
count = 0
first_last = []
for row in map: # Used to put "TYST AVDELNING" correctly.
count += len(row)
for row in map: # Used to indentify window and aisle seats repsectively
first_last.append(row[::len(row) - 1])
for i in data:
with open("%s_biljett.txt" % i, "w") as file:
file.write(" PLATSBILJETT\n Stockholm -> Kalmar\nAvgångstid: 11:15 ")
file.write(days_after)
file.write("\n Plats ")
file.write(str(i))
if data > (count / 2): # Decides when to add "TYST AVDELNING"
file.write("\n TYST AVDELNING")
if any(data in sl for sl in first_last):
file.write("\n Fönsterplats")
else:
file.write("\n Gångplats")
a += 1
With a class and then print the same thing but for every object of that class, even if it's just one ticket or multiple. I just have no idea how to "convert it" if that's a proper way to say it. Is anyone able to help me out a bit? If this is even possible or if I have to approach it differently altogether.
EDIT
When calling the functions, I have this:
printTickets(numberseats, map, days_after)
Which I know is lacking one condition since the printTickets function has four conditions, I simply don't know what to put there.
When running the program and I for example input that I want 2 ticket, and choose seats 13 and 14, these are stored in the numberseats variable as a list. When printing them with the original function, they are printed like this:
Biljett
Stockholm -> Kalmar
Avgång: 11:15 [10 days ahead of current date is inputted here]
Plats [13 for the first ticket and 14 for the second]
[TYST AVDELNING inputted if the seat number is higher than half of the available seats, in this case it's 32.]
[Window seat or aisle seat depending on where on the map the seats are located]
One separate .txt-file is created for every seat number, so in the case of reserving 13 and 14, two separate files would be created. For info, the seat chart looks like this:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
↓ TYST AVD ↓
17 18 19 20
24 23 22 21
25 26 27 28
32 31 30 29