d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Help Please! > Arrays - Applet
Add Reply New Topic New Poll
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 21 2014 07:25pm
I need help with creating a Airlines java applet that gives the user two choices: First Class or Economy?

The system has only one airplane with 10 seats: 1 to 5 for first class and 6 to 10 for economy. The applet has two buttons and a text field for boarding pass.
If the user clicked the First Class button, a currently availabe first class seat is printed in the boarding pass.
If the user clicked the Economy button, a currently available economy seat is printed in the boarding pass.
If the user selected section has no available seat, your program should ask if the user is OK with the other section seats.
If the user clicked OK, you print the boarding pass of the assigned seat. If the user clicked Cancel, print "Next flight leaves in 3 hours"

I only have the initial buttons/field setup and am clueless on how to proceed to the next step.
I am supposed to do a boolean method that shows that when the argument is true = first class (false = economy).
Can someone please help?

Here is what I have so far...

Code
import java.awt.*; // Container, FlowLayout
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;

public class Assign7 extends JApplet implements ActionListener
{
// graphical user interface components
JButton firstClassButton, economyButton, okButton, cancelButton;
JTextField infoField;

// set up GUI components
public void init()
{
// obtain content pane and change its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// create buttons and text fields
firstClassButton = new JButton( "First Class" );
firstClassButton.addActionListener(this);
container.add( firstClassButton );

economyButton = new JButton( "Economy" );
economyButton.addActionListener(this);
container.add( economyButton );

infoField = new JTextField(20);
infoField.setEditable(false);
container.add( infoField );

} // end method init

public int reserve(boolean firstClass)
{

}
// process button click
public void actionPerformed( ActionEvent actionEvent )
{


} // end method actionPerformed

}






This post was edited by Modification on Apr 21 2014 07:28pm
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 21 2014 08:25pm
Code
import java.awt.*; // Container, FlowLayout
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Assignment extends JApplet implements ActionListener
{
// set up for seats array
boolean[] seats = new boolean[11];

// graphical user interface components
JButton firstClassButton, economyButton;
JTextField infoField;

// set up GUI components
public void init()
{
// obtain content pane and change its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// create buttons and text fields
firstClassButton = new JButton( "First Class" );
firstClassButton.addActionListener(this);
container.add( firstClassButton );

economyButton = new JButton( "Economy" );
economyButton.addActionListener(this);
container.add( economyButton );

infoField = new JTextField(20);
infoField.setEditable(false);
container.add( infoField );

} // end method init

// method action performed
public void actionPerformed( ActionEvent actionEvent )
{
boolean firstClass = actionEvent.getSource() == this.firstClassButton;

int seat = reserve(firstClass);

// begin if statement for economy/first class
if (seat == -1)
{
String ok = JOptionPane.showInputDialog(null,
"Are you OK with " + (firstClass ? "Economy" : "FirstClass") +
" (click OK or Cancel)");

if (ok != null)
{
firstClass = !firstClass;
seat = reserve(firstClass);
}

} // end if statement

// if statements for info field

// if seats available
if (seat > 0)
{
infoField.setText("Boarding Pass: Seat " + seat +
(firstClass ? " FirstClass" : " Economy"));
} // end if seats available

// else if user cancels
else if (seat == -1)
{
infoField.setText("Next flight leaves in 3 hours");
} // end if user cancels

// extra credit if statement
else
{
infoField.setText("All the seats have been reserved");
firstClassButton.setEnabled(false);
economyButton.setEnabled(false);
} // end extra credit if statement

} // end if statements for info field

// method reserve boolean for seat numbers
public int reserve(boolean firstClass)
{
// range for first class
int start = 6;int end = 10;
boolean full = true;

//if statements

// if statement for first class
if (firstClass)
{
start = 1;
end = 5;
}

// counter for seats
for (int i = 1; i <= 10; i++)
{
if (seats[i] == 0)
{
if ((i >= start) && (i <= end))
{
seats[i] = true;
return i;
}
full = false;
} // end if statement (seats=0)
} // end counter for seats
return full ? -2 : -1;
} // end method reserve boolean
} // end Assignment


I managed to figure things out. But there is one problem.
The line near the bottom of the code (13 lines from the bottom to be exact) that says "if (seats[i] == 0) has an error saying "The operator == is undefined for the argument type(s) boolean, int"

