0

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 
Hvar01
  • 5
  • 2
  • Please provide a [mre] including how you're calling the functions, expected output, and the exact errors you get. BTW, [you don't need getters](https://stackoverflow.com/a/36943813/4518341) like `geSeat`; you can simply get rid of them. – wjandrea Dec 07 '20 at 22:08
  • Thank you for helping out! I've added the information you requested, if you need anything more just tell me. Also, I'll make sure to remove those getters if we don't need them anyways. – Hvar01 Dec 07 '20 at 22:37

1 Answers1

0

This should work if you look for a str for each class instance:

class Ticket:
    def __init__(self, val):
        self.val = val
    def __str__(self):
        return "This ticket's info: " + str(self.val)
        
def createTicket(list_of_val):
    return [Ticket(x) for x in list_of_val]

def printTickets(list_of_ticket):
    for x in list_of_ticket:
        print(x)
    return [str(x) for x in list_of_ticket]

list_of_ticket = createTicket([1,2,3])
printTickets(list_of_ticket)

Basically use __str__ to store the string you want for each ticket. It will be returned when you use str() or print() on the class instances.

Z Li
  • 4,133
  • 1
  • 4
  • 19
  • I tried using your code and it did work! Thanks. I just wonder, if I now want to increase the attributes used it def __str__(self): how do I go about that. When attempting to just add attributes, it wouldn't let me. Any tips? Do I have to do that in the function? – Hvar01 Dec 07 '20 at 23:00
  • you can define more attributes anywhere you want and use them in `__str__` to print more information. Just like `self.val` – Z Li Dec 07 '20 at 23:02
  • It says "Signature of method 'Ticket.__str__()' does not match signature of base method in class 'object'", this must mean I'm doing something wrong. If I try to add 'map' and 'days_after' for example, do I put them in the class part or in the methods? Or do I create new methods for them, as you did for val? – Hvar01 Dec 07 '20 at 23:26
  • it would be helpful if you post your code to replicate the error. – Z Li Dec 07 '20 at 23:28
  • I solved it, I forgot to use self. before every variable I used and they were supposed to be used in the createList function. Now everything works they way it's supposed to! Thank you so much for all your help! – Hvar01 Dec 07 '20 at 23:47