帮我写mainprogram
时间: 2024-11-27 20:24:13 浏览: 3
HelloWorld21-MainProgram
编写 `MainProgram` 类需要实现与用户的交互,并处理各种操作请求。以下是一个示例实现,涵盖了任务要求的所有功能:
### MainProgram.java
```java
import java.io.*;
import java.util.Scanner;
public class MainProgram {
private static Scanner scanner = new Scanner(System.in);
private static SortedLinkedList<Ticket> tickets;
private static SortedLinkedList<Customer> customers;
public static void main(String[] args) {
// 初始化数据
initializeData();
// 主菜单循环
while (true) {
displayMenu();
char choice = getChoice();
processChoice(choice);
if (choice == 'f') break;
}
}
private static void initializeData() {
tickets = new SortedLinkedList<>();
customers = new SortedLinkedList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("input_data.txt"))) {
int numCustomers = Integer.parseInt(reader.readLine());
for (int i = 0; i < numCustomers; i++) {
String[] parts = reader.readLine().split(" ");
customers.add(new Customer(parts[0], parts[1]));
}
int numTickets = Integer.parseInt(reader.readLine());
for (int i = 0; i < numTickets; i++) {
String name = reader.readLine();
double price = Double.parseDouble(reader.readLine());
tickets.add(new Ticket(name, price));
}
// 读取折扣信息
double[] discounts = new double[3];
for (int i = 0; i < 3; i++) {
discounts[i] = Double.parseDouble(reader.readLine());
}
// 将折扣信息传递给每个票类对象
for (Ticket ticket : tickets) {
ticket.setDiscounts(discounts);
}
} catch (IOException e) {
System.err.println("Error reading input_data.txt: " + e.getMessage());
}
}
private static void displayMenu() {
System.out.println("Menu:");
System.out.println("f - Finish");
System.out.println("t - Display tickets");
System.out.println("c - Display customers");
System.out.println("a - Add tickets to customer's account");
System.out.println("r - Remove tickets from customer's account");
}
private static char getChoice() {
System.out.print("Enter your choice: ");
return scanner.nextLine().charAt(0);
}
private static void processChoice(char choice) {
switch (choice) {
case 'f':
System.out.println("Finishing the program.");
break;
case 't':
displayTickets();
break;
case 'c':
displayCustomers();
break;
case 'a':
addTicketsToCustomerAccount();
break;
case 'r':
removeTicketsFromCustomerAccount();
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
private static void displayTickets() {
System.out.println("Available Tickets:");
for (Ticket ticket : tickets) {
System.out.println(ticket.getName() + ": £" + ticket.getPrice());
}
}
private static void displayCustomers() {
System.out.println("Registered Customers:");
for (Customer customer : customers) {
System.out.println(customer.getFirstName() + " " + customer.getSurname() + " - Total Cost: £" + customer.getTotalCost() + ", Discounted Cost: £" + customer.getDiscountedTotalCost());
}
}
private static void addTicketsToCustomerAccount() {
System.out.print("Enter customer's first name: ");
String firstName = scanner.nextLine();
System.out.print("Enter customer's surname: ");
String surname = scanner.nextLine();
Customer customer = findCustomer(firstName, surname);
if (customer == null) {
System.out.println("Customer not found.");
return;
}
System.out.print("Enter ticket name: ");
String ticketName = scanner.nextLine();
Ticket ticket = findTicket(ticketName);
if (ticket == null) {
System.out.println("Ticket not found.");
return;
}
System.out.print("Enter number of tickets to add: ");
int quantity = Integer.parseInt(scanner.nextLine());
if (customer.addTickets(ticket, quantity)) {
System.out.println("Tickets added successfully.");
if (!customer.isEligibleForDiscount()) {
writeLetter(customer, ticket, quantity);
}
} else {
System.out.println("Failed to add tickets.");
}
}
private static void removeTicketsFromCustomerAccount() {
System.out.print("Enter customer's first name: ");
String firstName = scanner.nextLine();
System.out.print("Enter customer's surname: ");
String surname = scanner.nextLine();
Customer customer = findCustomer(firstName, surname);
if (customer == null) {
System.out.println("Customer not found.");
return;
}
System.out.print("Enter ticket name: ");
String ticketName = scanner.nextLine();
Ticket ticket = findTicket(ticketName);
if (ticket == null) {
System.out.println("Ticket not found.");
return;
}
System.out.print("Enter number of tickets to remove: ");
int quantity = Integer.parseInt(scanner.nextLine());
if (customer.removeTickets(ticket, quantity)) {
System.out.println("Tickets removed successfully.");
} else {
System.out.println("Failed to remove tickets.");
}
}
private static Customer findCustomer(String firstName, String surname) {
for (Customer customer : customers) {
if (customer.getFirstName().equals(firstName) && customer.getSurname().equals(surname)) {
return customer;
}
}
return null;
}
private static Ticket findTicket(String name) {
for (Ticket ticket : tickets) {
if (ticket.getName().equals(name)) {
return ticket;
}
}
return null;
}
private static void writeLetter(Customer customer, Ticket ticket, int quantity) {
try (FileWriter writer = new FileWriter("letters.txt", true)) {
writer.write("Dear " + customer.getFirstName() + " " + customer.getSurname() + ",\n");
writer.write("Thank you for purchasing " + quantity + " " + ticket.getName() + " tickets.\n");
writer.write("However, you currently do not qualify for a discount.\n");
writer.write("To qualify for a discount, you need to purchase at least " + (6 - customer.getTotalQuantity()) + " more tickets.\n");
writer.write("Best regards,\nNERD\n\n");
} catch (IOException e) {
System.err.println("Error writing to letters.txt: " + e.getMessage());
}
}
}
```
### 说明
1. **初始化数据**:从 `input_data.txt` 文件中读取客户和票务信息,并将它们存储在 `SortedLinkedList` 中。
2. **主菜单**:提供一个简单的文本菜单,让用户选择不同的操作。
3. **处理用户输入**:根据用户的选择调用相应的处理方法。
4. **显示票务信息**:列出所有可用的票务及其价格。
5. **显示客户信息**:列出所有注册客户的详细信息,包括他们购买的票务数量和总费用(如果有折扣)。
6. **添加票务到客户账户**:允许用户为特定客户添加指定数量的票务,并检查是否符合折扣条件。
7. **从客户账户移除票务**:允许用户从特定客户账户移除指定数量的票务。
8. **查找客户和票务**:辅助方法用于查找特定的客户和票务。
9. **写信函**:如果客户不符合折扣条件,生成并写入一封通知信函到 `letters.txt` 文件中。
这个实现满足了任务的要求,包括读取文件、处理用户输入、更新客户和票务信息以及生成通知信函。希望这对你有帮助!
阅读全文