1

I have another class that has a main method and has the JFrame. When I run my code it stops after the user selects a cabin, and doesn't run the rest of my code such as asking the user for their first name. After it goes through my action listener it does not move forward.

  private class CabinListener implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      Object source = event.getSource();
      String setCabin;
        if(source == cabin1)
        {
        setCabin = "Cabin 1-1";
        System.out.print(setCabin);
        }
        else if(source == cabin2)
        {
        setCabin = "Cabin 1-2";
        System.out.print(setCabin);
        }
        else if(source == cabin3)
        {
        setCabin = "Cabin 1-3";
        System.out.print(setCabin);
        }
        else if(source == cabin4)
        {
        setCabin = "Cabin 1-4";
        System.out.print(setCabin);
        }
        else if(source == cabin5)
        {
        setCabin = "Cabin 1-5";
        System.out.print(setCabin);
        }
        else if(source == cabin6)
        {
        setCabin = "Cabin 1-6";
        System.out.print(setCabin);
        }
        else if(source == cabin7)
        {
        setCabin = "Cabin 1-7";
        System.out.print(setCabin);
        }
        else if(source == cabin8)
        {
        setCabin = "Cabin 1-8";
        System.out.print(setCabin);
        }
        else if(source == cabin9)
        {
        setCabin = "Cabin 1-9";
        System.out.print(setCabin);
        }
        else if(source == cabin10)
        {
        setCabin = "Cabin 1-10";
        System.out.print(setCabin);
        }
        else if(source == cabin11)
        {
        setCabin = "Cabin 2-1";
        System.out.print(setCabin);
        }
        else
        {
        setCabin = "Cabin 2-2";
        System.out.print(setCabin);
        }
        Scanner scan = new Scanner(System.in);

    Destination dest1 = new Destination("Jmaica", "stop1", 200.14, "stop2", 250.12, "stop3", 234.99, "stop4", 300.12);
    Destination dest2 = new Destination("Key largo", "stop1.1", 253.2, "stop2.1", 231.2, "stop3.1", 342.3, "stop4.1", 324.0);
    Destination dest3 = new Destination("Hawaii", "stop1.2", 258.2, "stop2.2", 265.2, "stop3.2", 348.3, "stop4.2", 334.4);
    Destination dest4 = new Destination("Carribean", "stop1.3", 158.2, "stop2.3", 325.2, "stop3.3", 768.3, "stop4.3", 564.4);
    Destination cust_dest = new Destination("", "", 0, "", 0, "", 0, "", 0); //creates seperate object that will adopt the properties of the destination that the user chooses.
    Account customer1 = new Account("","","","",0,"","","","","","");


    System.out.println("Please enter our first name"); //gets Customers first name and assigns it to customer1 obj
    String fName_scan = scan.nextLine();
    customer1.setFname(fName_scan);

    System.out.println("Please enter your last name");
    String lName_scan = scan.nextLine();
    customer1.setLname(lName_scan);

    System.out.println("Please enter your city");
    String city_scan = scan.nextLine();
    customer1.setCity(city_scan);

    System.out.println("Please enter your state");
    String state_scan = scan.nextLine();
    customer1.setState(state_scan);

    System.out.println("Please enter how many members are in your party");

    int party_scan = scan.nextInt();
    customer1.setGroup(party_scan);

    System.out.println("Please choose your cabin");

    System.out.println("Please enter the corresponding number for your desired destination" + " \t 1. Jamaica" + " \t 2. Key largo" +
                      " \t 3. Hawaii" + " \t 4. Carribean");
    int dest_scan = scan.nextInt();
    if(dest_scan == 1)
    {
      System.out.println(dest1);
      cust_dest = dest1; //adopts the properties of destination1 to the customer destination, so we can use getter methods to choose the users possible excursions 
    }
    else if(dest_scan == 2)
    {
      System.out.println(dest2);
       cust_dest = dest2;
    }
    else if(dest_scan == 3)
    {
      System.out.println(dest3);
       cust_dest = dest3;
    }
    else if(dest_scan == 4)
    {
      System.out.println(dest4);
       cust_dest = dest4;
    }

  double excursionTotal = 0.0;

  System.out.println("Now choose your excursions");
  String exit_loop = "no";
  String excursion_choice;
  System.out.print("Enter the corresponding number to add an excursion to your package. Type X when finished:");

     while(exit_loop.equals("no"))
             {
              excursion_choice = scan.nextLine();
              if(excursion_choice.equals("x") || excursion_choice.equals("X"))
                exit_loop = "yes";
              else if(excursion_choice.equals("1"))
              {
                customer1.setExcursion1(cust_dest.getExcursion1()); //assigns customers excursion 1 from the customer destination object; which was adopted when the user chose a destination.
              excursionTotal = excursionTotal + cust_dest.getExcursion1_price(); // gets excursion price from customer destination object for excursion 1.
              }

              else if(excursion_choice.equals("2"))
              {
                customer1.setExcursion2(cust_dest.getExcursion2());
              excursionTotal = excursionTotal + cust_dest.getExcursion2_price();
              }
              else if(excursion_choice.equals("3"))
                {
                customer1.setExcursion3(cust_dest.getExcursion3());
                excursionTotal = excursionTotal + cust_dest.getExcursion3_price();
                }

              else if(excursion_choice.equals("4"))
                {
                customer1.setExcursion4(cust_dest.getExcursion4());
                excursionTotal = excursionTotal + cust_dest.getExcursion4_price();

                }







  }
     System.out.println(customer1);
     System.out.print(excursionTotal);

    }
  }
}
Zachary Cheshire
  • 39
  • 1
  • 1
  • 8
  • Can you try to reduce your code to the problem itself? You are posting 300+ loc but your issue is probably contained in only a couple of them... If you are not sure how to do it, go back to the basics, create just a simple JPanel with one JRadioButton and one ActionListener and see how it behaves. Most likely you will see the answer there! If not, it will make it readable and encourage the community to help you. – migueldiab Mar 30 '15 at 19:54
  • @migueldiab After it goes through my action listener it does not move to the next lines. I edited off the top part of my code. – Zachary Cheshire Mar 30 '15 at 20:41

2 Answers2

1

Because in the CabinListener you have to use equals method and not '=='

ex:

   else if(source.equals(cabin8))

see: What is the difference between == vs equals() in Java?

Community
  • 1
  • 1
vathek
  • 531
  • 2
  • 9
0

As vathek said, have you checked that you are not using the == incorrectly?

Object source = event.getSource(); 
/**
 * You should probably think about casting this into
 * the right type of object, then checking if the source
 * is the exact same object in order to be able to use == as intended.
 */
  String setCabin;
    if(source == cabin1)
    {
    setCabin = "Cabin 1-1";
    System.out.print(setCabin);
    }
/**
 * Then you are creating a Java Swing app 
 * then waiting from input from console?
 * Are you sure this is right?
 */

Scanner scan = new Scanner(System.in);

migueldiab
  • 82
  • 1
  • 4