Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I do the AddDepartment and DeleteDepartment functions? This is my code:

ID: 3698081 • Letter: H

Question

How can I do the AddDepartment and DeleteDepartment functions?

This is my code:

import java.util.Scanner;

class College
{
public String CollegeName;
public int numofdeps;
public Department departments;
public College next;
public College PMU;
  
public College(String CollegeName, int numofdeps)
{
this.CollegeName = "";
this.numofdeps = 0;
this.PMU = null;
}
  
public void AddCollege()
{


  
Scanner in = new Scanner(System.in);
  
System.out.print("College name: ");
CollegeName = in.nextLine();

System.out.print("Enter the number of departments: ");
numofdeps = in.nextInt();

College tmp = new College(CollegeName,numofdeps);
tmp.next = PMU;
PMU=tmp;   

}
  
public void DeleteCollege() {
Scanner in = new Scanner(System.in);

String name = null;
System.out.println("Enter college name to delete:");
CollegeName = in.nextLine();
  
boolean found = false;
while(name != null) {
if(name.equals(CollegeName)) {

found = true;
break;
}
PMU = PMU.next;
PMU = PMU.next;
}
if(!found)
{
System.out.println(" College : "+CollegeName +" not found");
}
if(found)
{
System.out.println(" College : "+CollegeName +" is found");
}

}
public void Print()
{
System.out.println("ALL Colleges in Database : "+CollegeName);
College temp = PMU;
while(temp!=null)
{
temp.Print();
temp=temp.next;
}
}
public void FillDatabase()
{
  
}
}

class Department
{
public String DepName;
public int numofstuds;
public Department next;
  
public Department()
{
  
}
  
public void AddDepartment()
{
  
}
public void DeleteDepartment()
{
  
}
  
}
public class JavaApplication8 {


public static void main(String[] args) {
  
int choice;
String answer;
Scanner in = new Scanner(System.in);
  
College PMU = new College("Business", 2011);

  

System.out.print("Please select any item from the following menu: "
+ "1: Add a new college in the list " +
"2: Delete a college from the list " +
"3: Add a department in a college " +
"4: Delete a department from a college. " +
"5: Print all the colleges along with their departments "
+"Enter here: ");
choice = in.nextInt();
System.out.println(" ");

switch (choice)
{
case 1: PMU.AddCollege();
break;
case 2: PMU.DeleteCollege();
break;
case 3:
break;
case 4:
break;
case 5: PMU.Print();
break;
}
  
  
}
}
  

And also, can someone please fix the DeleteCollege and Print functions? In DeleteCollege, if a college is deleted then all of it departments should be deleted as well. Print function should print all the colleges along with their departments.

To make it clear,

1. Add a department in the list of departments in a college. You should ask two things, the name of the college to know which college you should add the department, and the department information to add that department in the list of departments of this particular college.

2. Delete a department from the list of departments in a college. You should ask department’s name to delete it from the list. First find out which college has this department and then delete this department from that college.

PS: PMU is the head.

Thank you so much if you do this. Its hard for me because I'm not an expert but I'm sure it's very easy.

Explanation / Answer

#include<iostream>
#include<string>
#include<string.h>
using namespace std;

