In: Computer Science
Write a program to display privilege list of the roles and role list for privileges using many to many concept.
Problem Specification:
1. Prefill the data for roles and privileges.
2. Create a menu 1. Assign privilege 2. Cancel privilege 3. Search privilege by role 4. Search role by privilege.
3. Assign privilege -- To assign specific privilege for role.
4. Cancel Privilege -- To cancel specific privilege for role.
5. Search privilege by role -- To display all the privileges assigned for specific role.
6. Search role by privilege -- To display all the roles who are having specified privilege.
[Note :Strictly adhere to the object oriented specifications given as a part of the problem statement.Use the same class names, method names and attribute names.]
Create a class Role with the following private member variables/attributes.
1. Include a default constructor and 3-argument constructor with following argument order id, name , privilegeList
2. Include appropriate getters and setters.
Create a class Privilege with the following private member variables/attributes.
1. Include a default constructor and 3-argument constructor with following argument order id, name, roleList.
2. Include appropriate getters and setters.
Input and Output Format:
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.
Sample Input and output :
Available roles:
Id Name
1 Manager
2 Shipper
3 Agent
4 Customer
Available privileges:
Id Name
1 Process Shipment
2 Create Shipment
3 Schedule Shipment
4 Cancel Shipment
1. Assign privilege
2. Cancel privilege
3. Search privilege by role
4. Search role by privilege
Enter the choice:
1
Enter the id for role:
1
Enter the id for privilege:
1
Do you want to process again?(yes/no)
yes
1. Assign privilege
2. Cancel privilege
3. Search privilege by role
4. Search role by privilege
Enter the choice:
1
Enter the id for role:
2
Enter the id for privilege:
1
Do you want to process again?(yes/no)
yes
1. Assign privilege
2. Cancel privilege
3. Search privilege by role
4. Search role by privilege
Enter the choice:
1
Enter the id for role:
3
Enter the id for privilege:
1
Do you want to process again?(yes/no)
yes
1. Assign privilege
2. Cancel privilege
3. Search privilege by role
4. Search role by privilege
Enter the choice:
1
Enter the id for role:
1
Enter the id for privilege:
2
Do you want to process again?(yes/no)
yes
1. Assign privilege
2. Cancel privilege
3. Search privilege by role
4. Search role by privilege
Enter the choice:
3
Enter the role name:
Manager
Process Shipment
Create Shipment
Do you want to process again?(yes/no)
yes
1. Assign privilege
2. Cancel privilege
3. Search privilege by role
4. Search role by privilege
Enter the choice:
4
Enter the privilege name:
Process Shipment
Manager
Shipper
Agent
Do you want to process again?(yes/no)
no
Don'ts:
1. Do not create packages for classes. Strictly adhere to the program structure given in the template code.
2. Do not use Internet Explorer, highly recommended to use chrome browser to launch Tekstac platform.
3. Do not create multiple classes inside a single file. Create separate file for each class.
3 classes--
Role.Java
Privilege.java
Main.Java
// Role.java
import java.util.List;
public class Role
{
private Integer id;
private String name;
private List<Privilege> privilegeList;
// default constructor
public Role()
{
id = 0;
name = "";
privilegeList = null;
}
// paramterized constructor
public Role(Integer id, String name,
List<Privilege> list)
{
this.id = new Integer(id);
this.name = name;
this.privilegeList = list;
}
// setters
public void setID(Integer id)
{
this.id = new Integer(id);
}
public void setName(String name)
{
this.name = name;
}
public void setPrivilegeList(List<Privilege>
list)
{
privilegeList = list;
}
// getters
public Integer getID()
{
return id;
}
public String getName()
{
return name;
}
public List<Privilege> getPrivilegeList()
{
return privilegeList;
}
}
//end of Role.java
// Privilege.java
import java.util.List;
public class Privilege
{
private Integer id;
private String name;
private List<Role> roleList;
// default constructor
public Privilege()
{
id = 0;
name = "";
roleList = null;
}
// paramterized constructor
public Privilege(Integer id, String name,
List<Role> list)
{
this.id = new Integer(id);
this.name = name;
roleList = list;
}
// setters
public void setID(Integer id)
{
this.id = new Integer(id);
}
public void setName(String name)
{
this.name = name;
}
public void setRoleList(List<Role> list)
{
roleList = list;
}
// getters
public Integer getID()
{
return id;
}
public String getName()
{
return name;
}
public List<Role> getRoleList()
{
return roleList;
}
}
//end of Privilege.java
// Main.java
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
// method to display the menu
private static void menu()
{
System.out.println("1. Assign
privilege" + "\n" +
"2. Cancel privilege" + "\n" +
"3. Search privilege by role" + "\n" +
"4. Search role by privilege");
}
// method to return the index of role in roles
list
private static int getRoleIndex(ArrayList<Role>
roles, int id)
{
for(int
i=0;i<roles.size();i++)
{
if(roles.get(i).getID() == id) // role found
return i;
}
return -1; // role not found
}
// method to return the index of privilege in
privileges list
private static int
getPrivilegeIndex(ArrayList<Privilege> privileges, int
id)
{
for(int
i=0;i<privileges.size();i++)
{
if(privileges.get(i).getID() == id) // privilege found
return i;
}
return -1; // privilege not
found
}
// method to return the role index in privilege
private static int roleExists(Privilege privilege, int
roleId)
{
List<Role> roles =
privilege.getRoleList(); // get the list of roles for the
privilege
for(int
i=0;i<roles.size();i++)
{
if(roles.get(i).getID() == roleId) // role found
return i;
}
return -1; // role not found
}
// method to return the privilege index in
role
private static int privilegeExists(Role roles, int
privilegeId)
{
List<Privilege> privileges =
roles.getPrivilegeList(); // get the list of privilege
for(int
i=0;i<privileges.size();i++)
{
if(privileges.get(i).getID() == privilegeId) // privilege
found
return i;
}
return -1; // privilege not
found
}
public static void main(String[] args) {
// pre-define roles and
privileges
ArrayList<Role> roles = new
ArrayList<Role>();
roles.add(new Role(1,"Manager",new
ArrayList<Privilege>()));
roles.add(new Role(2,"Shipper",new
ArrayList<Privilege>()));
roles.add(new Role(3,"Agent",new
ArrayList<Privilege>()));
roles.add(new Role(4,"Customer",new
ArrayList<Privilege>()));
ArrayList<Privilege>
privileges = new ArrayList<Privilege>();
privileges.add(new
Privilege(1,"Process Shipment",new ArrayList<Role>()));
privileges.add(new
Privilege(2,"Create Shipment",new ArrayList<Role>()));
privileges.add(new
Privilege(3,"Schedule Shipment",new
ArrayList<Role>()));
privileges.add(new
Privilege(4,"Cancel Shipment",new ArrayList<Role>()));
Scanner scan = new
Scanner(System.in);
int choice, roleId,
privilegeId;
String contChoice;
// display the available roles and
privileges
System.out.println("Available
roles:");
System.out.printf("%-10s%-20s\n","ID","Name");
for(int
i=0;i<roles.size();i++)
System.out.printf("%-10d%-20s\n",roles.get(i).getID(),roles.get(i).getName());
System.out.println("\nAvailable
privileges:");
System.out.printf("%-10s%-30s\n","ID","Name");
for(int
i=0;i<privileges.size();i++)
System.out.printf("%-10d%-30s\n",privileges.get(i).getID(),privileges.get(i).getName());
// loop that continues till the
user wants
do
{
menu(); //
display the menu
// input of
choice
System.out.print("Enter the choice: ");
choice =
scan.nextInt();
// validate
choice and re-prompt until valid
while(choice
< 1 || choice > 4)
{
System.out.println("Invalid choice. Re-enter:
");
choice = scan.nextInt();
}
if(choice == 1)
// assign privilege
{
// input of role id
System.out.print("Enter the id for role:
");
roleId = scan.nextInt();
// validate role id and re-prompt until valid
i.e exists in pre-defined role list
while(getRoleIndex(roles,roleId) == -1)
{
System.out.print("Invalid
role ID. Re-enter: ");
roleId =
scan.nextInt();
}
Role role =
roles.get(getRoleIndex(roles,roleId)); //get the role object
// input of privilege id
System.out.print("Enter the id for privilege:
");
privilegeId = scan.nextInt();
// validate privilege id and re-prompt until
valid i.e exist in pre-defined privilege list
while(getPrivilegeIndex(privileges,privilegeId)
== -1)
{
System.out.print("Invalid
privilege ID. Re-enter: ");
privilegeId =
scan.nextInt();
}
scan.nextLine();
Privilege privilege =
privileges.get(getPrivilegeIndex(privileges,privilegeId)); // get
the privilege object
// if role doesn't contain the privilege
if(privilegeExists(role, privilegeId) ==
-1)
{
//add privilege to role
List<Privilege>
privilegeList = role.getPrivilegeList();
privilegeList.add(privilege);
role.setPrivilegeList(privilegeList);
// add role to
privilege
List<Role> roleList =
privilege.getRoleList();
roleList.add(role);
privilege.setRoleList(roleList);
}
}
else if(choice
== 2) // cancel privilege
{
// input of role id
System.out.print("Enter the id for role:
");
roleId = scan.nextInt();
while(getRoleIndex(roles,roleId) == -1)
{
System.out.print("Invalid
role ID. Re-enter: ");
roleId =
scan.nextInt();
}
Role role =
roles.get(getRoleIndex(roles,roleId));
// input of privilege id
System.out.print("Enter the id for privilege:
");
privilegeId = scan.nextInt();
while(getPrivilegeIndex(privileges,privilegeId)
== -1)
{
System.out.print("Invalid
privilege ID. Re-enter: ");
privilegeId =
scan.nextInt();
}
scan.nextLine();
Privilege privilege =
privileges.get(getPrivilegeIndex(privileges,privilegeId));
int index = privilegeExists(role,
privilegeId);
// of privilege exists in role
if(index != -1)
{
// remove privilege from
role
List<Privilege>
privilegeList = role.getPrivilegeList();
privilegeList.remove(index);
role.setPrivilegeList(privilegeList);
index = roleExists(privilege,
roleId);
// remove role from
privilege
List<Role> roleList =
privilege.getRoleList();
roleList.remove(index);
privilege.setRoleList(roleList);
}
}
else if(choice
== 3) // display privileges by role
{
System.out.print("Enter the role name: ");
scan.nextLine();
String roleName = scan.nextLine();
for(int i=0;i<roles.size();i++)
{
if(roles.get(i).getName().equalsIgnoreCase(roleName))
{
List<Privilege> privilegeList
=roles.get(i).getPrivilegeList();
for(int
j=0;j<privilegeList.size();j++)
System.out.println(privilegeList.get(j).getName());
break;
}
}
}
else if(choice
== 4) // display roles by privilege
{
System.out.print("Enter the privilege name:
");
scan.nextLine();
String privilegeName = scan.nextLine();
for(int i=0;i<privileges.size();i++)
{
if(privileges.get(i).getName().equalsIgnoreCase(privilegeName))
{
List<Role> roleList =privileges.get(i).getRoleList();
for(int
j=0;j<roleList.size();j++)
System.out.println(roleList.get(j).getName());
break;
}
}
}
System.out.print("Do you want to process again?(yes/no) ");
contChoice =
scan.nextLine();
}while(contChoice.equalsIgnoreCase("yes"));
}
}
//end of Main.java
Output: