简体   繁体   中英

i Have written this membership class, how can i create a method to find a specific ID given to a member?

Guys this is my membership class so far, i am struggling to create a method that finds the full members details that i have given just using a uniqueId finder. Please help.

public class Membership {

    private String FirstName;
    private String LastName;
    private int memberId;
    private String listOfMembers;
    private int uniqueId;
    private long phoneNumber;



    public Membership(String FirstName, String LastName, int uniqueId,
    long phoneNumber)
    {


       this.uniqueId = uniqueId;
       this.FirstName = FirstName;
       this.LastName = LastName;
       this.phoneNumber = phoneNumber;
    }


    public String getMember()
    {
        return FirstName + LastName;
    }
    public String getlistOfMembers()
    {
        return (FirstName + LastName);
    }
    public int getId()
    {
      return uniqueId;
    }

    public void MemberId (int Id)
    {
        System.out.println("Id" + Id);
    }

    public String getMemberDetails ()
    {
        System.out.println("Member Id: " + uniqueId);
        System.out.println("first name:  " + FirstName);
        System.out.println("LastName:  " + LastName);
        System.out.println("Member phone number:  " + phoneNumber);
        return listOfMembers;
    }


}

This is what i have done so far.

Issues:

  • You've got user interface code where it doesn't belong. I would remove all System.out.println statements from this class and instead leave it in a UI class or main method (if very simple).
  • In particular, getter methods should return field values, and should not have System.out.println statements
  • I'm not sure why this class has a listOfMembers field, or why it's just a String. You look to be trying to combine Member and Membership together in one single class -- Don't do this.
  • I'd name this class Member since it holds information for just a single Member.
  • If I needed a Membership class, it would instead hold an ArrayList<Member>
  • And it would have a public Member getMember(int id) method that would return the item in the list above that shares the id passed into the method. A simple for loop that iterated through the list, comparing id's would suffice.

To add on Hovercraft's answer with an example.

You have your class handling all the members, very basic implementation of it.

public class Membership {
    private final Map<Integer, Member> members = new HashMap<>();

    public void addMember (Integer uniqueId, Member member) {
        members.put (uniqueId, member);
    }

    public void getMember (Integer uniqueId) {
        return members.get (uniqueId);
    }

    ...
}

Then you have the Member s themselves like this, more fields can be added as you want them.

public class Member {
    private String firstName;
    private String lastName;

    public Member (String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName () {
        return firstName;
    }

    ...
}

This is a very basic, but strong, feature in OOP to use.

Again see Hovercraft's answer as it provides all the details. If they were to edit/remove I will update this one.

Map vs List

One minor thing is I'd vote against using an ArrayList<E> to store the Member s. If you add to the implementation that you can remove users the uniqueId will shift from user to user. Instead I would be for making sure that you are not adding to an existing user.

If you want to keep it simple and just get going, an ArrayList<E> works, do know the problem you might get in the feature, an uniqueId is not necessarily tied to a Member .

"I am quite new to java and have never come across "map" can you please explain what it is?"

"An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value." - From: Documentation .

Instead of working with direct indexes as you do in an Array :

  • arr[5]; // here you get the value at index position 5.

Or like a List :

  • list.get(5); // here you get the fifth element, it can be stored (almost) anywhere in the memory, before or after 4, doesn't matter, as 4 knows where 5 is.

And for a Map :

  • map.get(5); // you get the object stored at 5, there might not be a 3 or 4 in the Map . You can store any Object s as anything. A String is another example of a common key .

I would suggest to use Map and use id as key of Map and store object of Membership as Value,thereby easy to retrieve and store also.

Something similar to this,

 Map<Integer,Membership> map = new HashMap<Integer,Membership>();
    Membership m = new Membership("First", "LastName", 1,1234567890);
    map.put(m.getId(), m);

To get member by id,

 System.out.println(map.get(id).getMemberDetails());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM