Results 1 to 3 of 3
  1. #1
    Peril is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Java Dossier, Piano Student Database. Main help!?

    I'm having trouble writing a main for my database. Basically the database I have is a database for piano teachers to keep track of their students. I'll attach the code. Can anyone help me with the main? Thanks. I will include all the sections of the code. There are 7.

    Below is the Student Class
    Java Code:
    public class Student 
    {
    	// attributes
    	private String firstName = " ";	
    	private String lastName = " ";	
    	private int musicLevel = 0;	
    	private CompetitionLinkedList competitions = null;		
    	private String futureGoals = " ";
    	
    
    // default constructor - initializes default attr values
    	public Student() 
       	{
       	}
    
    // specific constructor
    	public Student(String newFirstName, String newLastName, 
    		int newMusicLevel, CompetitionLinkedList newCompetitions, String newFutureGoals)
    	{
    	   	firstName = newFirstName;
    		lastName = newLastName;
    		musicLevel= newMusicLevel;		
    		competitions = newCompetitions;		
    		futureGoals = newFutureGoals;
    	}
    
        // setters (mutators)
    	public void setFirstName(String newFirstName)
    		{
    			firstName = newFirstName;
    		}
    	public void setLastName(String newLastName)
    		{
    			lastName = newLastName;
    		}
    	public void setMusicLevel(int newMusicLevel)
    		{
    			musicLevel = newMusicLevel;
    		}
    	public void setCompetitions (CompetitionLinkedList newCompetitions) 
    		{
    			competitions = newCompetitions;
    		}
    	public void setFutureGoals (String newFutureGoals)
    		{
    			futureGoals = newFutureGoals;
    		}
        // getters (accessors)
    	public String getFirstName()
    		{
    			return firstName;
    		}
    	public String getLastName()
    		{
    			return lastName;
    		}
    	public int getMusicLevel()
    		{
    			return musicLevel;
    		}
    	public CompetitionLinkedList getCompetitions()
    		{
    			return competitions;
    		}
    	public String getFutureGoals()
    		{
    			return futureGoals;
    		}
    
    }

    Below is the Student Node

    Java Code:
    public class StudentNode
    {
    	public Student myStudent = null;
    	public StudentNode next = null;
    	
    	//default constructor
    	public StudentNode()
    	{
    		
    	}
    }

    Below is the Student Linked List


    Java Code:
    public class StudentLinkedList
    {
    	// attributes
    	private StudentNode head = null;
    	private StudentNode tail = null;
    	private int size = 0;
    
    	// default constructor
    	public StudentLinkedList()
    	{
    	}
    	
    	// isEmpty() returns true if the LinkedList is empty, and false otherwise
    	public boolean isEmpty()
    	{
    		return size == 0;
    		//return head == null;
    	}
    		
    	public boolean add(Student newStudent)
    	{
    		//1st case - adding to empty LL
    		if (isEmpty())
    		{
    			StudentNode myStudentNode = 
    				new StudentNode();
    			myStudentNode.myStudent = newStudent;
    			head = myStudentNode;
    			tail = myStudentNode;
    			size ++;
    		}
    		//2nd case - adding to non-empty LL
    		else if (!isEmpty())
    		{
    			StudentNode myStudentNode = 
    				new StudentNode();
    			myStudentNode.myStudent = newStudent;
    			tail.next = myStudentNode;
    			tail = myStudentNode;
    			size ++;
    		}
    		else
    		{
    			return false; 
    		}
    	}
    	public Student remove(int index)
    	{
    		//0) removing from index out of bounds
    		if (index >= size || index < 0)
    		{
    			return null;
    		}
    		
    		//1) remove from empty LL - return error code
    		if (isEmpty())
    		{
    			return null;
    		}
    		
    		//2) remove last StudentNode from LL
    		if (index == 0 && head == tail)	// if (size == 1)
    		{
    			Student tmp = head.myStudent;
    			head = null;
    			tail = null;
    			size--;
    			return tmp;
    		}
    		
    		//3) remove from the middle (b/w head and tail) of the LL
    		if (index != 0 && index != size -1)
    		{
    			int count = 0;
    			StudentNode previous = head;
    			for (StudentNode current = head; 
    				current != null; current = current.next)
    			{
    				//if you are at the correct current, set t
    				//he previous next to the current's next
    				if (count == index)
    				{
    					Student x = current.myStudent;
    					previous.next = current.next;
    					size--;
    					return null;
    				}
    				
    				previous = current;
    				count++;
    			}
    		}
    		
    		//4) remove from the head
    		if (index == 0 && head != tail)
    		{
    			Student tmp = head.myStudent;
    			head = head.next;
    			return null;
    		}
    		
    		//5) remove from the tail
    		if ( index == size - 1 && size > 1)
    		{
    			Student tmp = tail.myStudent;
    			
    			for (StudentNode current = head; current != null; 
    				current = current.next)
    			{
    				if (current.next == tail)
    				{
    					tail = current;
    					tail.next = null; //current.next = null;
    					size--;
    					return tmp;
    				}
    			}
    			return tmp;
    		}
    		else
    		{
    			return null;
    		}
    	}
    		
    		//add num to the location index WHY DO I HAVE THIS?
    	public void add(int num, int index)
    	{
    			//0) adding to empty LL
    //		if (isEmpty())
    //		{
    //			StudentNode myStudentNode = new StudentNode();
    //			myStudentNode.myStudent = num;
    //			head = myStudentNode;
    //			tail = myStudentNode;
    //			size ++;
    //		}
    			//1) adding to the head (beginning)
    		
    		if (!isEmpty() && index == 0) 
    		{
    			StudentNode myStudentNode = new StudentNode();
    			myStudentNode.myStudent = null;
    			tail.next = myStudentNode;
    			tail = myStudentNode;
    			size ++;
    		}
    
    		if (!isEmpty() && index == size) 
    		{
    			StudentNode myStudentNode = new StudentNode();
    			myStudentNode.myStudent = null;
    			head.next = myStudentNode;
    			tail = myStudentNode;
    			size ++;
    		}
    	}
    	
    		//print() traverses the linked list and prints out the numbers in each Node
    		//CREATE print() METHOD HERE
    	public void print()
    	{
    		for(StudentNode currentNode = head; currentNode != null; currentNode = currentNode.next)
    		{
    			System.out.print(currentNode.myStudent + " ");
    		}
    			
    	}
    
    
    }

    Below is the Competition Class

    Java Code:
    public class Competition
    {	
    	private String competitionName = " ";
    	private int score = 0;
    	
    	public Competition()
    	{
    	}
    	//specific instructor
    	public Competition(String newCompetitionName, int newScore)
    	{
    		competitionName = newCompetitionName;
    		score = newScore;
    	}
    
    	//setters
    	public void setcompetitionName(String newcompetitionName)
    	{
    		competitionName = newcompetitionName;
    	}
    	public void setScore(int newScore)
    	{
    		score = newScore;
    	}
    
    	//getters
    	public String getcompetitionName()
    	{
    		return competitionName;
    	}
    	public int getScore()
    	{
    		return score;
    	}
    }

    Below is the Competition Node

    Java Code:
    public class CompetitionNode
    {
    	public Competition c = null;
    	public CompetitionNode next = null;
    	
    	//default constructor
    	public CompetitionNode()
    	{
    		
    	}
    }

    Below is the Competition Linked List Class

    Java Code:
    public class CompetitionLinkedList
    {
    	// attributes
    	private CompetitionNode head = null;
    	private CompetitionNode tail = null;
    	private int size = 0;
    
    	// default constructor
    	public CompetitionLinkedList()
    	{
    	}
    	
    	// isEmpty() returns true if the LinkedList is empty, and false otherwise
    	public boolean isEmpty()
    	{
    		return size == 0;
    		//return head == null;
    	}
    		
    	public void add(Competition newCompetition)
    	{
    		//1st case - adding to empty LL
    		if (isEmpty())
    		{
    			CompetitionNode myNode = new CompetitionNode();
    			myNode.c = newCompetition;
    			head = myNode;
    			tail = myNode;
    			size ++;
    		}
    		//2nd case - adding to non-empty LL
    		else if (!isEmpty())
    		{
    			CompetitionNode myNode = new CompetitionNode();
    			myNode.c = newCompetition;
    			tail.next = myNode;
    			tail = myNode;
    			size ++;
    		}	
    	}
    	// THIS DOESNT WORK! PUBLIC COMPETITION DOESNT WORK WITH AN INT
    	public Competition remove(int index)
    	{
    		//0) removing from index out of bounds
    		if (index >= size || index < 0)
    		{
    			return null;
    		}
    		
    		//1) remove from empty LL - return error code
    		if (isEmpty())
    		{
    			return null;
    		}
    		
    		//2) remove last Node from LL
    		if (index == 0 && head == tail)	// if (size == 1)
    		{
    			Competition temp = head.c; // here because the node says competition is c.
    			head = null;
    			tail = null;
    			size--;
    			return temp;
    		}
    		
    		//3) remove from the middle (b/w head and tail) of the LL
    		if (index != 0 && index != size -1)
    		{
    			
    			int count = 0;
    			CompetitionNode previous = head;
    			for (CompetitionNode current = head; current != null; current = current.next)
    			{
    				//if you are at the correct current, set the previous next to the current's next
    				if (count == index)
    				{
    					Competition x = current.c;
    					previous.next = current.next;
    					size--;
    					return x;
    				}
    				
    				previous = current;
    				count++;
    			}
    		}
    		
    		//4) remove from the head
    		if (index == 0 && head != tail)
    		{
    			Competition tmp = head.c;
    			head = head.next;
    			return tmp;
    		}
    		
    		//5) remove from the tail
    		if ( index == size - 1 && size > 1)
    		{
    			Competition tmp = tail.c;
    			
    			for (CompetitionNode current = head; current != null; current = current.next)
    			{
    				if (current.next == tail)
    				{
    					tail = current;
    					tail.next = null;	//current.next = null;
    					size--;
    					return tmp;
    				}
    			}
    			return tmp;
    		}
    		return null;
    	}
    		
    		//add num to the location index
    	public void add(int num, int index)
    	{
    		//0) adding to empty LL
    		if (isEmpty())
    		{
    			CompetitionNode temp = new CompetitionNode();
    			temp.c = null;
    			head = temp;
    			tail = temp;
    			size ++;
    		}
    		//1) adding to the head (beginning)
    		if (!isEmpty() && index == 0 )
    		{
    			CompetitionNode temp = new CompetitionNode();
    			temp.c = null;
    			temp.next = head;
    			head = temp;
    			size ++;
    		}
    		//2) adding to the tail (end)
    		if (!isEmpty() && index == size)
    		{
    			CompetitionNode temp = new CompetitionNode();
    			temp.c = null;
    			tail.next = temp;
    			tail = temp;
    			size ++;
    		}
    		//3) adding to the middle
    		if (!isEmpty() && index < size && index > 0)
    		{
    			int count = 0;
    			CompetitionNode previous = head;
    			for(CompetitionNode current = head; current != null; current = current.next)
    			{
    				if (count == index)
    				{
    					CompetitionNode temp = new CompetitionNode();
    					temp.c = null;
    					temp.next = current;
    					previous.next = temp;
    				}
    				previous = current;
    				count ++;
    			}
    			size ++;
    		}
    		//4) adding to an index that is out of bounds
    		if (!isEmpty() && index > size)
    		{
    			System.out.println("Error. Index is out of the range.");
    		}
    	}
    	
    	//getNum(int index) returns the number found in the Node at the specified index
    	//CREATE getNum() METHOD HERE
    	
    	public Competition getCompetition(int index)
    	{
    		int count = 0;
    		//int tmp = CompetitionLinkedList.find; // For returning a index?
    		for(CompetitionNode current = head; current != null; current = current.next)
    		{
    			if (count == index)
    			{
    				return current.c;
    			}
    			count ++;
    		}
    		return null;
    	}
    	//print() traverses the linked list and prints out the numbers in each Node
    	//CREATE print() METHOD HERE
    	public void print()
    	{
    		for(CompetitionNode currentNode = head; currentNode != null; currentNode = currentNode.next)
    		{
    			System.out.print(currentNode.c + " ");
    		}
    	}
    
    }



    And finally, my main.

    Java Code:
    import java.io.*;
    public class pianoStudentDatabaseMain
    {
    
    	private static BufferedReader stdin = 
    	    new BufferedReader( new InputStreamReader( System.in ) );
    
    	public static void main(String[] args) throws IOException
    	{
    
    		pianoStudentDatabaseMain database = new pianoStudentDatabaseMain();
    		System.out.println("Welcome to the Piano Student Database.");
    		while(true)
    		{
    			
    			
    			System.out.println("(main menu)");
    			String input = stdin.readLine();
    			
    			System.out.println("\n" + "Select (S) to search for a student, give first name");
    			System.out.println("\n" + "Select (A) to add a new student");
    			System.out.println("\n" + "Select (E) to edit an existing student");
    			System.out.println("\n" + "Select (Q) to quit");
    			
    			if (input.equals("S"))
    			{
    				// SEARCH GOES HERE
    			}
    			else if (input.equals("A"))
    			{	
    				Student newStudent = new Student();				
    
    				System.out.print( "Input first name: ");
    				input = stdin.readLine();
    				newStudent.setFirstName(input);		
    				
    				System.out.print( "Input last name: ");
    				input = stdin.readLine();
    				newStudent.setLastName(input);	
    				
    						
    				System.out.print( "Input music level: ");
    				input = stdin.readLine();
    				newStudent.setMusicLevel(Integer.parseInt(input));
    						
    				
    				System.out.print( "Input future goals for the selected student: ");
    				input = stdin.readLine();
    				newStudent.setFutureGoals(input);
    				
    				book.addStudent(newStudent);
    						
    			
    			}
    			else if (input.equals("E"))
    			{
    				
    			}
    
    			else if (input.equals("Q"))
    			{
    				System.out.println("You have quit the Piano Student Database.");
    				break;
    			}
    
    			else if (input.equals("S"))
    			{
    				book.selectionSort();
    				System.out.println("Selection list: ");
    				book.print();
    			}
    			
    			
    			
    			else
    			{
    				System.out.println("Error, inccorect input.");
    			}
    		}
    	
    	}
    }
    Last edited by Peril; 03-09-2010 at 04:10 AM. Reason: Clarification

  2. #2
    Peril is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Edit

    I've since gone back and fixed all the errors. Still can't make the main though...

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    Hi,
    Please don't expect the people to fix even the compilation errors.

    I just given a look at ur code.where u have declared the book?

    For add method where u have returned?
    Ramya:cool:

Similar Threads

  1. Replies: 7
    Last Post: 01-20-2010, 11:35 AM
  2. Netbeans java bsased student project
    By jagadish_321 in forum NetBeans
    Replies: 1
    Last Post: 02-04-2009, 04:28 AM
  3. New java student needs help
    By cmizer in forum New To Java
    Replies: 9
    Last Post: 12-13-2008, 06:16 AM
  4. Replies: 5
    Last Post: 10-06-2008, 12:11 AM
  5. Replies: 1
    Last Post: 08-06-2007, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •