I need to get a field from different classes to appear in the class. I am using now in bluej

Viewed 150

Id and Name are what i am trying to get from my other class but i am not sure how to get it to appear in this class

public class Store
{
    
    public String storeName;
    public int total;
    private Store member;
    
    public Store(String newStoreName, int newTotal)
    {
        storeName = newStoreName;
        total = newTotal;
    }
    
    public String getStoreName()
    {
        return storeName;
    }

    public int getTotal()
    {
        return total;
    }

    public String getMember()
    {
        return Id;
        return name;
    }
    
    public void memberRegister1()
    {
        System.out.println("Salford Thrifty Store welcomes");
    }
}
1 Answers

So it seems you are a bit lost, if you are trying to get the Id and name of the member of your store, your method:

public String getMember()
{
    return Id;
    return name;
}

should just be:

public Member getMember()
{
    return member;
}

Note that in your attribute declaration, member is of class Store, i assume this is wrong and your member should be of class Member, which could look something like this:

public class Member{

public int Id;
public String name;

public Member(int Id, String name){
this.Id = Id;
this.name = name
}
}

Once this is done you can access the member you returned in getMember() like this:

Member myMember = myStore.getMember();
System.out.println("The member with id: " + member.Id + " has the name: " + member.name);

If all this seems too advanced, I recommend that you take a calm look at Java, Classes, Methods, public and private fields and more introductory stuff...

If you need any help let me know, Good luck!

Related