简单的票务模拟器,为什么我只能按顺序出票



我只是在学习编码,我试图实现一个简单的票务模拟。

我有座位类,在这个类中,我有一个LinkedList,存储沙龙中的所有座位。我创建并编号了这些座位,并将它们添加到这个静态LinkedList中。当我给一个座位开罚单时,我会输入一个座位号码和一个排字符。。我有50个座位,每10个座位的字母值为char。1-10 A、11-20 B等等。

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();
}

}

我想确保售票座位从容纳所有座位的静态LinkedList中删除,这样就不能再预订了。当我测试我的代码时,我意识到,如果我不按顺序出票(第一张票是1号座位,第二张票是2号座位,a…等等),那么就好像拥有给定座位号和字符的座位不存在于容纳所有座位的静态链表中一样。。有人能帮我用linkedList看看我m doing wrong ? Does it require ticketing in order because Im是什么吗?非常感谢。

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;
}
}
}

}

这是测试。。公共类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"

*/

}

}

LinkedList.contains()将一个对象作为参数来测试LinkedList是否包含该对象。代替

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

您需要传递一个具有您要查找的seatChar和seatNumber的Seats实例。

如果你有一个名为Seat的类,它代表一个座位的一个实例,然后你可以定义一个类似的座位集合,这也会更有意义

LinkedList<Seat> seats;

从yout ticketing类中删除if-else条件,并为该功能创建另一个验证方法。您可以复制创建的列表,每次创建票证时将其删除,然后从中执行验证过程。。

删除此条件。

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

相关内容

  • 没有找到相关文章

最新更新