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!

Recommended Answers

All 3 Replies

if (true) { write = 1; } else if (false) { write = 0; }

The above condition always evaluates to true and write value will always remain 1; you should use a conditional statement which will return true or false in if() condition!
for example:
boolean b = true;
if(b==true)
something;
else
something;

Means you need to check the value of your variable againest true/false.
Hope this will help!
Thank you.

Remove line 81 to 88( the write code)
Edit line where u write to file as

outputStream.println(seatList.get(count)?1:0);

Here is the complete code of your ReservationSystem

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( ));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        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 (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)?1:0);
        }
        outputStream.close( );

}

}
commented: Very helpful. Solved my issue. +0

i
...
if(b==true)
...

Interesting to see that people are still doing this!
Why not go for it and code if (b==true==true==true)

b is a boolean. All you need is if (b)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.