Java Program of Member Manipulation

Question:

Like the book details the district library wants to store the details about their members.So that it is easy to provide login credentials for those members to access books online. The application should be a menu driven. Develop an application to implement the scenario. 

You are provided with  a class Member with the following private attributes:

  • int memberId
  • String memberName
  • String address

Necessary public getter and setter methods are provided.

A  no argument constructor and a 3 arguments constructor ( memberId, memberName and address) are also provided.

Create a class Library which has an  ArrayList of member as attribute as : 

List<Member> memberList = new ArrayList<Member>().  

Write the getter and setter.

 Also Library class should have the following methods :

  •  public void addMember(Member memberObj)  –  This method should add the memberObj to the memberList.
  •  public List<Member> viewAllMembers()  –  This method should return the memberList.
  •  public List<Member> viewMembersByAddress(String address)  –  This method should return the list of members who belong to that address.

Create a class Main which has the main method.  In the main function write the code to create the menu and invoke the needed functions based on the choice provided.

Design the UI as shown in the samples below.

Sample Input and Output 1:

1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
1
Enter the id:
123
Enter the name:
sudha
Enter the address:
coimbatore
1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
1
Enter the id:
124
Enter the name:
kavin
Enter the address:
chennai
1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
1
Enter the id:
124
Enter the name:
vivek
Enter the address:
coimbatore
1.Add
2.Display
3.Search by address
4.Exit
Enter your choice:
2
Id:123
Name:sudha
Address:coimbatore
Id:124
Name:kavin
Address:chennai
Id:124
Name:vivek
Address:coimbatore
1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
3
Enter the address:
coimbatore
Id:123
Name:sudha
Address:coimbatore
Id:124
Name:vivek
Address:coimbatore
1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
4

Sample Input and Output 2:

1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
1
Enter the id:
123
Enter the name:
sudha
Enter the address:
coimbatore
1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
3
Enter the address:
chennai
None of the member is from chennai
1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
4

Sample Input and Output 3:

1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
2
The list is empty
1.Add Member
2.View All Members
3.Search Member by address
4.Exit
Enter your choice:
4

Code:

Main.java

import java.util.*;
public class Main 
{
    public static void main(String args[])
    {
        Library o=new Library();
        Scanner sc=new Scanner(System.in);
        int i=0;
        while(i==0)
        {
            System.out.println("1.Add Member\n2.View All Members\n3.Search Member by address\n4.Exit");
            System.out.println("Enter your choice");
            int n=Integer.parseInt(sc.nextLine());
            if(n==1)
            {
                Member obj=new Member();
                System.out.println("Enter the id:");
                obj.setMemberId(Integer.parseInt(sc.nextLine()));
                System.out.println("Enter the name:");
                obj.setMemberName(sc.nextLine());
                System.out.println("Enter the address:");
                o.addMember(obj);
            }
            if(n==2)
            {
                List<Member>obj1=o.viewAllMembers();
                for(Member o1:obj1)
                {
                    System.out.println("Id:"+o1.getMemberId());
                    System.out.println("Name:"+o1.getMemberName());
                    System.out.println("Address:"+o1.getAddress());
                }
            }
            if(n==3)
            {
                System.out.println("Emter the address:");
                List<Member>
                obj=o.viewMembersByAddress(sc.nextLine());
                for(Member obj1:obj)
                {
                    System.out.println("Id:"+obj1.getMemberId());
                    System.out.println("Name:"+obj1.getMemberName());
                    System.out.println("Address:"+obj1.getAddress());
                }
            }
            if(n==4)
            {
                System.exit(0);
            }
        }
    }

}

Library.java

import java.util.*;
public class Library
{
    private List<Member> memberList=new ArrayList<Member>();
    public void setMemberList(List<Member> l)
    {
        memberList=l;
    }
    public List<Member> getMemberList()
    {
        return memberList;
    }
    public void addMember(Member memberObj)
    {
        memberList.add(memberObj);
    }
    public List<Member> viewAllMembers()
    {
        return memberList;
    }
    public List<Member>viewMembersByAddress(String address)
    {
        List<Member> l=new ArrayList<Member>();
        for(Member obj:memberList)
        {
            if(obj.getAddress().equalsIgnoreCase(address))
            {
                l.add(obj);
            }
        }
        return l;
    }
}

Member.java

public class Member
{
    private int memberId;
    private String memberName;
    private String address;
    
    public Member()
    {
        
    }
    public Member(int id,String name,String add)
    {
        memberId=id;
        memberName=name;
        address=add;
    }
    public void setMemberId(int id)
    {
        memberId=id;
    }
     public void setMemberName(String name)
    {
        memberName=name;
    }
     public void setAddress(String add)
    {
        address=add;
    }
    public int getMemberId()
    {
        return memberId;
    }
    public String getMemberName()
    {
        return memberName;
    }
    public String getAddress()
    {
        return address;
    }
    
}

Recommended:

  1. Feedback Manipulation
  2. Retrieve Student Info
  3. Member Manipulation
  4. PhoneBook Manipulation
  5. Count of Each Words

Leave a Comment

Your email address will not be published. Required fields are marked *

x