Results 1 to 1 of 1
  1. #1
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Help making a Menu system for Student List System

    Hey, so i have to create a program which allows the user to add/edit/remove Student Details (Forename, Surname and Mark), and several other analysis methods, such as show the average mark in the class, show all pass students, etc. so here is my code so far,

    UnitResults.java (holds all methods for the program)
    Java Code:
    package assignment1;
    
    public class UnitResults 
    {
        private String [] studentName = new String [20];
        private int [] studentMark  = new int [20];
        public String unitTitle;
        private static String course;
        private static int pointer;
        
    public UnitResults()
        {
            pointer = 0;
        }
    public String displayAll()
        {
            String strOutput = "";
            //System.out.println("Students\n");
            strOutput = strOutput + "Student List for the " + unitTitle + " Unit\n------------------------------------------\n";
            
            if(pointer == 0)
                {    
                    //System.out.println("There are no Elemets in the Array\n");
                    strOutput = strOutput + "There are no Students in the Class\n";
                    return strOutput;
                }
            
            for(int index = 0; index < pointer; index++)
                {
                    //System.out.println(studentName[index] + " " + studentMark[index]);
                    strOutput = strOutput + studentName[index] + " " + studentMark[index] + "\n";
                }
            return strOutput;
        }//End of display Method
        
    public String add(String foreName, String surName, int mark)
        {
            String strOutput = "";
            if(pointer == studentName.length)
            {
                strOutput = strOutput + "The Class is full!\n";
                return strOutput;
            }
            
            studentName[pointer] = (foreName + " " + surName);
            studentMark[pointer] = mark;
            pointer++;
            
            strOutput = strOutput + "Student was successfully added.\n";
            
            return strOutput;    
        }//End of add Method.
        
    public String delete(int element)
        {
            String strOutput = "";
            element = element - 1; //array location
            String tempName = studentName[element];
            if ((element >= 0) && (element < pointer))
            {
                for(int index = (element + 1); index < pointer; index ++)
                {
                    studentName[index -1] = studentName[index];
                    studentMark[index -1] = studentMark[index];
                }
                pointer--;
                strOutput = strOutput + "This student has now been deleted: " + tempName + "\n";
                return strOutput;
            }
            else
            {
                return strOutput = strOutput + "There are no students in the Class List.";
            }
        }//End of delete Method
    
    public String searchStudentNumber (int element)
        {
            String strOutput;
            int tempPointer;
            int tempElement;
      
            tempElement = element - 1;
            tempPointer = pointer;
            pointer = tempElement;
            
            if (pointer < 0)
            {
                strOutput = "There are no Elements in the Array\n";
                pointer = tempPointer;
                return strOutput;
            }
            else if (pointer > studentName.length)
            {
                strOutput = "This area in space is empty.\n";
                pointer = tempPointer;
                return strOutput;
            }
            else
            {
                
                strOutput = "The student with the ID: " + element + " is:\n" + studentName[pointer] + " " + studentMark[pointer] + "\n";
                pointer = tempPointer;
                return strOutput;
            }   
        }//end of searchStudentNumber
    
    public String updateResult(int element, int NewResult)
        {
            String strOutput;
            int tempPointer;
            element = element - 1;
            tempPointer = pointer;
            pointer = element;
            
            studentMark[pointer] = NewResult;
            pointer = tempPointer;
            
            return strOutput = "The Students Result has been updated.\n";
        }//end of updateResult
    
    public String updateName(int element, String NewForename, String NewSurname)
        {
            String strOutput;
            int tempPointer;
            element = element - 1;
            tempPointer = pointer;
            pointer = element;
            
            studentName[pointer] = NewForename + " " + NewSurname;
            pointer = tempPointer;
            return strOutput = "The Students Name has been updated.\n";
        }//end of updateName
    
    public String sortByName()
        {
            String strOutput;
            boolean swapped;
            do
            {
            swapped = false;
                for (int index = 0; index < studentName.length-1; index++) 
                {
                    //System.out.println("Debug 1: " + studentName[i]);
    
                    if(studentName[index].compareTo(studentName[index+1])>0)
                    {             
                        String tempName = studentName[index+1];
                        studentName[index+1] = studentName[index];
                        studentName[index] = tempName;
    
                        int tempResult = studentMark[index+1];
                        studentMark[index+1] = studentMark[index];
                        studentMark[index] = tempResult;
    
                        swapped=true;
                    }
                }
            }
            
            while(swapped);   
            return strOutput = "The Student Class List has been Sorted Alphabetically.\n";         
        }//end of sortByName Method
    
    public String sortByResult()
        {
            String strOutput;
            boolean swapped;
            do
            {
            swapped = false;
                for (int index = 0; index < studentMark.length-1; index++) 
                {
                    if(studentMark[index]>(studentMark[index+1]))
                    {             
                        int tempResult = studentMark[index+1];
                        studentMark[index+1] = studentMark[index];
                        studentMark[index] = tempResult;
                        
                        String tempName = studentName[index+1];
                        studentName[index+1] = studentName[index];
                        studentName[index] = tempName;
                        
                        swapped=true;
                    }
                }
            }
            while(swapped);  
            return strOutput = "The Student Class List has been Sorted Numerically.\n";
        }//end of sortByResult Method
    
    public String average()
        {
            int sum = 0;
            int average = 0;
            String strOutput = "";
    
            for(int index = 0; index < studentMark.length; index++)
                {
                    sum += studentMark[index];
                }
            
            average = sum/studentMark.length;
    
            strOutput = strOutput + "The Average Mark in the Class is: " + average + "\n";
            return strOutput;
        }//end of average Method
    
    public String passStudents()
        {
            String strOutput = "";
    
            strOutput = "All Pass Students\n------------------\n";
    
            for(int index = 0; index < studentMark.length; index++)
            {
                if(studentMark[index] >= 70)
                {
                    strOutput = strOutput + studentName[index] + " " + studentMark[index] + "\n";
                    //System.out.println(studentName[index] + " " + studentMark[index]);
                }
            }
            
            return strOutput;
        }//end of passStudents Method
    }//end of Class
    menuSystem.java (shows a menu, allowing the user to call the methods within UnitResults.java)
    Java Code:
    package assignment1;
    
    import java.util.Scanner;
    
    public class menuSystem {
        
        public static Scanner keyb = new Scanner(System.in);
        
        static UnitResults ClassList;
        
        public static int Menu()
        {
            System.out.println("Student Class List System\n");
            System.out.println("1. Add Student");
            System.out.println("2. Dispaly Student List");
            System.out.println("3. Search for Student");
            System.out.println("4. Delete Student");
            System.out.println("5. Update Student Result");
            System.out.println("6. Update Student Name");
            System.out.println("7. Sort By Name");
            System.out.println("7. Sort by Result");
            System.out.println("8. Display all Pass Students");
            System.out.println("9. Display Average Result");
            System.out.println("8. Exit System");
            
            System.out.print("\n Enter choice:");
            int option = keyb.nextInt();
            
            return option;
        }// end menu
        
        public static void main(String[] args) 
        {
            UnitResults Web = new UnitResults();
            
            int option = 0;
            
            while (option != 11)
            {
                option = Menu();
                
                switch(option)
                {
                    case 1:
                        System.out.println("Add a Student");
                        System.out.println("-------------");
                        break;
                    case 2:
                        System.out.println("Display Student List");
                        System.out.println("--------------------");
                        break;
                    case 3:
                        System.out.println("Search for Student");
                        System.out.println("------------------");
                        break;
                    case 5:
                        System.out.println("Delete Student");
                        System.out.println("--------------");
                    case 6:
                        System.out.println("Update Student Result");
                        System.out.println("---------------------");
                        break;
                    case 7:
                        System.out.println("Update Student Name");
                        System.out.println("-------------------");
                        break;
                    case 8:
                        System.out.println("Sort by Result");
                        System.out.println("--------------");
                        break;
                    case 9:
                        System.out.println("Display all Pass Students");
                        System.out.println("-------------------------");
                        break;
                    case 10:
                        System.out.println("Display Average Result");
                        System.out.println("----------------------");
                        break;
                    case 11:
                        System.out.println("Exit System");
                        System.out.println("-----------");
                        break;
                    default:
                        System.out.println("Invalid option");
                        System.out.println("--------------");
                }//end switch 
            }//end while loop  
        }//end main
    }//end of class
    so what i need help with, is how can i call a method from UnitResults, when the user chooses one of the options, for example, the user enters 1, into the menu, then the add method is used to enter a students details, this is what I'm thinking:

    Java Code:
    case 1:
                        System.out.println("Add a Student");
                        System.out.println("-------------");
                        System.out.println("Please enter the Students Forename:");
                        Web.add.foreName =  = keyb.nextLine();
                        System.out.println("Please enter the Students Surname:");
                        Web.add.surName =  = keyb.nextLine();
                        System.out.println("Please enter the Students Mark:");
                        Web.add.mark =  = keyb.nextLine();
                        break;
    I know this is wrong, although some help to get this to work would be much appreciated!!

    Thanks in Advance, Jason.
    Last edited by jason3460; 12-09-2012 at 02:44 PM.

Similar Threads

  1. system.out.printf versus system.out.format
    By bigsonny in forum New To Java
    Replies: 10
    Last Post: 06-21-2011, 11:40 PM
  2. Replies: 4
    Last Post: 03-10-2009, 07:31 AM
  3. Menu driven system - atm
    By thelinuxguy in forum Advanced Java
    Replies: 1
    Last Post: 02-19-2009, 12:14 AM
  4. Replies: 0
    Last Post: 07-25-2008, 03:32 PM
  5. List of System properties
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 05:56 PM

Posting Permissions

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