Hi,

I am working on a Java assignment of developing a University diploma program as follows:
uni-diploma (one-to-many)
diploma-module(one-many)
module-lectures(one-many)
lecture-student(many-many)

class uni contains
TreeSet diploma
all set and get methods for adding each new diploma in the set and association to class diploma.

class diploma contains
TreeMap module
all set and get methods for inserting each module into the map module and the

class module contains
ArrayList lecture
all set and get methods

class lecture contains
ArrayList student
association to class student and the set and get methods.

class student contains
ArrayList studies and association to class lecture etc

I also have a driver class to run the JOptionPane interface.
My program is working in the sense I am accordingly able to create a new diploma then a new module ..a new lecture and assign a student objects etc.
The problem is with not being able to save the instances.
I mean once I assign a student to a lecture, if i go back to create another lets say lecture, i loose the previous one!!hehe
I know this is by design of my program. The problem is I do not know what I need to do to get over this without changing my basic design. Please help this novice. !!

Recommended Answers

All 14 Replies

Post the full java code here and specify which function you are having problem with.

I got this feeling that the garbage handler is eating up my lecture instance objects.
if someone can please, give me a workaround insight into avoiding this so as to serve my purpose...thanx muxh in advance.

Your GC has nothing to do with it. Here is a small example that will help you understand what you are doing:

Person p = new Person();
p.setName("My Name");
System.out.println(p.getName()); // will print My Name

p = new Person();
System.out.println(p.getName()); // will print null

You successfully create the lecture but when you go back that instance is out of scopt if you "close" the class, so it's gone. Do you have any ArrayLists or Vectors to store what you have created?
I mean you need to save somewhere what you have created.

thnx for the example..I think I understand what you y=trying to say...about the vector or the arraylist you talk about...well i have used variety of collection as per the teacher's wish but I dont know...err..It makes sense when you say I need to save them somewhere but,,,ah i can't seem to find anywhere to place them.
Heres the classes and a sample driver..could you give me some direction please.. thnx much again.appreciate it your time.

import java.util.*;



public class Uni {
	
	TreeSet diploma=new TreeSet();
	
	
@SuppressWarnings("unchecked")
public void addDiploma(Diploma name){
	String message;
	//check if the entry exists
		this.diploma.add(name.getName());
}		

public String displayAllDiplomas(){
	String dipStrings="";
	Iterator listOfDip=this.diploma.iterator();

	//display elements in the iterator	
	while(listOfDip.hasNext()){
		
		dipStrings=(dipStrings+(String)(listOfDip.next()))+"\n";
	}
	return (dipStrings);
}

}
import javax.swing.JOptionPane;
import java.util.*;

public class Module {
	private String moduleName,credit;
	 ArrayList student=new ArrayList();
	public Module() {}
	
	public Module(String moduleName,String credit){
		setModuleName(moduleName);
		setCredit(credit);
			
	}
	
	public void addStudent(Student aStudent){
			
				this.student.add(aStudent);
	}
				
	public void removeBooking(Student aStudent){
			
			if(this.student.contains(aStudent)){
				this.student.remove(aStudent);
			}
	}
		
	public String toString(){
		String asString;
		asString="Module Name:"+" "+this.moduleName ;
		asString=asString+ "\nModule Credit: "+" "+this.credit+" ";
		
		return asString;
	}
	public void printDetails() {
		String output;
		output=toString();
		JOptionPane.showMessageDialog(null,output,"Module Details",JOptionPane.INFORMATION_MESSAGE);
		
	}
	public void printDetailsOfStudentsOnModule(){
		String output;
		output=toString()+"\n Student Details:\n";
		Iterator allStudent=this.student.iterator();
		while(allStudent.hasNext()){
			output=output+((Student)(allStudent.next())).toString();
		}
		JOptionPane.showMessageDialog(null,output,"Student Details",JOptionPane.INFORMATION_MESSAGE);
	}
	
	//set methods

	private void setModuleName(String modulename){
		this.moduleName=moduleName;
	}
	private void setCredit(String credit){
		this.credit=credit;
	}
	//get methods
	public String getModuleName(){
		return this.moduleName;
	}
	public String getCredit(){
		return this.credit;
	}
		
}
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;

public class Diploma {

	 TreeMap modules=new TreeMap();
	//instance variables
	private String name;
		


public Diploma (){} //empty constructor

public Diploma(String name){
		setName(name);
}

public  void addModule(Module module){
	
	modules.put(module.getModuleName(), module);
		
	}
		
public  String displayAllModule(){
	String moduleString="";
	Module tempEntry;
	Iterator allModules=this.modules.values().iterator();
	
	while(allModules.hasNext()){
		tempEntry=(Module)allModules.next();
		moduleString=moduleString+tempEntry.toString();
	}
	return moduleString;
}
public String toString(){
	String asString;
	asString="Lecture Name:"+this.name+"\n";
	return asString;
	}

//set method

private void setName(String name){
	this.name=name;
}
//get method
public String getName(){
	return this.name;
}


}//end of class
import javax.swing.JOptionPane;
import java.util.*;

public  class Student {
	//instance variable
	String firstName,secondName,name;
	protected  ArrayList allModules=new ArrayList();
		
	public Student(){}//empty constructor
	
	public Student(String name){
		setName(name);						
	}
	
		
	public String toString(){
		String output;
		output=" "+"Name:"+" "+this.name+" "+"\n";
		return output;
	}
	
	//set and get methods
	
	public void setName(String name){
		this.name=name;
	}
	
	//get
	
	public String getName(){
		return this.name;
	}
	
	public void printDetails() {
		String output=toString();
		Iterator allModules=this.allModules.iterator();
		while(allModules.hasNext()){
			output=output+((AirlineFlight1)(allModules.next())).toString();
		}
		
		JOptionPane.showMessageDialog(null,output,"Student Module Details",JOptionPane.INFORMATION_MESSAGE);
		
	}
		
}//end class
import java.util.*;

import javax.swing.JOptionPane;

public class Unidriver {
	
	//instance variable
	Uni theUni=new Uni();
	Uni tempUni;
	Diploma theDiploma=new Diploma();
	Diploma tempDiploma;
	Module theModule;
	Student theStudent;
	 
	static String diploma1;

	public Unidriver () {}//empty constructor
	
	public void displayMenu(){
		String output,UserChoiceString;
		int userChoice;
		
		do{
			output="University system \n";
			output=output+"Select a Menu Option ( 0 to quit )\n\n";
			output=output+"1- Diploma Management\t\n"+"2- Student management";
			
			UserChoiceString=askUser(output);
			userChoice=Integer.parseInt(UserChoiceString);
			
			switch(userChoice){
			case 1:
				diplomaManagementMenu();
				break;
				
			case 2:
				studentManagementMenu();
				break;
			}
			
		}while(userChoice!=0);
		System.exit(0);
	}//end display menu method
	
	private void diplomaManagementMenu(){
		String output,UserChoiceString;
		int userChoice;
		do{
		output="Diploma Management System \n";
		output=output+"Select a Menu Option ( 0 to quit )\n\n";
		output=output+"1- Add a New Diploma\n"+"2- View all Diplomas \n3- Go Back";
		
		UserChoiceString=askUser(output);
		userChoice=Integer.parseInt(UserChoiceString);
		switch(userChoice){
		case 1:
			addDiploma();
			break;
			
		case 2:
			displayAllDiploma();
			break;
		case 3:
			displayMenu();
		}
			
		}while(userChoice!=0);
		System.exit(0);
	}
	
	private void addDiploma(){
		Diploma aDiploma;
		aDiploma=getDiplomaDetails();
		theUni.addDiploma(aDiploma);
		
	}
	
	public Diploma getDiplomaDetails(){
		String aname;
		Diploma aaDiploma;
		aname=askUser("Diploma Name ");
		
		aaDiploma=new Diploma(aname);
		return aaDiploma;
	}
	private void displayAllDiploma(){
		String allDiploma;
		String title="List of diplomas\n\n";
		allDiploma=this.theUni.displayAllDiplomas();
		//check for no entries
		display(title+allDiploma);
		
	}
	
