CodeBlanks Logo Icon CodeBlanks

Member management

Register members with Member.

Member management

The third lesson defines the Member class. Each member has a name and an ID number. The Member class represents people who register at the library.

Example: class Member { String name; int id; Member(String n, int i) { name = n; id = i; } }. This class is also added alongside Library and Book. A toString method shows the member readably: name + " (" + id + ")". The member ID should be unique; the library assigns it during registration. Later, loan operations will use these members.

Three classes together

You now have three classes: Library, Book and Member. Each is defined with its own fields and constructor. By putting the main method in the most recently added class, you can test the instance.

Example: In Member's main method, a member is created with new Member("Alex", 101) and printed. The other classes stay unchanged. This structure shows the project growing step by step; each lesson adds a class and preserves the previous code. The next lesson will define the loan flow that links these classes.

Practice Add the Member class

Add a Member class to the file. Write String name and int id fields. Add a Member(String n, int i) constructor; assign both fields. Add a public String toString() method returning name + " (" + id + ")". Put main inside Member; create a Member instance and print it.

Starter code

Main.java
class Library {
    String name;
    Library(String n) {
        name = n;
    }
    public String toString() {
        return "Library: " + name;
    }
}
class Book {
    String title;
    String author;
    Book(String t, String a) {
        title = t;
        author = a;
    }
    public String toString() {
        return title + " - " + author;
    }
}

// Add the Member class
// name and id fields, constructor and toString
// Instantiate and print in main

Sample solution

Main.java
class Library {
    String name;
    Library(String n) {
        name = n;
    }
    public String toString() {
        return "Library: " + name;
    }
}
class Book {
    String title;
    String author;
    Book(String t, String a) {
        title = t;
        author = a;
    }
    public String toString() {
        return title + " - " + author;
    }
}
class Member {
    String name;
    int id;
    Member(String n, int i) {
        name = n;
        id = i;
    }
    public String toString() {
        return name + " (" + id + ")";
    }
    public static void main(String[] args) {
        Member m = new Member("Alex", 101);
        System.out.println(m);
    }
}

Expected output

Alex (101)

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What type should the id field of Member be?

  • String
  • boolean
  • double
  • int Correct answer

Explanation: The id is an integer; int is suitable.

2 Complete the Member constructor

Question
class Member {
    String name;
    int id;
    ____(String n, int i) {
        name = n;
        id = i;
    }
}
Answer
class Member {
    String name;
    int id;
    Member(String n, int i) {
        name = n;
        id = i;
    }
}

Explanation: The constructor name must match the class name.

3 Match the Member parts

Term Meaning
String name Name field
int id ID field
Member(String n, int i) Constructor
name + id toString body

All lessons in Intermediate