Results 1 to 7 of 7
Thread: JAVA Project
- 12-06-2009, 10:01 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
JAVA Project
Hello,
I am having problems in my JAVA project using JFrame and I would appreciate any assistance.
I am writing a JAVA reservation system. When a user selects a seat button (1-15) they are prompted for an authorization code. After entering a 3 digit code, the seat button text changes to "Booked." The user can also select the "Booked" seat and if they enter the correct authorization code, it will change the seat text back to "Seat [1-15])" If they enter an incorrect authorization code, it prompts the user to contact customer service.
I have this working correctly. What doesn't work is if a user enters an incorrect authorization code to unlock the seat, any subsequent attempts to cancel any other seats prompts the user that they are entering the incorrect authorization code, even though they are entering the correct code.
Here is the method I am using:
Java Code: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))) { seatCounter++; int choice = JOptionPane.showConfirmDialog(null, confirm, "Confirm", JOptionPane.YES_NO_CANCEL_OPTION); if(choice == JOptionPane.YES_OPTION) { authorization = JOptionPane.showInputDialog("Enter a 3 digit authorization number"); seatAuthorization[i] = Integer.parseInt(authorization); if ((seatAuthorization[i] >= 100) && (seatAuthorization[i] <= 999)) { buttonArray[i].setText("Booked"); } if ((seatAuthorization[i] < 100) | (seatAuthorization[i] > 999)) { System.out.println("Seat Auth Code " + seatAuthorization[i]); JOptionPane.showMessageDialog(null, "Invalid authorization code. Please enter a 3 digit code.","", JOptionPane.INFORMATION_MESSAGE); } if (seatCounter == 12) { JOptionPane.showMessageDialog(null, "Unfortunately the flight is booked. Please choose a different flight.","", JOptionPane.INFORMATION_MESSAGE); } } } else if ((e.getActionCommand( ).equals("Booked")) && (seatCounter == i)) { seatCounter++; JOptionPane.showMessageDialog(null, "This seat is booked. Enter the authorization code to " + "cancel the reservation","", JOptionPane.INFORMATION_MESSAGE); authorization = JOptionPane.showInputDialog("Enter a 3 digit authorization number"); number[i] = Integer.parseInt(authorization); if (number[i] == seatAuthorization[i - 1]) { buttonArray[i - 1].setText("Seat " + i); JOptionPane.showMessageDialog(null, "Your reservation has been cancelled","", JOptionPane.INFORMATION_MESSAGE); seatCounter--; return; } if (number[i] != seatAuthorization[i - 1]) { JOptionPane.showMessageDialog(null, "Incorrect authorization number. Please contact customer service " + "to cancel the reservation","", JOptionPane.INFORMATION_MESSAGE); number[i] = 0; return; } } else if (e.getActionCommand( ).equals("Click to Exit")) { int choiceExit = JOptionPane.showConfirmDialog(null, confirmExit, "Confirm", JOptionPane.YES_NO_OPTION); if(choiceExit == JOptionPane.YES_OPTION) System.exit(0); } else System.out.println("Error in button interface"); } } }
-
It's hard to figure this sucker out out of context. One thing that bugs me is your use of the seatCounter variable. I assume that it keeps track of the number of booked seats, but are you sure that you're using it right? For instance, why check it in the first line here, and why increment it in the third line?
Java Code:else if ((e.getActionCommand( ).equals("Booked")) && (seatCounter == i)) { seatCounter++;
Last edited by Fubarable; 12-06-2009 at 11:27 PM.
-
- 12-13-2009, 10:15 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
ReadInput Problems
Thanks for your help Fubarable.
I believe I have everything sorted out (I was making the code more complicated than it had to be). However, I am having one last problem on reading input. Essentially when the program closes the user input gets written to a text file using 0/1 values to determine if the seat has been booked or not. I have this working correctly but I am having problems reading the the text file values into the program when it is launched. Here is my code so far (the teacher would like to keep all of this in one class):
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.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; 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( ); //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 readFromFile(){ try{ ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName)); int anInteger = inputStream.readInt(); while (anInteger >= 0){ anInteger = inputStream.readInt(); } System.out.println("End of reading from file"); inputStream.close(); } catch(FileNotFoundException e){ System.out.println("Error opening the file " + fileName); } catch(EOFException e){ System.out.println("Error reading the file " + fileName); System.out.println("Reached end of the file " + fileName); } catch(IOException e){ System.out.println("Error reading the file " + fileName); } } 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) System.exit(0); } else{ super.processWindowEvent(e); } } }
-
Are you "Matthew Schexnayder" who posted the question on JavaRanch:
Class Project: Airline Reservation System (Swing / AWT / SWT / JFace forum at JavaRanch)
- 12-13-2009, 10:24 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
No, my name is Scott :) but I believe he is in the same class at a different time.
-
My concern here is that you never replied to my initial post, seemingly never read it as you ignored any advice given (never used it or explained how it doesn't help), and only replied now 7 days later when you needed more help. I think you'll understand if I excuse myself from this thread. Hopefully someone else will pitch in. Good luck.
Last edited by Fubarable; 12-13-2009 at 10:32 PM.
Similar Threads
-
Java Project
By dessen in forum JCreatorReplies: 4Last Post: 04-18-2009, 05:38 PM -
Java Project
By Smirre in forum New To JavaReplies: 16Last Post: 11-17-2008, 09:37 PM -
java project
By gvdn in forum Advanced JavaReplies: 4Last Post: 07-13-2008, 07:45 AM -
Help Java project.
By mandrake446 in forum New To JavaReplies: 1Last Post: 11-27-2007, 12:52 AM -
Java Project Help
By Gambit17 in forum New To JavaReplies: 3Last Post: 11-05-2007, 12:53 PM
Bookmarks