class Department
{
public:
   string name;
   int numOfStudents;
   Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
   void Print(){cout<<name<<" "<<numOfStudents;}
  
};
class College
{
public :
string name; //for name of the colleg
int numOfColleges;
int numDepartments; //number of departments in this college
Department* dep; //this will point to the department in this college
College * next; //the next pointer to point to the next college
College(){name =" "; numDepartments=0 ;dep=NULL; next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){cout<<name<<" ";}


};


  
void AddCollege( College* &head)
   {
       string n;
       int numD;
       cout<<"Enter the name of the College : ";
       cin>>n;
       cout<<"Enter the number of Departments : ";
       cin>>numD;
      
       College* tmp = new College(n,numD);
if(!head)
   {
       head=tmp;
       return;
   }
College * t=head;

while(t->next) t=t->next;
   t->next=tmp;
      
       cout<<"college added";
   }
  
   void DeleteCollege(College*&head)
   {
       string name;
       cout<<"enter name of college:";
   cin>>name;
if((!head)||(!head->next && head->name!=name))
   {cout<<"could not find "<<name<<" in the list "; return;}

if(head->next && head->name==name)
   {
       College *tmp=head;
   head=head->next;
   delete tmp;
       tmp=NULL;
       return;
   }

   College* tmp = head;
   College* t;
  
while(tmp->next)
   {
if(tmp->name==name)
       {
       College* tmp = head;
   head = head->next;
   delete tmp;
       tmp->next=NULL;
       }
if(tmp->next->name == name)
       {
           t = tmp->next;
           tmp->next = tmp->next->next;
           delete t;          
           return;
       }
       tmp=tmp->next;
   }
   cout<<"could not find "<<name<<" in the list ";
   };
  
/*Print the list of colleges int the college head*/
void printColleges(College*& head)
{
   cout<<"ALL Colleges in Database : ";
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
}

void AddDepartment( College* &head)
{ if(!head)
{ cout<<"NO COLLEGES ADDED!!";
return;}


   string n,Dname;
   int numS;

College* tmp = head;
College*tmp2 = tmp;
;
while(tmp)
       {
       cout<<"->"<<tmp->name;
       tmp=tmp->next;
       cout<<endl;
       }
cout<<"Type a College name to Add a Department inside ";
cin>>n;
       cout<<"Enter Department name:";
       cin>>Dname;
   cout<<"Enter the number of students :";
   cin>>numS;

while(n!=tmp->name)
{
   tmp=tmp->next;
}

  


if(!tmp->dep)
{
   College * tmpD = new Department(Dname,numS);
   Department*tmpDD = tmpD;
   head=tmp;
      
return;
}

/*if(tmp->dep)
   {
Department *tmp3 = tmp->dep->next;
   t=tmp->dep;
   dep->next=tmp3;
}
   */
};




//void DeleteDepartment(College* head )
//{

   //
   //
   // ;}

void PrintAll(College*head)
{
  
College * temp = head;
while(temp!=NULL)
         
{
temp->Print();
temp=temp->next;
}

   };

  

int main()
{
   int choice=0;
College * h = NULL;
  
while(choice!=11) //Menu choice repeat
{
cout<<" Menu ";
cout<<"1:   Add a new college in the list ";
cout<<"2:   Delete a college from the list ";
cout<<"3:   Add a department in a college ";
cout<<"4:   Delete a department from a college. ";
cout<<"5:   Print all the colleges along with their departments ";
cout<<"6:   Delete all the departments of a particular college ";
    cout<<"7:   Delete all the colleges from the list. ";
   cout<<"8:   Print the total number of students in a college. ";
   cout<<"9:   Find and print the college that has the highest number of students. ";
   cout<<"10:   Find and print the department that has the highest number of students. ";
   cout<<"EXIT ";
   cin>>choice;
switch(choice)
{

   case 1: AddCollege(*&h);
       break;
     
   case 2: DeleteCollege(*&h);
  
       break;

   case 3: printColleges(*&h);
       break;
case 4:
AddDepartment(*&h);
break;
/* case 4:
DeleteDepartment(*&h);
break;
case 5:
PrintAll(*&h);
break;
case 6:
       DeleteDepartment(*&h);
break;
   case 7:
       DeleteAllColleges(*&h);
       break;

   case 8:
       NumOfStudentsInCollege(*&h);
       break;
   case 9:
       HighestNumOfStudentsInCollege(*&h);
       break;
   case 10:
       HighestNumOfStudentsInDep(*&h);
   break;
   case 11:
       cout<<"bye";
       break;*/

   default:

cout<<" Invalid menu choice";
}

  

}
};

i think this will solve your problem. check it once and execute

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote