0

These are my two classes. Now I've made the method static and removed static from addMember(). Am I doing correctly as you told me. Now I am having an error but in the for loop (non-satic variable cannot be referenced from a static context). Help me pls. Thank you for already helping me a lot.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
 ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     static boolean alreadyExists(GymUser user)
    {
        for(GymUser listEntry : UserListIn)
        {
            if(listEntry.getID() == user.getID())
            {
                return true;
            }
        }
         return false;
    }
    
    
    
    
    
      // method for adding a new member to the list

      addMember(ArrayList<GymUser> UserListIn)
    {

        //Primary Key
        // int UserrID = NextID;
        // NextID++;
        /*TODO When loading Set Next ID greatest id +1*/




        String tempUserrID;
        String tempID;
        String tempName;
        String tempSurname;
        int tempAge;
        String tempAddress;
        int tempPhone;
        String tempDuration;
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");
        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();
        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();
        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();
       
      
        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration); 
        

            boolean b = alreadyExists(newGymUser );
        if (b) {
                    System.out.println("Cannot insert existing user");
                } 
                else 
                {
                        UserListIn.add(newGymUser );
                }
}

My other class that contains methods and constructor. Do I have to change something here.

/**
 * This class contains instance variables and method(members)
 * 
 */
import java.io.*;
public class GymUser implements Serializable
{
        //these are instance variables
     

        private String m_UserrID;
        private String m_UserName;
        private String m_UserID;
        private String m_UserSurname;
        private String m_UserAddress;
        private int m_UserPhone;
        private int m_UserAge;
        private String m_UserDuration;
        
        //this is the constructor
        public GymUser(String UserrID,String UserName,String UserID,String UserSurname, String UserAddress, int UserPhone, int UserAge,String UserDuration)
        {
            m_UserrID =  UserrID;
            m_UserName = UserName;
            m_UserSurname = UserSurname;
            m_UserAddress = UserAddress;
            m_UserPhone = UserPhone;
            m_UserAge= UserAge;
            m_UserID = UserID;
            m_UserDuration = UserDuration;
        }
        
        
        public String getM_ID()
       {
           return m_UserrID;
       }
       // method to get the membership duration
       public String getDuration()
       {
           return m_UserDuration;
       }
       
       // method to get the name
       public String getName()
       {
           return m_UserName;
       }
           
       //method to set name
       public void setName(String UserName)
       { 
            m_UserName = UserName;
       }
           
        
       
        //method to return age
        public int getAge()
        {
            return m_UserAge;
            }
            
          //method to set age
        public void setAge(int UserAge)
        {
            m_UserAge =UserAge;
            }   
            
        
        //method to return address
        public String getAddress()
        {
            return m_UserAddress;
            }
            
         
         //method to set address
        public void setAddress(String UserAddress)
        {
            m_UserAddress=  UserAddress;
            }
            
        //method to return phone
        public int getPhone()
        {
            return m_UserPhone;
        }
        
        //method to set phone
        public void setPhone(int UserPhone)
        {
            m_UserPhone = UserPhone;
        }
        
        //method to return suname
        public String getSurname()
        {
            return m_UserSurname;
        }
        
        //method to set surname
        public void setSurname(String UserSurname)
        {
            m_UserSurname = UserSurname;
        }
        
        //method to get name
        public String getID()
        {
            return m_UserID;

        }
}
0

Sorry I forgot to paste it method addMember() is public and void. I still can't manage to fix the error. I am having an error in the line for(GymUser listEntry : UserListIn)
Can you help me with my programming pls? Thanks.

0

Remove the static from alreadyExists(). That method only needed to be static if addMember() was static. You're getting the current error because the static alreadyExists() method is trying to use the UserListIn variable, which is not static.

Static methods can only make use of static class member variables and can only call static methods unless they create or already have access to an instance of the class for that method.

edit: Bottom line - remove all occurrences of the word 'static' from that class.

0

I've managed that error of the static. Now I am having no syntax errors and the program is running but the primary key still is not working. When I am inputting the same ID no. I am having no error and I can't even see my mistake because everthing is working and according to me the logic is right. Now I've been trying to alter the code to see if it works but the same. Can you help me pls?
These are my two type of codes. Are they correct? (both are not having syntax errors, but I am having not having that errors when entering the same ID no.). Thanks.

