【发布时间】:2018-05-19 21:53:37
【问题描述】:
我正在尝试弄清楚如何在我已经建立的代码中实现用于写入文本文件的代码(在第三方的主要帮助下,因为我是 Java 新手)。
我希望将已注册或创建的客户或预订的详细信息保存在文本文件中,如下所示(第一步 - 1)注册新客户,或 2)创建一个新的预订):
public static void main(String[] args) {
// setup and initialise the test data
List<Flight> availableFlights = generateTestFlights();
List<Hotel> availableHotels = generateTestHotels();
List<Excursion> availableExcursions = generateTestExcursions();
// create a new list of bookings
List<Booking> listOfReservedBookings = new ArrayList<>();
// initialise a new sales consultant which will operate on the behalf of the
// customer
SalesConsultant salesConsultant = new SalesConsultant();
// initialise the keyboard code
Scanner scanner = new Scanner(System.in);
// what will be displayed when executing application
System.out.println("Menu: ");
System.out.println("1) Register a new Customer");
System.out.println("2) Create a new Booking");
System.out.println("3) View available list of Flights");
System.out.println("4) View available list of Hotels");
System.out.println("5) View available list of Excursions");
System.out.println("6) View Reserved Bookings");
System.out.println("Please make your choice (type code number): ");
// displaying above written within brackets
String choice = scanner.nextLine();
// when "exit" choice is not showing
while (choice.equalsIgnoreCase("exit") == false) {
// register customer
if (choice.equalsIgnoreCase("1")) {
System.out.println("Registering a new customer: ");
// get the card ID for the new customer
System.out.println("Please insert Customer ID Card Number: ");
String cardID = scanner.nextLine();
// get the name of the new customer
System.out.println("Please insert Customer Name & Surname: ");
String name = scanner.nextLine();
// get the address of the new customer
System.out.println("Please insert Customer Address: ");
String address = scanner.nextLine();
// register customer
Customer customer = new Customer();
salesConsultant.registerCustomer(customer);
System.out.println("Customer has been registered successfully!");
}
此外,我需要为此创建一个新类,还是可以通过 main 方法完成?
不确定是否需要更多代码。
有人可以就此提出建议吗?
非常感谢。
【问题讨论】:
-
虽然不是强制性的,但如果您要将这些数据存储并检索到文本文件中,那么在我看来,是的,您应该创建另一个类来处理读取和写入文件等等.这种东西最适合数据库,因为在需要时检索特定于数据的查询要容易得多。