So my code does everything I want it to do with the exception of when writing to storage I am not sure how to make true and false write as 1 and 0 respectively. I do not know if it's my syntax or my placement within my program...Kind of lost on this. I attempted to do this with the following code:
if (true)
{
write = 1;
}
else if (false)
{
write = 0;
}
The program compiles fine but of course that does not mean results.
My full code is below. Any suggestions/pointers are greatly appreciated.
package airlinereservationsystem;
import java.awt.BorderLayout;
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.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class ReservationSystem extends JFrame implements ActionListener
{
private int SC = 12;
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 ArrayList<Boolean> seatList = new ArrayList<Boolean>(SC);
String fileName = "c:/temp/projectData.text";
public ReservationSystem()
{
super("Air Maybe 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<SC;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();
}
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e)
{
String confirm = "Are you sure you want to book this seat?";
int write;
for (int i = 0; i < SC; i++)
{
if (e.getActionCommand( ).equals("Seat " +(i + 1)))
{
int choice = JOptionPane.showConfirmDialog(null, confirm, "Confirm",
JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.YES_OPTION)
{
buttonArray[i].setText("Booked");
seatList.set(i, false);
if (true)
{
write = 1;
}
else if (false)
{
write = 0;
}
}
}
if (e.getActionCommand( ).equals("Booked"))
{
JOptionPane.showMessageDialog(null, "This seat is already booked. Please choose another.");
}
if (e.getActionCommand( ).equals("Click to Exit"))
{
int choice = JOptionPane.showConfirmDialog(null, "Do you want to exit the reservation screen?",
"Click Yes or No:", JOptionPane.YES_NO_CANCEL_OPTION);
if(choice == 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 <SC; count++)
{
outputStream.println(seatList.get(count));
}
outputStream.close( );
}
}
Main Class:
package airlinereservationsystem;
import javax.swing.SwingUtilities;
public class Main
{
public static void main (String[] args) {
SwingUtilities.invokeLater (new Runnable ()
{
public void run () {
ReservationSystem gui = new ReservationSystem();
gui.setVisible(true);
}
});
}
}
Thanks again!