Solution includes three java files copy source code in three different files(AddressBook.java , Student.java , Assignment1.java). zip these files and submit the zip folder.
File = AddressBook.java
import java.util.*;
import javax.swing.*;
public class AddressBook {
ArrayList mylist;
ArrayList projectlist;
//default constructor
public AddressBook(){
mylist = new ArrayList();
projectlist = new ArrayList();
}
public void addToBook(){
Student std1 = new Student();
std1.addStudent();
mylist.add(std1);
JOptionPane.showMessageDialog(null,"Now size is " + mylist.size());;
}
//search student function
public void searchStudent(){
String search = JOptionPane.showInputDialog("Enter Roll Number for search");
Student std;
int i = 0;
for( i = 0; i < mylist.size(); i++){
std = (Student) mylist.get(i);
if(search.equals(std.rollnum)){
JOptionPane.showMessageDialog(null,"Name: " + std.name + "\nRoll Number: "+ std.rollnum + "\nPrograme: " + std.programe + "\nPhone: " + std.phone + "\nStatus: " + std.status);
} else {
JOptionPane.showMessageDialog(null,"Student record not found for " + search);
}
}
}
//Function to delete a student
public void deletStudent(){
String delete = JOptionPane.showInputDialog("Enter Roll number to delete");
Student std1;
for(int i = 0; i < mylist.size(); i++){
std1 = (Student) mylist.get(i);
if(delete.equals(std1.rollnum)){
mylist.remove(i);
JOptionPane.showMessageDialog(null, "Student deleted successfully for roll number" + delete);
}else if(!delete.equals(std1.rollnum)){
JOptionPane.showMessageDialog(null, "No record found for" + delete);
}
}
}
//add project to projectlist
public void addProject(){
String search0 = JOptionPane.showInputDialog("Enter a roll number to add project");
Student s = new Student();
Student.Projects proj = s.new Projects();
for(int i = 0; i < mylist.size(); i++){
s = (Student) mylist.get(i);
if(search0.equals(s.rollnum)){
proj.addProject();
projectlist.add(proj);
break;
} else {
JOptionPane.showMessageDialog(null, "There found no such roll number to add a project for " + search0);
}
}
}
//find projects
public void findProject()
{
String search1 = JOptionPane.showInputDialog("Entter rollnumber to search project");
Student std2 = new Student();
for(int i = 0; i < mylist.size(); i++)
{
std2 = (Student) mylist.get(i);
if(search1.equals(std2.rollnum)) {
Student stdp = new Student();
Student.Projects prj = stdp.new Projects();
for(int j = 0; j < projectlist.size(); j++){
prj = (Student.Projects) projectlist.get(j);
if(search1.equals(prj.rollnumber)){
JOptionPane.showMessageDialog(null, "Roll Number:" + prj.rollnumber + "\nProject Title: " + prj.title + "\nCurrent Phase:" + prj.cPhase + "\nProject Status:" + prj.pStatus);
break;
} else {
JOptionPane.showMessageDialog(null, "There's found no project for " + search1);
}
}
} else if(!search1.equals(std2.rollnum)){
JOptionPane.showMessageDialog(null, "There's found no record for " + search1);
}
}
}
public void deleteProject()
{
String search1 = JOptionPane.showInputDialog("Entter rollnumber to delete project");
Student std2 = new Student();
for(int i = 0; i < mylist.size(); i++){
std2 = (Student) mylist.get(i);
if(search1.equals(std2.rollnum)) {
Student stdp = new Student();
Student.Projects prj = stdp.new Projects();
for(int j = 0; j < projectlist.size(); j++){
prj = (Student.Projects) projectlist.get(j);
if(search1.equals(prj.rollnumber)){
projectlist.remove(j);
JOptionPane.showMessageDialog(null, "Project has been deleted");
break;
} else {
JOptionPane.showMessageDialog(null, "There's found no project for " + search1);
}
}
} else if(!search1.equals(std2.rollnum)){
JOptionPane.showMessageDialog(null, "There's found no record for " + search1);
}
}
}
}
File = Student.java
import javax.swing.*;
public class Student {
public String name, rollnum, programe, status;
public int phone;
//default Constructor
public Student(){
name = "No name";
rollnum = "BC0000";
phone = 0000000;
status = "no status";
}
//Function for add a student
public void addStudent(){
name = JOptionPane.showInputDialog("Enter Student Name");
rollnum = JOptionPane.showInputDialog("Enter Roll Number");
programe = JOptionPane.showInputDialog("Enter Programe Name");
String ph = JOptionPane.showInputDialog("Enter Phone Number");
phone = Integer.parseInt(ph);
String st = JOptionPane.showInputDialog("Enter status\n0 for blocked\n1 for active");
if(st.equals("0")){
status = "Blocked";
} else if(st.equals("1")){
status = "Active";
}
}
//inner class for projects
public class Projects{
public String rollnumber;
public String title;
public String cPhase;
public String pStatus;
//default constructor
public Projects(){
rollnumber = "no rollnumber";
title = "No title";
cPhase = "No Phase";
pStatus = "No project Status";
}
//Add project to a student
public void addProject(){
rollnumber = JOptionPane.showInputDialog("Enter Student roll number");
title = JOptionPane.showInputDialog("Enter project title");
cPhase = JOptionPane.showInputDialog("Enter current phase");
pStatus = JOptionPane.showInputDialog("Enter project status");
}
}
}
File = Assignment1.java
import javax.swing.*;
public class Assignment1 {
public static void main(String[] args) {
// TODO code application logic here
String input;
int ch;
AddressBook ab = new AddressBook();
while(true)
{
input = JOptionPane.showInputDialog("1 for add\n2 for Search \n3 for delete student \n4 for add project \n5 for find project \nEnter 6 to delete all projects of a student\nEnter 7 to exit the Application");
ch =Integer.parseInt(input);
switch(ch){
case 1:
ab.addToBook();
break;
case 2:
ab.searchStudent();
break;
case 3:
ab.deletStudent();
break;
case 4:
ab.addProject();
break;
case 5:
ab.findProject();
break;
case 6:
ab.deleteProject();
break;
case 7:
System.exit(1);
}
}
}
}