📜  售票java程序 - Java(1)

📅  最后修改于: 2023-12-03 14:50:44.312000             🧑  作者: Mango

售票Java程序介绍

本文将介绍一个售票Java程序,该程序由Java编写,能实现售票、检票等基本功能。该程序采用面向对象编程思想,使用了Java 8的Lambda表达式和Stream API,代码结构清晰,逻辑简单易懂。

功能描述

该程序的主要功能包括:

  • 用户能够查询余票信息、购票、退票等;
  • 售票处能够开票、关闭售票、更新车次、车票等信息;
  • 检票员能够检票,并对检票结果进行记录和统计。
实现细节
1. 车次信息类

该类表示一个车次的信息,包括起点站、终点站、车次号、发车时间、到达时间、票价等属性。代码如下:

public class Train {
    private String startStation;
    private String endStation;
    private String trainNumber;
    private LocalDate departureDate;
    private LocalTime departureTime;
    private LocalDate arrivalDate;
    private LocalTime arrivalTime;
    private double ticketPrice;
    private int totalSeats;
    private int availableSeats;

    // 省略getter和setter方法,以及构造方法和toString方法
}
2. 车票信息类

该类表示一张车票的信息,包括车次、座位号、购票时间等属性。代码如下:

public class Ticket {
    private Train train;
    private int seatNumber;
    private LocalDateTime purchaseTime;

    // 省略getter和setter方法,以及构造方法和toString方法
}
3. 用户信息类

该类表示一个用户的信息,包括用户名、密码、已购车票等属性。代码如下:

public class User {
    private String username;
    private String password;
    private List<Ticket> ticketList = new ArrayList<>();

    // 省略getter和setter方法,以及构造方法和toString方法
}
4. 售票处类

该类表示一个售票处,包括起始日期、结束日期、车次信息、车票信息等属性。代码如下:

public class TicketOffice {
    private LocalDate startingDate;
    private LocalDate endingDate;
    private List<Train> trainList = new ArrayList<>();
    private List<Ticket> ticketList = new ArrayList<>();

    // 省略getter和setter方法,以及构造方法和toString方法
}
5. 检票员类

该类表示一个检票员,包括姓名、工号、已检车票信息等属性。代码如下:

public class Checker {
    private String name;
    private String id;
    private List<Ticket> ticketList = new ArrayList<>();

    // 省略getter和setter方法,以及构造方法和toString方法
}
6. 功能实现

在程序中,我们定义了一个TicketSystem类,用于实现各种功能,包括初始化售票处信息、查询余票信息、购票、退票、开票、关闭售票、更新车次信息等。代码如下:

public class TicketSystem {
    private TicketOffice ticketOffice;
    private User currentUser = null;
    private Checker currentChecker = null;

    // 省略构造方法

    public Train findTrain(String trainNumber) {
        return ticketOffice.getTrainList().stream()
            .filter(train -> train.getTrainNumber().equals(trainNumber))
            .findFirst()
            .orElse(null);
    }

    public List<Train> findTrains(String startStation, String endStation, LocalDate departureDate) {
        return ticketOffice.getTrainList().stream()
            .filter(train -> train.getStartStation().equals(startStation))
            .filter(train -> train.getEndStation().equals(endStation))
            .filter(train -> train.getDepartureDate().equals(departureDate))
            .collect(Collectors.toList());
    }

    public int getAvailableSeats(Train train) {
        return train.getAvailableSeats();
    }

    public void login(String username, String password) {
        Optional<User> optionalUser = ticketOffice.getUserList().stream()
            .filter(user -> user.getUsername().equals(username))
            .findFirst();
        if (optionalUser.isPresent()) {
            User user = optionalUser.get();
            if (user.getPassword().equals(password)) {
                currentUser = user;
            } else {
                System.out.println("Invalid password");
            }
        } else {
            System.out.println("User not found");
        }
    }

    public void logout() {
        currentUser = null;
    }

    public void purchaseTicket(Train train, int seatNumber) {
        Ticket ticket = new Ticket(train, seatNumber, LocalDateTime.now());
        currentUser.getTicketList().add(ticket);
        train.setAvailableSeats(train.getAvailableSeats() - 1);
        ticketOffice.getTicketList().add(ticket);
    }

    public void refundTicket(int ticketIndex) {
        Ticket ticket = currentUser.getTicketList().get(ticketIndex - 1);
        ticket.getTrain().setAvailableSeats(ticket.getTrain().getAvailableSeats() + 1);
        ticketOffice.getTicketList().remove(ticket);
        currentUser.getTicketList().remove(ticket);
    }

    public void openTicketOffice() {
        ticketOffice.setStartingDate(LocalDate.now());
        ticketOffice.setEndingDate(LocalDate.of(2022, 12, 31));
    }

    public void closeTicketOffice() {
        ticketOffice.setEndingDate(LocalDate.now());
    }

    public void addTrain(Train train) {
        ticketOffice.getTrainList().add(train);
    }

    public void updateTrain(Train train) {
        // TODO
    }
}
7. 程序入口

程序入口在Main类中,代码如下:

public class Main {
    public static void main(String[] args) {
        TicketSystem ticketSystem = new TicketSystem(new TicketOffice());

        Scanner scanner = new Scanner(System.in);

        while (true) {
            if (ticketSystem.getCurrentUser() == null) {
                System.out.print("Username: ");
                String username = scanner.nextLine();
                System.out.print("Password: ");
                String password = scanner.nextLine();
                ticketSystem.login(username, password);
                if (ticketSystem.getCurrentUser() == null) {
                    continue;
                }
            }

            System.out.println("1.Query available trains");
            System.out.println("2.Purchase ticket");
            System.out.println("3.Refund ticket");
            System.out.println("4.Exit");

            System.out.print("Input choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("Input start station: ");
                    String startStation = scanner.nextLine();
                    System.out.print("Input end station: ");
                    String endStation = scanner.nextLine();
                    System.out.print("Input departure date (yyyy-MM-dd): ");
                    LocalDate departureDate = LocalDate.parse(scanner.nextLine());
                    List<Train> trainList = ticketSystem.findTrains(startStation, endStation, departureDate);
                    for (Train train : trainList) {
                        System.out.printf("%s %s-%s %s-%s %.2f %d%n",
                                train.getTrainNumber(), train.getStartStation(), train.getEndStation(),
                                train.getDepartureTime(), train.getArrivalTime(), train.getTicketPrice(), train.getAvailableSeats());
                    }
                    break;
                case 2:
                    System.out.print("Input train number: ");
                    String trainNumber = scanner.nextLine();
                    Train train = ticketSystem.findTrain(trainNumber);
                    if (train == null) {
                        System.out.println("Train not found");
                        break;
                    }
                    System.out.printf("Available seats: %d%n", ticketSystem.getAvailableSeats(train));
                    System.out.print("Input seat number: ");
                    int seatNumber = scanner.nextInt();
                    scanner.nextLine();
                    ticketSystem.purchaseTicket(train, seatNumber);
                    System.out.println("Purchase succeed");
                    break;
                case 3:
                    if (ticketSystem.getCurrentUser().getTicketList().isEmpty()) {
                        System.out.println("No ticket to refund");
                        break;
                    }
                    System.out.println("My ticket list:");
                    for (int i = 0; i < ticketSystem.getCurrentUser().getTicketList().size(); i++) {
                        Ticket ticket = ticketSystem.getCurrentUser().getTicketList().get(i);
                        System.out.printf("%d %-10s %s-%s %d %s%n",
                                i + 1, ticket.getTrain().getTrainNumber(),
                                ticket.getTrain().getStartStation(), ticket.getTrain().getEndStation(),
                                ticket.getSeatNumber(), ticket.getPurchaseTime());
                    }
                    System.out.print("Input ticket number to refund: ");
                    int ticketIndex = scanner.nextInt();
                    scanner.nextLine();
                    ticketSystem.refundTicket(ticketIndex);
                    System.out.println("Refund succeed");
                    break;
                case 4:
                    ticketSystem.logout();
                    System.out.println("Bye");
                    return;
                default:
                    System.out.println("Invalid choice");
            }
        }
    }
}
总结

本文介绍了一个售票Java程序的实现细节,包括类的设计、功能实现细节和程序入口。此程序问题一定不少,但在这里,我就不再赘述了,如您对此程序还有任何问题,欢迎在评论区中与我交流。