how do i fix this?
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 21 2014 09:27pm
Quote (Modification @ Apr 21 2014 07:25pm)
Code
import java.awt.*;        // Container, FlowLayout
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Assignment extends JApplet implements ActionListener
{
// set up for seats array
boolean[] seats = new boolean[11];

// graphical user interface components
    JButton firstClassButton, economyButton;
    JTextField infoField;

// set up GUI components
    public void init()
    {
    // obtain content pane and change its layout to FlowLayout
        Container container = getContentPane();
        container.setLayout( new FlowLayout() );
       
    // create buttons and text fields
        firstClassButton = new JButton( "First Class" );
        firstClassButton.addActionListener(this);
        container.add( firstClassButton );
       
        economyButton = new JButton( "Economy" );
        economyButton.addActionListener(this);
        container.add( economyButton );
       
        infoField = new JTextField(20);
        infoField.setEditable(false);
        container.add( infoField );
       
    } // end method init
   
// method action performed
    public void actionPerformed( ActionEvent actionEvent )
    {
    boolean firstClass = actionEvent.getSource() == this.firstClassButton;
     
    int seat = reserve(firstClass);
   
    // begin if statement for economy/first class
    if (seat == -1)
    {
      String ok = JOptionPane.showInputDialog(null,
        "Are you OK with " + (firstClass ? "Economy" : "FirstClass") +
        " (click OK or Cancel)");
     
      if (ok != null)
      {
      firstClass = !firstClass;
      seat = reserve(firstClass);
      }
     
    } // end if statement
   
    // if statements for info field
   
    // if seats available
    if (seat > 0)
    {
      infoField.setText("Boarding Pass: Seat " + seat +
        (firstClass ? " FirstClass" : " Economy"));
    } // end if seats available
   
    // else if user cancels
    else if (seat == -1)
    {
      infoField.setText("Next flight leaves in 3 hours");
    } // end if user cancels
   
    // extra credit if statement
    else
    {
    infoField.setText("All the seats have been reserved");
    firstClassButton.setEnabled(false);
    economyButton.setEnabled(false);
    } // end extra credit if statement
   
    } // end if statements for info field
   
    // method reserve boolean for seat numbers
    public int reserve(boolean firstClass)
    {
  // range for first class
      int start = 6;int end = 10;
      boolean full = true;
     
      //if statements
     
      // if statement for first class
      if (firstClass)
      {
        start = 1;
        end = 5;
      }
     
      // counter for seats
      for (int i = 1; i <= 10; i++)
      {
        if (seats[i] == 0)
        {
          if ((i >= start) && (i <= end))
          {
            seats[i] = true;
            return i;
          }
          full = false;
        } // end if statement (seats=0)
      } // end counter for seats
      return full ? -2 : -1;
    } // end method reserve boolean
} // end Assignment


I managed to figure things out. But there is one problem.
The line near the bottom of the code (13 lines from the bottom to be exact) that says "if (seats[i] == 0) has an error saying "The operator == is undefined for the argument type(s) boolean, int"

how do i fix this?


i ended up removing the ==0 and it seemed to fix the error.
however whenever i run the program and I click either "First Class" or "Economy" it says "All the seats have been reserved."

anyone know why?


/edit I changed the"if (seats[i] == 0)" to "if (seats[i])"
and it seemed to fix the error but when I run it keeps saying "All the seats have been reserved"

so i tried to change it again from "if (seats[i])" to "if(!seats[0]".
it got rid of the "All the seats have been reserved" message but now the text field alternates from seat 1 and seat 6 when i press first class and economony (respectively).

I think I am supposed to disable the two buttons but how do i do that?

This post was edited by Modification on Apr 21 2014 09:42pm
Member
Posts: 23,261
Joined: Dec 17 2006
Gold: 18,129.00
Apr 22 2014 04:32pm
Quote (Modification @ Apr 21 2014 10:27pm)
i ended up removing the ==0 and it seemed to fix the error.
however whenever i run the program and I click either "First Class" or "Economy" it says "All the seats have been reserved."

anyone know why?


/edit  I changed the"if (seats[i] == 0)" to "if (seats[i])"
and it seemed to fix the error but when I run it keeps saying "All the seats have been reserved"

so i tried to change it again from "if (seats[i])" to "if(!seats[0]".
it got rid of the  "All the seats have been reserved" message but now the text field alternates from seat 1 and seat 6 when i press first class and economony (respectively).

I think I am supposed to disable the two buttons but how do i do that?


If I'm understanding your code correct, you're checking to see if that slot in "seat[i]" is not reserved meaning there has been no value allocated to that slot in the array. From my understanding, this means that you have not filled that seat slot. Is that correct?

If so, You need to change your code from "seats[i] == 0" to seats[i] == null. The commenting is very good, but I don't understand the structure of Java all too well, but I do very well with the logic.

Let me know.
Member
Posts: 19,234
Joined: Aug 13 2009
Gold: 33,940.00
Apr 22 2014 10:48pm
Quote (PureOwnage2 @ Apr 22 2014 03:32pm)
If I'm understanding your code correct, you're checking to see if that slot in "seat[i]" is not reserved meaning there has been no value allocated to that slot in the array.  From my understanding, this means that you have not filled that seat slot.  Is that correct?

If so,  You need to change your code from "seats[i] == 0" to seats[i] == null.  The commenting is very good, but I don't understand the structure of Java all too well, but I do very well with the logic. 

Let me know.


"The operator == is undefined for the argument type(s) boolean, null"

thats what it says if i change it to that.

/edit. i simply had to change the "!seats[0]" to "!seats[i]"

case closed. thanks for trying though.

This post was edited by Modification on Apr 22 2014 10:51pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll