[英]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. 各位,到目前为止,这是我的会员资格类,我正在努力创建一种方法,该方法可以找到仅使用uniqueId查找器给出的完整成员详细信息。 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: 问题:
System.out.println
statements from this class and instead leave it in a UI class or main method (if very simple). System.out.println
语句,而是将其保留在UI类或main方法中(如果非常简单)。 System.out.println
statements System.out.println
语句 listOfMembers
field, or why it's just a String. listOfMembers
字段,或者为什么它只是一个字符串。 You look to be trying to combine Member and Membership together in one single class -- Don't do this. Member
since it holds information for just a single Member. Member
因为它仅包含单个Member的信息。 ArrayList<Member>
ArrayList<Member>
public Member getMember(int id)
method that would return the item in the list above that shares the id passed into the method. public Member getMember(int id)
方法,该方法将返回上面列表中共享传递给该方法的ID的项目。 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. 然后,您便拥有了这样的
Member
本身,可以根据需要添加更多字段。
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. 这是要使用的OOP中非常基本但很强大的功能。
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. 一件小事是我反对使用
ArrayList<E>
存储Member
。 If you add to the implementation that you can remove users the uniqueId
will shift from user to user. 如果添加到可以删除用户的实现中,则
uniqueId
将在用户之间转移。 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
. 如果您想保持简单并开始使用,则
ArrayList<E>
可以正常工作,请知道您可能会遇到的功能问题, uniqueId
不一定与Member
绑定。
"I am quite new to java and have never come across "map" can you please explain what it is?" “我对Java很陌生,从未接触过“地图”,您能解释一下它是什么吗?”
"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
: 而不是像在
Array
那样使用直接索引:
Or like a List
: 或像
List
:
And for a Map
: 对于
Map
:
Map
. Map
可能没有3或4处。 You can store any Object
s as anything. Object
存储为任何Object
。 A String
is another example of a common key
. String
是公用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. 我建议使用Map并将id用作Map的键,并将Membership的对象存储为Value,从而也易于检索和存储。
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, 要通过ID获得会员,
System.out.println(map.get(id).getMemberDetails());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.