|
||
|
|||
|
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);
}
}
}
|
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |