Home > Net >  Simple ticketing simulator , Why am I able to issue tickets only in the order?
Simple ticketing simulator , Why am I able to issue tickets only in the order?

Time:07-09

I`m just learning to code and I was trying to implement a simple Ticketing Simulation .

I have Seats class , in this class I have a LinkedList storing all the seats in the saloon . I created and numbered the seats , and added them to this static LinkedList .When I`m ticketing a seat , it is done by entering a seat number and a char for its row .. I have 50 seats , every 10 seats has an alphabetic value of char . 1-10 A , 11-20 B and so on..

public class Seats {

public static LinkedList<Seats> seatsInSaloon;
private final int seatNumber;
private final char seatChar;

public Seats(int seatNumber, char seatChar) {

    this.seatChar = seatChar;
    this.seatNumber = seatNumber;
}

private static void generateSeats() {

    seatsInSaloon = new LinkedList<Seats>();
    char x = 'a';
    for (int i = 1; i < 51; i  ) {
        if (i % 10 == 0) {
            Seats seats = new Seats(i, x);
            seatsInSaloon.add(seats);
            x  ;

        } else {
            Seats seats = new Seats(i, x);
            seatsInSaloon.add(seats);
        }

    }

}

public int getSeatNumber() {
    return seatNumber;
}

public char getSeatChar() {
    return seatChar;
}

public static void fillSeats() {
    generateSeats();
}

}

I wanted to make sure the ticketed seat is removed from that static LinkedList holding all seats , so it cannot be booked again . While I was testing my code , I realized , if I do not issue tickets in order ( first ticket for seat 1,a.. second ticket for 2,a .. and so on..) , it acts as if that seat with given seat number and char is not existent in the static linkedlist which holds all the seats .. Can somebody help me see what Im doing wrong ? Does it require ticketing in order because Im using a linkedList ? Thank you..

    public class Ticketing {

private static Seats seats;

public static LinkedList<Seats> seeAllSeatsAvailable() {
    return Seats.seatsInSaloon;
}

public static void ticketSeat(int seatNum, char seatChar) {
    if ((seatNum > 50 || seatNum < 1) || (seatChar > 'e' || seatChar < 'a')) {
        System.out.println("please check the numbers you have entered .... ");
    }

    for (Seats s : Seats.seatsInSaloon) {
        if (s.getSeatChar() == seatChar && s.getSeatNumber() == seatNum) {
            System.out.println("HERE IS YOUR TICKET : "   seatChar   seatNum);
            Seats.seatsInSaloon.remove(s);
            break;
        } else if (!Seats.seatsInSaloon.contains(s.getSeatChar() == seatChar && s.getSeatNumber() == seatNum)) {
            System.out.println("sorry... This ticket has already been issued");
            break;
        }
    }

}

}

here is the test.. public class TicketDriver {

public static void main(String[] args) {

    // System.out.println(Ticketing.seeAllSeats());
    Seats.fillSeats();

    Ticketing.ticketSeat(1, 'a'); //fine



    Ticketing.ticketSeat(2, 'a'); //fine
    Ticketing.ticketSeat(3, 'a'); //fine
    Ticketing.ticketSeat(4, 'a'); //fine
    Ticketing.ticketSeat(4, 'a'); //fine , prints the correct error.

    Ticketing.ticketSeat(11, 'b'); //?????
    
    /*
    
    Last one Gives error as if this seat does not exist inside linkedlist.
    It prints "sirry,, This ticket has already been issued"
                    
    */
    

}

}

CodePudding user response:

LinkedList.contains() takes an object as it's param to test if the LinkedList contains that object. Instead of

} else if (!Seats.seatsInSaloon.contains(s.getSeatChar() == seatChar && s.getSeatNumber() == seatNum)) {
    System.out.println("sorry... This ticket has already been issued");
    break;
}

You need to pass an instance of Seats with the seatChar and seatNumber that you're looking for.

It would also make more sense if you have a class called Seat which represents one instance of a seat, and then you can define a Collection of Seat like

LinkedList<Seat> seats;

CodePudding user response:

Remove the if else condition from yout ticketing class and create an another validation method for that functionality.You can copy the list you created, delete it every time you create a ticket, and perform the validation process from there..

Delete this condition.

else if (!Seats.seatsInSaloon.contains(s.getSeatChar() == seatChar && s.getSeatNumber() == seatNum)) {
        System.out.println("sorry... This ticket has already been issued");
        break;
    }
  • Related