	private void studentManagementMenu(){
		
		ArrayList keys=new ArrayList(this.theUni.diploma);
		String output;
		String diploma;
		int choice,count;
		
		count=0;
		output=" select the Diploma Menu \n";
		Iterator listOfDiploma=this.theUni.diploma.iterator();
		while(listOfDiploma.hasNext()){
			count++;
			output=output+count+":"+(String)(listOfDiploma.next())+"\n";
		}
		do{
			
			String userChoice=JOptionPane.showInputDialog(output);
			choice=Integer.parseInt(userChoice);
		}while(choice<1||choice>count);
		diploma1=(String)keys.get(choice-1);
		tempDiploma=new Diploma(diploma1);
		studentMenu();
	}
		
	private void studentMenu(){
		String output1,UserChoiceString;
		int userChoice;
		do{
			
			output1="Module MANAGEMENT :\n"+" "+"1- Add  New Module\n"+" "+"2- Display all Modules for"+" "+diploma1+" "+
			"\n\n"+"Student MANAGEMENT :\n"+" "+"3- Register New Student to a Module\n"+" "+"4- Cancel Module Registration\n"+" 5-GO Back\n";
			
			UserChoiceString=askUser(output1);
			userChoice=Integer.parseInt(UserChoiceString);
			switch(userChoice){
			case 1:
				addModule();				
			break;
				
			case 2:
				displayAllModule();
				break;
			case 3:
				addStudent();
				break;
			case 4:
				//removePassenger();
			
			case 5:
				displayMenu();
			
				
			}
		}while(userChoice!=0);
		System.exit(0);
	}
	private void addModule(){
		Module tempModule;
		tempModule=getModuleDetails();
		this.theDiploma.addModule(tempModule);
	}
	private Module getModuleDetails(){
		Module newModule;
		String moduleName,credit;
		moduleName=askUser("Please enter the Name of Module");
		credit=askUser("Please enter the credit alloted");
				
		newModule=new Module(moduleName.toUpperCase(),credit);
		return newModule;
	}
	private void displayAllModule(){
		String allModule;
		String title=diploma1+" "+"- Currently Awarded Diplomas in Order\n\n";
		allModule=this.theDiploma.displayAllModule();
		
		display(title+allModule);
	}
	
	private void addStudent(){
		//..........
	}
	private void getStudentDetails(){
		//...........
				
	}
	private void displayAllStudent(){
		//...
	}
	
	/*display String*/
	private void display(String aMessage){
		JOptionPane.showMessageDialog(null,aMessage,"Uni System",JOptionPane.INFORMATION_MESSAGE);
	}
	/*user input method*/
	private String askUser(String aMessage){
		return JOptionPane.showInputDialog(aMessage);
	}

}
public class RunDemo{

	public static void main(String[] args) {
		Unidriver theInterface=new Unidriver();
		theInterface.displayMenu();
		System.exit(0);
		

	}

}

What are you having trouble with? You said you're having trouble storing things in a list, but I see from your diploma code where you add the thing to the TreeSet that you are doing this fine. An ArrayList works the same way. Going off of JavaAddict's earlier example,

ArrayList<People> list = new ArrayList<People>();
list.add(new People());

Maybe you should give an example of what you're talking about or a more specific section of code you can't do or something.

It will take me sometime to look the code, but I have some suggestions which I will post later, when I have the time.
And I did look at the code and I think I know what you should change.

First of all the Uni class has a TreeSet that takes type "Diploma". So when you do this:

ArrayList keys=new ArrayList(this.theUni.diploma);

The keys has Diploma inside it. So you don't need to do this:

diploma1=(String)keys.get(choice-1);
tempDiploma=new Diploma(diploma1);

This would be simpler:

Diploma dipl = (Diploma)keys.get(choice-1);

With the above you get the diploma that is inside the ArrayList, you don't create a new one. Which brings me to what think is the problem. Now I don't know where are you having your problem but this is what I think:

You declare this: Diploma tempDiploma; as global at the begining of your class. You don't use that anywhere it is just a diploma.
So when you do this: tempDiploma=new Diploma(diploma1); it has no meaning

The same goes for the Diploma theDiploma=new Diploma(); I don't know what you are trying to accomplish but if you want to add the new module to the diploma selected:

studentManagementMenu() {
....
}

Then you need to add it to this diploma: Diploma dipl = (Diploma)keys.get(choice-1); So you should have the studentMenu() takes as argument a Diploma and pass as argument the diploma selected. Then pass as argument the same diploma to the addModule() method


If this is not your problem give more details of what you are trying to accomplish and which part of your code doesn't work.

Hi,
Thank you again for taking time to go through the codes.
As per your suggestion , I have added the argument dip1 to studentMenu() to make it studentMenu(dip1).
Further I have passed the same Diploma dip1 argument to addModule() . I am getting a String cannot be cast to Diploma @ UniDriver.StudentManagementMenu Error.
Here is the Main method again. Could you go through it again please.

import java.util.*;

import javax.swing.JOptionPane;

public class Unidriver {
	
	//instance variable
	Uni theUni=new Uni();
	Diploma theDiploma=new Diploma();
	Module theModule=new Module();
	 
	public Unidriver () {}//empty constructor
	
	public void displayMenu(){
		String output,UserChoiceString;
		int userChoice;
		
		do{
			output="University system \n";
			output=output+"Select a Menu Option ( 0 to quit )\n\n";
			output=output+"1- Diploma Management\t\n"+"2- Student management";
			
			UserChoiceString=askUser(output);
			userChoice=Integer.parseInt(UserChoiceString);
			
			switch(userChoice){
			case 1:
				diplomaManagementMenu();
				break;
				
			case 2:
				studentManagementMenu();
				break;
			}
			
		}while(userChoice!=0);
		System.exit(0);
	}//end display menu method
	
	private void diplomaManagementMenu(){
		String output,UserChoiceString;
		int userChoice;
		do{
		output="Diploma Management System \n";
		output=output+"Select a Menu Option ( 0 to quit )\n\n";
		output=output+"1- Add a New Diploma\n"+"2- View all Diplomas \n3- Go Back";
		
		UserChoiceString=askUser(output);
		userChoice=Integer.parseInt(UserChoiceString);
		switch(userChoice){
		case 1:
			addDiploma();
			break;
			
		case 2:
			displayAllDiploma();
			break;
		case 3:
			displayMenu();
		}
			
		}while(userChoice!=0);
		System.exit(0);
	}
	
	private void addDiploma(){
		Diploma aDiploma;
		aDiploma=getDiplomaDetails();
		theUni.addDiploma(aDiploma);
		
	}
	
	public Diploma getDiplomaDetails(){
		String aname;
		Diploma aaDiploma;
		aname=askUser("Diploma Name ");
		
		aaDiploma=new Diploma(aname);
		return aaDiploma;
	}
	private void displayAllDiploma(){
		String allDiploma;
		String title="List of diplomas\n\n";
		allDiploma=this.theUni.displayAllDiplomas();
		//check for no entries
		display(title+allDiploma);
		
	}
	
	private void studentManagementMenu(){
		
		ArrayList keys=new ArrayList(this.theUni.diploma);
		String output;
		String diploma;
		int choice,count;
		
		count=0;
		output=" select the Diploma Menu \n";
		Iterator listOfDiploma=this.theUni.diploma.iterator();
		while(listOfDiploma.hasNext()){
			count++;
			output=output+count+":"+(String)(listOfDiploma.next())+"\n";
		}
		do{
			
			String userChoice=JOptionPane.showInputDialog(output);
			choice=Integer.parseInt(userChoice);
		}while(choice<1||choice>count);
		[B]Diploma dipl = (Diploma)keys.get(choice-1);
		
		studentMenu(dipl);
	}
		
	private void studentMenu(Diploma dip1){
		String output1,UserChoiceString;
		String diploma1=(String)dip1.getName();[/B]
		int userChoice;
		do{
			
			output1="Module MANAGEMENT :\n"+" "+"1- Add  New Module\n"+" "+"2- Display all Modules for"+" "+diploma1+" "+
			"\n\n"+"Student MANAGEMENT :\n"+" "+"3- Register New Student to a Module\n"+" "+"4- Cancel Module Registration\n"+" 5-GO Back\n";
			
			UserChoiceString=askUser(output1);
			userChoice=Integer.parseInt(UserChoiceString);
			switch(userChoice){
			case 1:
			[B]	addModule(dip1);				
			break;
				
			case 2:
				displayAllModule(dip1);
				break;
						
			}
		}while(userChoice!=0);
		System.exit(0);
	}
	private void addModule(Diploma dip1){
		Module tempModule;
		tempModule=getModuleDetails();
		dip1.addModule(tempModule); [/B]
	}
	private Module getModuleDetails(){
		Module newModule;
		String moduleName,credit;
		moduleName=askUser("Please enter the Name of Module");
		credit=askUser("Please enter the credit alloted");
				
		newModule=new Module(moduleName.toUpperCase(),credit);
		return newModule;
	}
	private void displayAllModule(Diploma dip1){
		String allModule;
		String title=(String)dip1.getName()+" "+"- Currently Awarded Diplomas in Order\n\n";
		allModule=dip1.displayAllModule();
		
		display(title+allModule);
	}
	
		
	/*display String*/
	private void display(String aMessage){
		JOptionPane.showMessageDialog(null,aMessage,"Uni System",JOptionPane.INFORMATION_MESSAGE);
	}
	/*user input method*/
	private String askUser(String aMessage){
		return JOptionPane.showInputDialog(aMessage);
	}

}

I took the code and I don't see any errors apart from this at the Student class:

public void printDetails() {
		String output=toString();
		Iterator allModules=this.allModules.iterator();
		while(allModules.hasNext()){

// WHAT IS THE AirlineFlight1 ? 			
output=output+((AirlineFlight1)(allModules.next())).toString();
		}
		
		JOptionPane.showMessageDialog(null,output,"Student Module Details",JOptionPane.INFORMATION_MESSAGE);
		
	}

Also when you call the getName() you don't need to cast it to String because the method already returns a String.

OK there is your problem at the Uni class:

public void addDiploma(Diploma name){
	String message;
	//check if the entry exists
		this.diploma.add(name.getName());
}

You are supposed to add diplomas at the TreeSet, not their names. The diploma has more than just a name, it has Modules. So when you add the "diploma" object to the TreeSet, you add everything the diploma has(name and modules)
So when you create the ArrayList and get an element from it you get the entire diploma with name and everything.
With the way you had it you were adding the only the name which is a String: this.diploma.add(name.getName()); But when you were getting the elements you were casting it to Diploma.
You cannot take the name and create a new Diploma because you will loose any modules that the Diploma had. You need to add to the TreeSet the entire Diploma.

oh, thats my mistake.err..it should have been (Module).The Air..thingy is a class from some other program.
After correction I am still getting an error when I run it on eclipse ide i get this message:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to airlineReservationSys.Diploma
	at airlineReservationSys.Unidriver.studentManagementMenu(Unidriver.java:110)

This looks like because of the code at line 116. Could you check it again for me please. Thank you.

right.Looks like you hit the bull's eye this time for me...Thank you so much.U are amazing, a help. Ill try and work it out agin.
WIll be back, if I again come to the dead end :).
Thnx again.cheers

btw when you say ** I need to add to the treeset the entire diploma** u just mean I change it from

this.diploma.add(name.getName());
to
this.diploma.add(name);

right??

btw when you say ** I need to add to the treeset the entire diploma** u just mean I change it from

this.diploma.add(name.getName());
to
this.diploma.add(name);

right??

Yes, in that way the TreeSet will have the enire Diploma inside it, not just the its name.

Also you need to change the casting when you get the object from the TreeSet. Don't worry you will get the exceptions that will tell you where. Remember the TreeSet and ArrayList now have type diploma.


Also I have noticed 3 more mistakes. Once you have corrected those you will be ready to go. Just do some tests

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.