简体   繁体   English

我已经编写了该成员资格类,如何创建找到给成员的特定ID的方法?

[英]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: 问题:

  • 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). 我将从此类中删除所有System.out.println语句,而是将其保留在UI类或main方法中(如果非常简单)。
  • In particular, getter methods should return field values, and should not have System.out.println statements 特别是,getter方法应返回字段值,并且不应具有System.out.println语句
  • I'm not sure why this class has a 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. 您可能想将“会员”和“会员资格”组合到一个类中-请勿这样做。
  • I'd name this class Member since it holds information for just a single Member. 我将其命名为Class Member因为它仅包含单个Member的信息。
  • If I needed a Membership class, it would instead hold an ArrayList<Member> 如果我需要一个Membership类,它将保存一个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. 并且它将具有一个public Member getMember(int id)方法,该方法将返回上面列表中共享传递给该方法的ID的项目。 A simple for loop that iterated through the list, comparing id's would suffice. 一个简单的for循环遍历列表,比较id即可。

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那样使用直接索引:

  • arr[5]; ARR [5]; // here you get the value at index position 5. //在此处获得索引位置5的值。

Or like a List : 或像List

  • list.get(5); 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. //在这里,您将获得第五个元素,它可以(几乎)存储在内存中的任何位置,即4之前或之后,因为4知道5在哪里。

And for a Map : 对于Map

  • map.get(5); map.get(5); // you get the object stored at 5, there might not be a 3 or 4 in the Map . //您将对象存储在5处,而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.

相关问题 给定类成员名称或对象,如何在Java中找到其声明/封闭类对象? - Given a class member name or object, how can I find its declaring/enclosing class object in java? 鉴于我有构造函数,如何从类中调用方法? - How to call a method from a class given that I have the Constructor for it? 我如何单元测试java中类的特定方法在给定的持续时间内被定期调用 - How can I Unit test that a specific method of a class in java was periodically invoked for a given duration 如果我已将其声明为Java中类的成员变量,是否可以访问另一个方法中一个方法的变量值? - Can i access the value of a variable of one method in another method if i have declared it as a member variable of the class in java? 如何找到类路径上具有特定方法注释的所有类? - How can I find all classes on the classpath that have a specific method annotation? 如何使用ArrayList中的成员创建实例<Class> - How can I create an instance using a member in ArrayList<Class> 我可以创建特定方法的toString()吗? 不是全班 - Can I create toString() of a specific method? Not the whole class 如何在 Java 中找到调用给定方法的所有方法? - How can I find all the methods that call a given method in Java? 如何获得给定方法的实现类? - How can I get the implementing class for a given method? 如何根据特定变量(ID)使此类工作? - How can I make this class work based on a on specific variable (ID)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM