-1

I'm trying to figure out how to implement code for writing in a text file, within the code that I have already established (with major help from a third party, as I am a Java newbie).

The way I would like it is to save the details of a customer or a booking, that has been registered or created, in a text file, as per below (first step - 1) Register a new Customer, or 2) Create a new Booking):

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!");
        }

Furthermore, would I need to create a new class for this, or can it be done through the main method?

Not sure if further code is needed.

Can someone kindly advise on this, please?

Many thanks in advance.

  • Although not mandatory, If you are going to store and retrieve this data into a text file then In my opinion, yes, you should make another class just for dealing with reading and writing to file among other things. This sort of thing is best suited for a database since it is much easier to retrieve data specific queries when desired. – DevilsHnd - 退職した May 19 '18 at 23:00

2 Answers2

0

What you are looking for is File Handling in Java. You could have easily looked it up on the web.
Anyways,
You are already using System.out.println() and scanner.nextLine() for I/O,where

System is a Java Class.
out is member of System class.
println is the method.

These are used for Reading/Writing variables from/to console.

Similarly to Read/Write from Files, you need to use FileWriter, FileReader or FileInputStream,FileOutputStream classes, depending on what you are dealing with, binary data or ASCII text.

Here are some references for you to learn :

GeeksforGeeks File Handling in Java
File Handling writing objects

Loner
  • 166
  • 1
  • 4
  • 10
0

I suggest using Customer methods and fields for operations concerning a particular customer, this will encapsulate behaviour common to all customers. You need to have the Customer class look like this:

public class Customer {
    String cardID;
    String name;
    String address;

    public Customer(String cardID, String name, String address) {
        this.cardID = cardID;
        this.name = name;
        this.address = address;
    }
}

Then you can create a method saveToFile inside this class:

public void saveToFile(String fileToWrite) {
    List<String> lines = Arrays.asList(cardID, name, address);
    Path file = Paths.get(fileToWrite);
    try {
        Files.write(file, lines);
    } catch (IOException ex) {
        System.err.println("Error writing to " + file);
    }
}

(for other approaches to creating and writing to text files, see this question). After creating this method, use it in your main method:

// register customer                
Customer customer = new Customer(cardID, name, address);
customer.saveToFile(cardID + ".txt");

Here, a customer is created by the non-default constructor shown above (in fact, the default one doesn't exist now) and saved to the file in current directory with the name defined by the customer's cardID (to distinguish between files corresponding to different customers, you are free to change the naming scheme to whatever you like).

By the way, do you really want to keep all records for such kind of application in text files? Generally, databases are used for such purposes, because it is more convenient and secure.

John McClane
  • 3,498
  • 3
  • 12
  • 33