首页 > 解决方案 > 如何输出完整的详细信息

问题描述

我希望在 Java 中创建一个利用 OOP 的休闲中心预订系统。

其中 2 个类收集名称和地址以及成员类型,它们被添加到名为 memberRegister 的 ArrayList 中。如何打印所有成员详细信息(即存储在数组列表中的内容),从而在一个命令中输出名称、地址、成员类型等?

我的相关类的源代码如下......

public class Name {
    
    private String firstName;
    private String middleName;
    private String lastName;
    
    //constructor to create object with a first and last name
    public Name(String fName, String lName) {
        
        firstName = fName;
        middleName = "";
        lastName = lName;
    }
    
    //constructor to create object with first, middle and last name
    //if there isn't a middle name, that parameter could be an empty String
    public Name(String fName, String mName, String lName) {
        firstName = fName;
        middleName = mName;
        lastName = lName;
    }
    
    // constructor to create name from full name
    // in the format first name then space then last name
    // or first name then space then middle name then space then last name
    public Name (String fullName) {
        int spacePos1 = fullName.indexOf(' ');
        firstName = fullName.substring(0, spacePos1);
        int spacePos2 = fullName.lastIndexOf(' ');
        if (spacePos1 == spacePos2)
            middleName = "";
        else
            middleName = fullName.substring(spacePos1+1, spacePos2);
        lastName = fullName.substring(spacePos2 + 1);
    }
    
    // returns the first name
    public String getFirstName() {return firstName; }
    // returns the last name
    public String getLastName() {return lastName; }
    
    //change the last name to the value provided in the parameter
    public void setLastName(String ln) {
        lastName = ln;
    }
    
    //returns the first name then a space then the last name
    public String getFirstAndLastName() {
        return firstName + " " + lastName;
    }
    
    // returns the last name followed by a comma and a space
    // then the first name
    public String getLastCommaFirst() {
        return lastName + ", "+ firstName;
    }
    
    public String getFullname() {
        return firstName + " " + middleName + " " + lastName;       
        
    }
    
}

public class Address {

    private String first_line, town, postcode;
    
    public Address(String first_line, String town, String pcode)
    {
        this.first_line = first_line;
        this.town = town;
        postcode = pcode;
    }
    
    public Address()
    {
        first_line = "";
        town = "";
        postcode = "";
    }
    
    public String getFirst_line() {
        return first_line;
    }
    
    public void setFirst_line(String first_line) {
        this.first_line = first_line;
    }
    
    public String getTown() {
        return town;
    }
    
    public void setTown() {
        this.town = town;
    }
    
    public String getPostcode() {
        return postcode;
    }
    
    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }
}

public class Member extends Person {
    
    private String id; // membership ID number
    private String type; // full, family, exercise, swim, casual
    
    public Member(String id, String type, Name n, Address a)
    {
        super(n, a);
        this.id = id;
        this.type = type;
    }
    
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
        
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
}


import java.util.ArrayList;

public class Registration {
    
    private ArrayList<Member> memberRegister;
    
    public Registration()
    {
        memberRegister = new ArrayList();
    }
    
    public void register(Member m)
    {
        memberRegister.add(m);
    }
    
    public int countMembers()
    {
        return memberRegister.size();
    }
    
    public Member getMember(int i) {
        return memberRegister.get(i);
    }

public class Main {

    public static void main(String[] args) {
        
        Name n = new Name("Kieran", "David", "Nock");
        Address a = new Address ("123 Skywalker Way", "London", "NW1 1AA");
        
        Member m = new Member("001", "Full", n, a);
        Registration reg = new Registration();
        reg.register(m);
        
        System.out.println(reg.countMembers());
        System.out.println(reg.getMember(0).getName().getFullname());
 
    }

}

标签: java

解决方案


嘿,我会按照以下方式进行

首先覆盖toString()所有模型类的方法,并记住toString()按以下方式覆盖成员类

@Override
    public String toString() {
        return "Member{" +
                "id='" + id + '\'' +
                ", type='" + type + '\'' +
                '}'+super.toString();
    }

在此之后在 main 方法中添加以下单行将起作用

reg.getMemberRegister().stream().forEach(System.out::println);

注意:为ClassmemberRegister中存在的列表创建一个 getterRegistration


推荐阅读