code 1.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
     // create an empty list to hold Cars
     static ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     
     public ManipulateGymUser (ArrayList<GymUser> arr)
     {
         UserListIn = arr;
     }

  // method for adding a new member to the list

     public static void addMember(ArrayList<GymUser> UserListIn)
    {





        String tempUserrID;
        String tempID = null;
        String tempName = null;
        String tempSurname = null;
        int tempAge='0';
        String tempAddress=null;
        int tempPhone = '0';
        String tempDuration = null;
        
        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration);
          
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");
        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();
        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();
        
     // primary key.   
        
     boolean b=false;
     for(GymUser listEntry : UserListIn)
        {
            if(tempID.equals(listEntry.getID()))
            {
                b = true;
            }
        
                else  b= false;
        
        }
        
        if (b== true) {
                    System.out.println("Cannot insert existing user");
                } 
                else 
                {
                        UserListIn.add(newGymUser );
                }
                
        
        
        
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();
        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();

}

This is the code that you helped me with yesterday.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
     // create an empty list to hold Cars
    static ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     
     public ManipulateGymUser (ArrayList<GymUser> arr)
     {
         UserListIn = arr;
     }

    

     static boolean alreadyExists(GymUser user)
    {
        for(GymUser listEntry : UserListIn)
        {
            if(listEntry.getID() == user.getID())
            {
                return true;
            }
        }
         return false;
    }
    
    
    
    
    
      // method for adding a new member to the list

     public static void addMember(ArrayList<GymUser> UserListIn)
    {
 String tempUserrID;
        String tempID;
        String tempName;
        String tempSurname;
        int tempAge;
        String tempAddress;
        int tempPhone;
        String tempDuration;
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");
        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();
        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();
        System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();
        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();
       
      
        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration); 
        

            boolean b = alreadyExists(newGymUser );
        if (b) {
                    System.out.println("Cannot insert existing user");
                } 
                else 
                {
                        UserListIn.add(newGymUser );
                }

}
0

When you are calling addMember, you are inserting the new user inside the arrayList that you pass as argument, but the alreadyExists method checks the arrayList that you have declared in your class. Meaning you are adding in one arrayList nad checking another:

Remove ALL static declarations.
The constructor must be empty with no argument. You are declaring an array list at the beging of you class.

The addMember does not need an argument either. You already have a arrayList in you class.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
     // create an empty list to hold Cars
     ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     
     public ManipulateGymUser ()
     {
     }

  // method for adding a new member to the list

     public void addMember()
    {
        String tempUserrID;
        String tempID = null;
        String tempName = null;
        String tempSurname = null;
        int tempAge='0';
        String tempAddress=null;
        int tempPhone = '0';
        String tempDuration = null;

//Read from the keyboard and add the values to the above varaibles

Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");

        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();

        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();


        System.out.print("Enter Name: ");
        tempName = keyboard.next();

        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();

        System.out.print("Enter Address:");
        tempAddress = keyboard.next();

        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();

        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();

        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();

        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration);
          
        
     // primary key.   
        
     boolean b = alreadyExists(newGymUser );
        
        if (b== true) {
                    System.out.println("Cannot insert existing user");
                } 
                else 
                {
                        UserListIn.add(newGymUser );
                }
    

}

public boolean alreadyExists(GymUser gUser) {
  for (int i=0;i<UserListIn.size();i++) {
    GymUser temp = (GymUser)UserListIn.get(i);
    if (temp.getId()=gUser.getId()) {
       return true;
    }
  }
  return false;
}

Then in another class (or in the same it doesn't matter) create an innstace of ManipulateGymUser and call the above methods. Fell free to add other methods as well for getting the elements of the ArrayList. In the alreadyExists method I am sure about the syntax(don't remember the entire API of ArrayList), you can use any code you like (the one you had).

Put this inside the main

ManipulateGymUser  mgu = ManipulateGymUser ();
mgu.addMember();

By the way if run the program, then the program finishes and then you run it again, of course the ArrayList will be empty. You must call more than once the addMember() inside the main. Perhaps write a menu using the Scanner in order to ask the user if he wants to add a user, or end the programm. Put it inside a while(). If you want to stop adding when break from the while() or make the boolean false:

boolean done=false;
while(!done) {
  //read From keyboard
 //if input equals Y or something else (whatever you like) then call addMember 
//else if user wants to exit: done=true;
}
0

This version will not work correctly.

boolean b=false;
     for(GymUser listEntry : UserListIn)
        {
            if(tempID.equals(listEntry.getID()))
            {
                b = true;
            }
                else  b= false;
        }

The result will be set based entirely on whether or not the last entry in the list has the same Id. So only set the flag to true if they are equal - don't set it to false if they aren't (it's false to begin with until a match is found).

Additionally, this piece of code at the end of addMember()

System.out.print("Enter Name: ");
        tempName = keyboard.next();
        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();
        System.out.print("Enter Address:");
        tempAddress = keyboard.next();
        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();
        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();
        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();

Is just collecting info from the keyboard and not doing anything with it at all. So if that is where you are entering the info with the dupe id, it certainly won't check for that Id in your list.

As for the second, don't use == to compare string values. Use .equals()

(listEntry.getID().equals(user.getID())

Why you are using a string to hold an int Id value is beyond me, but if they are strings you have to use .equals(). I did not notice you declared those as strings earlier, so that part slipped by.

You also do not show how you are using this class. There could also be some issues with the way your driver class is calling addMember(). I see you chose to ignore the suggestion to ditch the static declarations as well.

At this point, you should learn to place System.out.println() messages in your programs to help you debug them. Use them to output the values of variables at important points so you can see exactly what the program is doing as it runs. This feedback will save you a lot of time over just staring at the code trying to see what is wrong with it.

Edit: javaAddict posted in the interim. He's correct. Ditch the UserListIn parameter on that method. It's occluding the variable of the same name in your class.

0

Hey guys sorry giving you so much troube but I'm still learning java. I tried the following and I am also showing you my main class. What do I have to do now. I know it's still not correct but I am having an error in the if statment unexpected type. I am not removing static as you told me since in my main I am working with static. Can you show me from my own coding please. Sorry to ask but where I am having that error I think I have to use the .equals..yes? Thank you very much.

This is part of my main.

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
public class ArrayList_Member
{
     public static void main (String args[])
    {
ArrayList<GymUser> arr = new ArrayList<GymUser>();
        Scanner keyboard = new Scanner(System.in);

 boolean Exit = false;
        do {
 System.out.println("---------------------");
            System.out.println("|  ** MAIN MENU **   |");
            System.out.println("|  1.Gym menu        |");
            System.out.println("|  2.Pool menu       |");
            System.out.println("|  3.Courts          |");
            System.out.println("|  4.Pool and Gym    |");
            System.out.println("|  5.Transactions    |");
            System.out.println("|  6.Exit            |");
            System.out.println("---------------------");

            int optionNumber = scanner.nextInt();

            switch ( optionNumber )
            {
                case 1:
                        ArrayList_Member.ShowGymMenu(arr);
                        break;

               case 2:
                         ArrayList_Member.ShowPoolMenu(arrr);
                         break;
                         
               case 3:
                         ArrayList_Member.ShowCourtMenu(a);
                         break;
                         
              case 4:
                         ArrayList_Member.ShowBothMenu(arr);
                         break;
              case 5:
                         ArrayList_Member.ShowTransaction(Tran);
                         break;
               case 6:
                       Exit = true;
                       break;
            }

        }
        while (Exit == false);

        System.out.println("Goodbye");
}


static void ShowGymMenu(ArrayList<GymUser> arr )
 {
      
    boolean Back = false;
  //  do
   // {
                        System.out.println("       Gym Menu         ");
                        System.out.println(" -----------------------");
                        System.out.println("|  1.Add Member         |");
                        System.out.println("|  2.Remove Member      |");
                        System.out.println("|  3.Edit Member        |");
                        System.out.println("|  4.List All           |");
                        System.out.println("|  5.Save               |");
                        System.out.println("|  6.Gym Equipment list |");
                        System.out.println("|  7.Gym Rates          |");
                        System.out.println("|  8.Back               |");
                        System.out.println(" -----------------------");

                        System.out.println("Make your choice ");
                        System.out.println(" ");

                        Scanner scanner = new Scanner(System.in); 
                        int optionNumber = scanner.nextInt();



                    switch ( optionNumber )
                    {

                     case 1: ManipulateGymUser.addMember(arr);
                            break;
                     case 2: ManipulateGymUser.removeMember(arr);
                             break;
                    case 3: ManipulateGymUser.editMember(arr);
                            break;
                   case 4:  ManipulateGymUser.listAll(arr);
                            break;
                   case 5:  ManipulateGymUser.writeList(arr);
                            break;
                  case 6 :  Other.GymEqiupment();  // write to the file
                            break;
              //    case 7 : Other.GymRates();  // write to the file
                //           break;
                case 8:  Back = true;
                  default  : System.out.print ("\nPlease choose a number from 1 - 9 only\n ");
                        }
  //}
 // while(Back == false);
 }
}

Now this is the class I am having trouble because of primary key.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
     // create an empty list to hold members
     static ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     
     public ManipulateGymUser (ArrayList<GymUser> arr)
     {
         UserListIn = arr;
     }
     public static void addMember(ArrayList<GymUser> UserListIn)
    {

         String tempUserrID;
        String tempID = null;
        String tempName = null;
        String tempSurname = null;
        int tempAge='0';
        String tempAddress=null;
        int tempPhone = '0';
        String tempDuration = null;

//Read from the keyboard and add the values to the above varaibles

    Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");

        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();

        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();


        System.out.print("Enter Name: ");
        tempName = keyboard.next();

        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();

        System.out.print("Enter Address:");
        tempAddress = keyboard.next();

        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();

        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();

        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();

        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration);
          
        
     // primary key.   
        
     boolean  b = alreadyExists(newGymUser );
        
        if (b== true) {
                    System.out.println("Cannot insert existing user");
                } 
                else 
                {
                        UserListIn.add(newGymUser );
                }
    

}

        public static boolean alreadyExists(GymUser gUser)
  {
        for (int i=0;i<UserListIn.size();i++)
    {
        GymUser temp = (GymUser)UserListIn.get(i);
        if (temp.getID()=gUser.getID())                     //error here
        {
        return true;
        }
     }
    return false;

  }
0

I have modified the code and used the . equals instead and I solved the error. But still when running the program I still not having an error when entering the same ID. Do you know why so that I solve this problem cause I'm really getting tired with it and I have to hand this assigment soon. :( Use examples from my coding pls and tell me where they must be placed cause I think I understand better. My main class is pasted in the thread before this one. Thanks a lot for your help and patience.

import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
     // create an empty list to hold Cars
     static ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     
     public ManipulateGymUser (ArrayList<GymUser> arr)
     {
         UserListIn = arr;
     }

     //static int NextID;

    
      // method for adding a new member to the list

     public static void addMember(ArrayList<GymUser> UserListIn)
    {

         String tempUserrID;
        String tempID = null;
        String tempName = null;
        String tempSurname = null;
        int tempAge='0';
        String tempAddress=null;
        int tempPhone = '0';
        String tempDuration = null;

//Read from the keyboard and add the values to the above varaibles

    Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");

        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();

        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();


        System.out.print("Enter Name: ");
        tempName = keyboard.next();

        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();

        System.out.print("Enter Address:");
        tempAddress = keyboard.next();

        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();

        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();

        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();

        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration);
          
        
     // primary key.   
        
     boolean  b = alreadyExists(newGymUser );
        
        if (b== true) {
                    System.out.println("Cannot insert existing user");
                } 
                else 
                {
                        UserListIn.add(newGymUser );
                }
    

}

        public static boolean alreadyExists(GymUser gUser)
  {
        for (int i=0;i<UserListIn.size();i++)
    {
        GymUser temp = (GymUser)UserListIn.get(i);
        if (temp.getID().equals(gUser.getID()))
        {
        return true;
        }
     }
    return false;

  }
0

javaAddict already pointed out (and I concurred in an edit to reiterate it) that you need to remove the "UserListIn" parameter from addMember(). You have two arraylist variables of the exact same name, one of which is local to addMember() and the other at the class level.

0

To tell you the truth I tried that before I have sent the thread but then I am having an error in the main class near the case 1 :ManipulateGymUser.addMember(arr) error given (addMember() in ManipulateGymUser cannot be applied to java.util.arrayList <GymUser>).
But if the brackets of addMember are left empty will data be 'saved' in arrayList .?

Or are you telling me to remove UserListIn only from method addMember brackets. I tried this and I am also having an error <identifer> expected.

Is this what you're telling me to do? (code below)

Thanks

Main

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
public class ArrayList_Member
{
     public static void main (String args[])
    {
ArrayList<GymUser> arr = new ArrayList<GymUser>();
        Scanner keyboard = new Scanner(System.in);

 boolean Exit = false;
        do {
 System.out.println("---------------------");
            System.out.println("|  ** MAIN MENU **   |");
            System.out.println("|  1.Gym menu        |");
            System.out.println("|  2.Pool menu       |");
            System.out.println("|  3.Courts          |");
            System.out.println("|  4.Pool and Gym    |");
            System.out.println("|  5.Transactions    |");
            System.out.println("|  6.Exit            |");
            System.out.println("---------------------");

            int optionNumber = scanner.nextInt();

            switch ( optionNumber )
            {
                case 1:
                        ArrayList_Member.ShowGymMenu(arr); 
                        break;

               case 2:
                         ArrayList_Member.ShowPoolMenu(arrr);
                         break;
                         
               case 3:
                         ArrayList_Member.ShowCourtMenu(a);
                         break;
                         
              case 4:
                         ArrayList_Member.ShowBothMenu(arr);
                         break;
              case 5:
                         ArrayList_Member.ShowTransaction(Tran);
                         break;
               case 6:
                       Exit = true;
                       break;
            }

        }
        while (Exit == false);

        System.out.println("Goodbye");
}


static void ShowGymMenu(ArrayList<GymUser> arr )
 {
      
    boolean Back = false;
  //  do
   // {
                        System.out.println("       Gym Menu         ");
                        System.out.println(" -----------------------");
                        System.out.println("|  1.Add Member         |");
                        System.out.println("|  2.Remove Member      |");
                        System.out.println("|  3.Edit Member        |");
                        System.out.println("|  4.List All           |");
                        System.out.println("|  5.Save               |");
                        System.out.println("|  6.Gym Equipment list |");
                        System.out.println("|  7.Gym Rates          |");
                        System.out.println("|  8.Back               |");
                        System.out.println(" -----------------------");

                        System.out.println("Make your choice ");
                        System.out.println(" ");

                        Scanner scanner = new Scanner(System.in); 
                        int optionNumber = scanner.nextInt();



                    switch ( optionNumber )
                    {

                     case 1: ManipulateGymUser.addMember(arr);    //error here
                            break;
                     case 2: ManipulateGymUser.removeMember(arr);
                             break;
                    case 3: ManipulateGymUser.editMember(arr);
                            break;
                   case 4:  ManipulateGymUser.listAll(arr);
                            break;
                   case 5:  ManipulateGymUser.writeList(arr);
                            break;
                  case 6 :  Other.GymEqiupment();  // write to the file
                            break;
              //    case 7 : Other.GymRates();  // write to the file
                //           break;
                case 8:  Back = true;
                  default  : System.out.print ("\nPlease choose a number from 1 - 9 only\n ");
                        }
  //}
 // while(Back == false);
 }
}
import java.io.*;         // required for handling the IOExceptions
import java.util.*;

class ManipulateGymUser
{
     // create an empty list to hold Cars
     static ArrayList<GymUser> UserListIn = new ArrayList<GymUser>();
     
     public ManipulateGymUser (ArrayList<GymUser> arr)
     {
         UserListIn = arr;
     }

     public static void addMember()      //like this? or addMember(ArrayList<GymUser>) ?
    {

         String tempUserrID;
        String tempID = null;
        String tempName = null;
        String tempSurname = null;
        int tempAge='0';
        String tempAddress=null;
        int tempPhone = '0';
        String tempDuration = null;

//Read from the keyboard and add the values to the above varaibles

    Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("Please enter the member's Personal Details");
        System.out.println("-------------------- ");

        System.out.println("Enter the mebership number");
        tempUserrID = keyboard.next();

        System.out.print("Enter the ID no.: ");
        tempID = keyboard.next();


        System.out.print("Enter Name: ");
        tempName = keyboard.next();

        System.out.print("Enter Surname:");
        tempSurname = keyboard.next();

        System.out.print("Enter Address:");
        tempAddress = keyboard.next();

        System.out.print("Enter Age:");
        tempAge = keyboard.nextInt();

        System.out.print("Enter Phone:");
        tempPhone = keyboard.nextInt();

        System.out.print("MemberShip Duration:");
        tempDuration = keyboard.next();

        GymUser newGymUser = new GymUser(tempID, tempName, tempID, tempSurname, tempAddress, tempPhone, tempAge,tempDuration);
          
        
     // primary key.   
        
     boolean  b = alreadyExists(newGymUser );
        
        if (b== true) {
                    System.out.println("Cannot insert existing user");
                } 
                else 
                {
                        UserListIn.add(newGymUser );
                }
    

}

        public static boolean alreadyExists(GymUser gUser)
  {
        for (int i=0;i<UserListIn.size();i++)
    {
        GymUser temp = (GymUser)UserListIn.get(i);
        if (temp.getID().equals(gUser.getID()))
        {
        return true;
        }
     }
    return false;

  }
0

Of course ManipulateGymUser.addMember(arr) is giving you an error. You changed the method signature. If addMember() no longer has any parameters then you have to change those method calls to not use a parameter.

You need to review the basics of methods because they are still eluding you.

0

But then by doing that no data is being saved in the arrayList!!! So what can I do? That's why i did not remove the param. Now no data is being saved!. What am I going to do now?
Thanks.

0

You already have an ArrayList in ManipulateGymUser and that is the one that alreadyExists() is checking against. Basically you have a big organization problem right now in that you need to decide which class should contain the list and stick with it. If you want the operations in ManipulateGymUser to operate on a list that you pass to it, that's fine - get rid of the array list in that class and work with the one that you pass to the method. If ManipulateGymUser is suppose to maintain the list, get rid of the list in your main driver class and drop the method parameters that take a reference to an arraylist.

You're working with multiple lists right now and until you get that resolved you are going to have issues.

0

I am being confused what arrayList am I going to get rid of and how am I going to do the things you told me in your reply. Can you show me with my coding pls (by showing me how you told me to select an arraylist and work with it by getting rid of one of my arraylist etc) cause I am getting really confused and I need to get it ready today. But if I do what your telling me, does it affect the other methods I have in ManipulateGymUser ? (I am showing you some of the other methods.) Can you help me a bit in the coding?

Thanks a lot

e.g method removeMember that is under addMember()

static void removeMember(ArrayList<GymUser> UserListIn)
    {
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.print("Enter the ID no of the member to be removed: ");
        String ID = keyboard.next();
        int location = -1;
        if (UserListIn.size() == 0){
            System.out.println("You have no members to remove!");
        }
        else {
            for (int i=0; i<UserListIn.size(); i++)
            {
                GymUser gu= UserListIn.get(i);

                //UserListIn.get(i);
                if (gu.getID().equals(ID)) 

                {
                   location = i;
                   break;
                }
              // location = UserListIn.indexOf(new GymUser(ID,null, null,null,null,null,null,null));
            }
            if (location == -1)
            {
                System.out.println("The ID No. you have entered does not exist in this database!");
            }
            else UserListIn.remove(location);
        }
    }
0

Why are reading these posts if you are not following our suggestions? Didn't I tell you to do this in you main:

ManipulateGymUser mgu = ManipulateGymUser();
...
...
mgu.addMember();

You have declared a Constructor, but you are not using. You are passing a parameter to it, which is used in alreadyExists() (The UserList that it is used in the alreadyExists is the one of the constructor, but you keep using the static methods) .
And inside the alredyExists you have one '=', it needs '=='

0

Put in ONE class (ManipulateGymUser) everything non static. One empty Constructor. An ArrayList instance where you have now.:

class ManipulateGymUser {
 ArrayList<GU> userList = new ArrayList<GU>();

 public ManipulateGymUser() {
 }
}

Add in this class every method you need that manipulates this list. They don't require an ArrayList as arguments. They already have one declared in the class.:

public void addMember() {...} accesses the declared ArrayList
public boolean alreadyExists(GymUser user) {...} accesses the declared ArrayList

And in the main create ONE instance of ManipulateGymUser

ManipulateGymUser mgu = ManipulateGymUser();
...
//printMenu or whatever
...
//when you select the option for adding:
mgu.addMember();

Just because you have already everything in other classes static doesn't mean that it is better to continue writing bad code declaring other method static too.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.