-1

I have a code that atm checks if the array list has reached the size or not, if no I want it to to perform checks before adding anything else to the list. I have attempted it but cannot figure out why it does not work. below is my method.

private static void addToArrayList(String fruit, double no1, int no2, int no3) throws Exception {

    try {
        if (arraysList.size() <= 5) {

            int count = 0;
            for (StoringArray item : arraysList)
                if (item.equals("Apple")) {
                    ++count;
                    if (count > 2)
                        throw new IllegalArgumentException( "You cannot add more than 2 apples." ); //Instead of this I want a Joption pane pop up to give this error if it applies, but at the moment I am not sure but this code with the current code I have is not working.
                }
            {
                if ( arraysList.get( arraysList.size() - 1 ).equals("Banana") )
                    throw new IllegalArgumentException( "You have just added this please add something else and then add this if you want." ); }


            arraysList.add(new StoringArray(fruit, no1, no2, no3));
        }else{

            JOptionPane.showMessageDialog(contentPane, "You cannot added mroe than 6 elements.");


        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

I want the error messages to appear in a Joption Pane and I want to check the following errors;

Say the list includes Apples, Bananas, Oranges, PineApples, Grapes

1; I want to check whether the user given parameters no1, no2 and no3 meet the conditon I want i.e.

for (StoreCommands item : commandsList)
                    if (item.equals("Apple")) { 

}no1 need to be greater then 0, no2 needs to be less than 10 and no 3 needs to be less than 15. 2; If user tries to add two apples together in any order it should not be allowed, directly after one another. 3; If the user adds 2 Oranges, they should not be allowed and error message saying this should come up in JOption Pane message box.

If all the conditions are the array values get added to the array list. Thanks I hope I explained myself properly, I have been working on this problem for ages and cannot figure it out for some reason. Thanks again.

----------------edited-----------------with class that stores the arrayList.

public class StoreCommands {
       public String toString(){
              return Name + " " + Number1 + " " + Number2 + " " + Number3;
         }

    private String Name;
    private int Number1;
    private int Number2;
    private int Number3;


    public String getCommand() {
        return Name;
    }



    public double getcommandNOS() {
        return Number1;
    }



    public int getcommandVLW() {
        return Number2;
    }



    public int getcommandVRW() {
        return Number3;
    }



    public StoringArray(String fruitsNames, double fno1, int fno2, int fno3) throws Exception{
        Name =  fruitsNames;
        Number1 = (int) fno1;
        Number2 = fno1;
        Number3 = fno3;

        }

    }

2 Answers2

1

There are also some problems in your StoreCommands (?StoringArray) class and it doesn't compile.

1) The constructor is called StoringArray while the class is called StoreCommands.

2) You shouldn't accept a double value as second parameter and cast it to an int.

3) "Number2 = fno1;" inside the constructor should be "Number2 = fno2;" instead

4) You cannot compare your StoreCommands instance to a String value using equals. You need to compare to the String returned from the getCommand() method:

if (item.getCommand().equals("Apple"))

no1 need to be greater then 0, no2 needs to be less than 10 and no 3 needs to be less than 15. 2; If user tries to add two apples together in any order it should not be allowed, directly after one another. 3; If the user adds 2 Oranges, they should not be allowed and error message saying this should come up in JOption Pane message box.

perhaps something like this would do the job:

public static String getErrorMessage(List<StoreCommands> commands, String fruitsName, int no1, int no2, int no3) {
    if (no1 <= 0 || no2 >= 10 || no3 >= 15) {
        return "Some Error message...";
    }
    String previous = null;
    int orangeCount = 0;
    for (StoreCommands c : commands) {
        if (fruitsName.equals("Apple") && previous != null && previous.equals("Apple")) {
            return "Some Error message...";
        } else if (c.getCommand().equals("Orange")) {
            orangeCount++;
        }
        previous = c.getCommand();
    }
    return fruitsName.equals("Orange") && orangeCount == 1 ? "Some Error message" : null;
}
Balder
  • 8,623
  • 4
  • 39
  • 61
  • I am accepting the double value because the user can type in decimal numbers for that parameter, but the way I use it will be as int hence I cast it. – user3282615 Feb 07 '14 at 07:29
  • Forgot to check for the fruitNames in my code so I changed this. – Balder Feb 07 '14 at 07:32
  • can I added more than 1 return statement in a method? – user3282615 Feb 07 '14 at 07:54
  • Yes you can. But it is sometimes considered bad style to have too many return statements in one method... it really depends - see http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – Balder Feb 07 '14 at 11:30
0

your class name is StoreCommands

but you have declared constructor named StoringArray

public StoringArray(String fruitsNames, double fno1, int fno2, int fno3) throws Exception
{
        Name =  fruitsNames;
        Number1 = (int) fno1;
        Number2 = fno1;
        Number3 = fno3;

}

replace this by

public StoreCommands(String fruitsNames, double fno1, int fno2, int fno3) throws Exception
{
         Name =  fruitsNames;
        Number1 = fno1;  //you do not need to cast int because both are int
        Number2 = fno1;
        Number3 = fno3;

}

in for loop change the conditional logic

for (StoringArray item : arraysList)
        if (item.getCommand().equals("Apple")) 
         {

         }

.. it should works now if your other logic and code is ok

java seeker
  • 1,246
  • 10
  • 13