Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reading and Writing Text Files

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading and Writing Text Files

    Hello Everyone,

    I am having a bit of trouble with a project I am working on. I am creating an Airline reservation system in which a user can select buttons 1-12 to book a seat. After booking the seat they receive an authorization number. The user can also select a booked seat to unbook if they enter the correct authorization number.
    When a user goes to exit the program, the results of the reservation are saved to a text file (1 for unbooked, 0 for booked).

    Up to this point I have the code working correctly. My only problem is that I also need to read input from the same text file so that when a user opens the program the seats are booked/unbooked in the same way they designated.

    Any help would be greatly appreciated

    Here is my code:

    package airlinereservationsystem;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import java.awt.event.WindowEvent;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class ReservationSystem extends JFrame implements ActionListener
    {
        private int seatCounter = 0;
        private JLabel userInstructions = new JLabel("Click the button" +
                " representing the seat you want " +
            "to reserve the seat");
        private JButton[] buttonArray = new JButton[12];
        private JButton exitButton = new JButton("Click to Exit");
        private String authorization;
        private int[] bookedNumber = new int[12];
        private ArrayList<Boolean> seatList = new ArrayList<Boolean>(12);
        String fileName = "c:/temp/projectData.txt";
     
        public ReservationSystem()
        {
            super("SouthEast Airline Reservation System");
            addWindowListener(new WindowDestroyer( ));
            Container contentPane = getContentPane( );
            contentPane.setLayout(new BorderLayout( ));
            JPanel InstructionsPanel = new JPanel( );
            JPanel buttonPanel = new JPanel( );
            JPanel exitPanel = new JPanel( );
            ScannerReadFile();
     
            //add listener to the exit button
            exitButton.addActionListener(this);
     
     
            InstructionsPanel.add(userInstructions);
            contentPane.add(InstructionsPanel, BorderLayout.NORTH);
     
            buttonPanel.setLayout(new GridLayout(4,3));
     
            for (int i=0;i<12;i++)
            {
                buttonArray[i] = new JButton("Seat " + (i+1));
                buttonArray[i].addActionListener(this);
                buttonPanel.add(buttonArray[i]);
                seatList.add(i, true);
            }
     
            contentPane.add(buttonPanel,BorderLayout.CENTER);
            exitPanel.add(exitButton);
            contentPane.add(exitPanel,BorderLayout.SOUTH);
            pack();
     
     
        }
     
    public void actionPerformed(ActionEvent e)
    {
        String confirmExit = "Are you sure you want to exit?";
        String confirm = "Are you sure you want to book this seat?";
        for (int i = 0; i < 12; i++) {
        if (e.getActionCommand().equals("Seat " + (i + 1))) {
           int choice = JOptionPane.showConfirmDialog(null, confirm, "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
           if (choice == JOptionPane.YES_OPTION) {
              JOptionPane.showMessageDialog(null, "Your authorization number is 197", "", JOptionPane.INFORMATION_MESSAGE);
              buttonArray[i].setText("BOOKED");
              seatList.set(i, false);
              buttonArray[i].setToolTipText("Seat Occupied");
              buttonArray[i].setForeground(Color.red);
              buttonArray[i].setActionCommand("Booked");
              seatCounter++;
              if (seatCounter == 12) {
                 JOptionPane.showMessageDialog(null, "Unfortunately the flight is booked. Please choose a different flight.", "", JOptionPane.INFORMATION_MESSAGE);
              }
              }
              }
        if (e.getActionCommand().equals("Booked")) {
           JOptionPane.showMessageDialog(null, "This seat is booked. Enter the authorization code to " + "cancel the reservation", "", JOptionPane.INFORMATION_MESSAGE);
           authorization = JOptionPane.showInputDialog("Enter the authorization number");
           bookedNumber[i] = Integer.parseInt(authorization);
           if (bookedNumber[i] == 197) {
              buttonArray[i] = (JButton) e.getSource();
              JOptionPane.showMessageDialog(null, "Your reservation has been cancelled", "", JOptionPane.INFORMATION_MESSAGE);
              buttonArray[i].setText("Available");
              buttonArray[i].setForeground(Color.black);
              seatCounter--;
              return;
            }
            if (bookedNumber[i] != 197) {
               JOptionPane.showMessageDialog(null, "Incorrect authorization number. Please contact customer service " + "to cancel the reservation", "", JOptionPane.INFORMATION_MESSAGE);
               return;
            }
         }
         }
         if (e.getActionCommand( ).equals("Click to Exit")){
            int choiceExit = JOptionPane.showConfirmDialog(null, confirmExit, "Confirm",
            JOptionPane.YES_NO_OPTION);
            if (choiceExit == JOptionPane.YES_OPTION){
                writeToFile();
                System.exit(0);
            }
            }
     
    }
    public void writeToFile(){
        PrintWriter outputStream = null;
        try{
          outputStream = new PrintWriter(fileName);
            }
            catch(FileNotFoundException e){
               System.out.println("Error opening the file " + fileName);
               System.exit(0);
            }
        for (int count = 0; count <12; count++){
            outputStream.println(seatList.get(count)?1:0);
        }
            outputStream.close( );
     
    }
    public void ScannerReadFile(){
        File filePath = new File("c:/temp/projectData.txt");
        try{
        Scanner scanner = new Scanner(filePath);
            while (scanner.hasNextLine()){
            String line = scanner.nextLine();
            System.out.println(line);
            }
        }
        catch (FileNotFoundException e) {
        e.printStackTrace();
        }
    }
    protected void processWindowEvent(WindowEvent e)
    {
        if (e.getID() == WindowEvent.WINDOW_CLOSING){
           int exit = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?");
           if (exit == JOptionPane.YES_OPTION)
               writeToFile();
               System.exit(0);
        }
           else{
              super.processWindowEvent(e);
            }
     
    }
     
    }


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading and Writing Text Files

    From what I can see, this produces a file with 0's and 1's, each being on a different line, correct? ...so in order to read those back in (which you already have setup in ScannerReadFile() ... btw, it's good practice to start method names with a lower-case character... where was I ? ...ah yes, so in order to read in the numbers...) and apply them back onto the buttons, you would edit your loop inside "ScannerReadFile()" to be something like:

    int seat = 0;
    while (scanner.hasNextLine()){
            String line = scanner.nextLine();
            if (line.equals("1")) {
                // set the seat to available
                buttonArray[seat].setText("Seat " + (seat+1));
            } else {
                // set the seat to booked
                buttonArray[seat].setText("BOOKED");
            }
            seat++;
    }
    Last edited by Nilhanth; March 1st, 2010 at 07:24 PM.

Similar Threads

  1. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM
  2. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM
  3. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM
  4. Reading IEEE float values from files.
    By username9000 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 30th, 2009, 12:56 PM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM