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;
}
}