Go Back   Java Programming Forums > Java Standard Edition Programming Help > File I/O & Other I/O Streams


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 15-12-2009, 02:49 AM
Junior Member
 

Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
kappasig84 is on a distinguished road
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:

Java 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);
        }
  
}

}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 01-03-2010, 11:16 PM
Junior Member
 

Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Nilhanth is on a distinguished road
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:

Java Code
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; 01-03-2010 at 11:24 PM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading from a text file. Help needed urgently. TheAirPump File I/O & Other I/O Streams 2 14-12-2009 10:16 PM
Writing clone() method for LinkedList vluong Collections and Generics 6 27-10-2009 12:41 PM
Reading many files using a scanner jayjames90 File I/O & Other I/O Streams 2 22-10-2009 09:35 PM
Reading IEEE float values from files. username9000 File I/O & Other I/O Streams 3 30-06-2009 05:56 PM
I need help writing this program kev2000 Algorithms & Recursion 5 04-06-2009 08:14 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:51 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.