In: Computer Science
Implementation Requirements:
1. Inheritance
2. Polymorphism
3. Linked List with appropriate payload
4. UML Diagrams (pre-coding design)
Project Problem Domain:
Write a program that tracks the faculty's information, such as id, names, rank, and number of allowed courses if the faculty is an adjunct. The hierarchy will be based on an abstract class Faculty and two derived classes, Professor and Adjunct. For this project, you will store all instances of faculty in a linked list! Have your program allow the user to enter records, display all records, find a record, and delete a record. Those options should be displayed in a menu at run time.
Here is the code,
Faculty.java
public abstract class Faculty{
private int id;
private String name;
private String rank;
public Faculty(int id, String name, String rank){
this.id = id;
this.name = name;
this.rank = rank;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
@Override
public String toString() {
return "id=" + id + ", name='" + name + "', rank='" + rank+"'";
}
}
Professor.java
public class Professor extends Faculty{
public Professor(int id, String name, String rank){
super(id, name, rank);
}
@Override
public String toString() {
return "Adjunct{" + super.toString() + '}';
}
}
Adjunct.java
public class Adjunct extends Faculty{
private int numberOfAllowedCourses;
public Adjunct(int id, String name, String rank, int numberOfAllowedCourses){
super(id, name, rank);
this.numberOfAllowedCourses = numberOfAllowedCourses;
}
public int getNumberOfAllowedCourses() {
return numberOfAllowedCourses;
}
public void setNumberOfAllowedCourses(int numberOfAllowedCourses) {
this.numberOfAllowedCourses = numberOfAllowedCourses;
}
@Override
public String toString() {
return "Adjunct{" + super.toString() +
", numberOfAllowedCourses=" + numberOfAllowedCourses + '}';
}
}
Runner.java
import java.util.*;
public class Runner {
private static final Scanner sc = new Scanner(System.in);
private static LinkedList<Faculty> list;
public static void main(String[] args) {
/*
For this project, you will store all instances of faculty in a linked list!
Have your program allow the user to enter records, display all records, find a record, and delete a record.
Those options should be displayed in a menu at run time.
*/
list = new LinkedList<>();
System.out.println("1. Enter Record\n2. Display all records\n3. Find a record.\n4. Delete a record.\n5. Exit");
while (true){
System.out.println("Chose one of the options :\n");
int choice = sc.nextInt();
switch (choice){
case 1 : enterRecord();
break;
case 2 : displayRecord();
break;
case 3 : findRecord();
break;
case 4 : deleteRecord();
break;
case 5 : return;
default:
System.out.println("Please enter correct choice.");
}
}
}
private static void enterRecord(){
System.out.println("Enter id : ");
int id = sc.nextInt();
System.out.println("Enter name : ");
String name = sc.next();
System.out.println("Enter rank : ");
String rank = sc.next();
System.out.println("Is Adjunct? (y/n): ");
boolean isAdjunct = sc.next().charAt(0) == 'y';
Faculty faculty;
if(isAdjunct){
System.out.println("Enter number of allowed courses : ");
int numberOfAllowedCourses = sc.nextInt();
faculty = new Adjunct(id, name, rank, numberOfAllowedCourses);
}
else faculty = new Professor(id, name, rank);
list.add(faculty);
System.out.println("Successfully entered record.");
}
private static void displayRecord(){
if(list.isEmpty()){
System.out.println("No data records available.");
return;
}
StringBuilder records = new StringBuilder();
for(Faculty faculty : list){
if(faculty instanceof Adjunct) {
Adjunct adjunct = (Adjunct) faculty;
records.append(adjunct.toString()).append("\n");
}
else{
Professor professor = (Professor) faculty;
records.append(professor.toString()).append("\n");
}
}
System.out.println("Records in the list :\n"+records.toString());
}
private static void findRecord(){
System.out.println("Enter id to search for a record : ");
int id = sc.nextInt();
for(Faculty faculty : list){
if(faculty instanceof Adjunct) {
Adjunct adjunct = (Adjunct) faculty;
if(adjunct.getId() == id){
System.out.println("Record found : "+adjunct.toString());
}
}
else{
Professor professor = (Professor) faculty;
if(professor.getId() == id){
System.out.println("Record found : "+professor.toString());
}
}
}
}
private static void deleteRecord(){
System.out.println("Enter id to delete a record : ");
int id = sc.nextInt();
for(Faculty faculty : list){
if(faculty.getId() == id){
list.remove(faculty);
System.out.println("Record deleted.");
return;
}
}
}
}
OUTPUT :
If you have any doubt ask in the comments. Also don't forget to upvote the solution.