菜单界面学生记录系统

时间:2013-03-24 19:48:44

标签: java loops interface menu

我有一个部分工作的记录系统,但我在菜单界面遇到问题。基本上,菜单应该允许您:

  1. 添加学生
  2. 删除学生
  3. 打印学生
  4. 退出
  5. 现在菜单工作正常但我在添加学生部分时遇到问题。当他们添加学生时,他们需要:

    • 提示名字
    • 提示姓氏
    • 提示输入身份证号码

    当他们这样做时,它询问他们是否想要添加另一个;如果输入“y”,它再次循环,如果输入“n”,则返回主菜单。

    现在,当他们按下“n”时没有任何反应,当你按下y时,它会再次循环,但它不会让你输入任何第一个名字的值。

    以下是系统的所有代码:

    REGISTRYINTERFACE.java

    /**
     * @version 1.0
     * @author parry2411 
     */
    package student;
    
    import java.util.Scanner;
    
        public class RegistryInterface
        {
    
            private Registry theRegistry = new Registry();
            Scanner input = new Scanner(System.in);
    
            public RegistryInterface(Registry theRegistry)
            {
            }
    
            public void doMenu()
            {
                boolean done = false;
    
                while (!done)
                {
                    try
                    {
                        System.out.println("\nRegistry Main Menu\n*******************\n");
                        System.out.println("1. Add a Student \n2. Delete a Student"
                                + "\n3. Print Registry\n0. Quit");
    
                        System.out.println("Select Option [1, 2, 3, 0] :>");
                        String userDecission = input.nextLine();
    
                        int decission = Integer.parseInt(userDecission);
    
                        switch (decission)
                        {
                            case 0:
                                System.out.println("System Closing Down..");
                                break;
    
                            case 1:
                                doAddStudent();
                                break;
    
                            case 2:
                                doDeleteStudent();
                                break;
    
                            case 3:
                                doPrintRegistry();
                                break;
    
                            default:
                                System.out.println("\nPlease Enter in a valid"
                                        + " Menu Option");
                                doMenu();
                                break;
                        }
                        done = true;
                    }catch(Exception e)
                    {
                        System.out.println("Incorrect Value Entered, Try Again....");
                    }
                }
            }
    
    
        private void doAddStudent()
            {
                String addMore;
                do
                {
                    System.out.println("\nAdd New Student\n***********\n");
    
                    try
                    {
                        System.out.println("Enter Students Forename :>");
                        String fName = input.nextLine();
    
                        System.out.println("Enter Student Surname :>");
                        String sName = input.nextLine();
    
                        System.out.println("Enter Student ID Number :>");
                        int idNum = input.nextInt();
    
                        theRegistry.addStudent(new Student(fName, sName, idNum));
    
                    } catch (Exception e)
                    {
                        System.out.println("\nERROR OCCURED: Incorect Value Entered"
                                + "\nTry Again... \nStudent Was NOT added");
                    }
    
    
                    System.out.println("\nAdd Another Student (Y/N) : >");
                    addMore = input.next();
    
    
    
                } while (!"n".equals(addMore));
            }
    
            private void doDeleteStudent()
            {
                String another;
                do
                {
                    System.out.println("\nDelete Student\n***********\n");
                    System.out.println("Enter Student ID To Delete :>");
                    try
                    {
                        int studID = input.nextInt();
                        theRegistry.deleteStudent(studID);
    
                    } catch (Exception e)
                    {
                        System.out.println("\nERROR OCCURED: Incorect Value Entered"
                                + "\nTry Again...\n");
                    }
    
                    System.out.println("\nDelete Another? (Y/N)");
                    another = input.next();
    
    
                } while (!"n".equals(another));
            }
    
            private void doPrintRegistry()
            {
    
                System.out.println("\nPrinting Registry\n***********\n");
    
                if(theRegistry.studentList.size() == 0)
                {
                    System.out.println("The Student Record System Contains No Student"
                            + " Records, Please Add Students\n\n");
                }
                else
                {
                   System.out.println(theRegistry.format()); 
                    System.out.println("\n****Printing Finished........");
                }
    
                doMenu();
    
            }
        }
    

    REGISTRY.java

    /**
     * @version 1.0
     * @author parry2411 
     */
    package student;
    
    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class Registry
    {
        public LinkedList<Student> studentList = new LinkedList<>();
        public Iterator<Student> iter = studentList.iterator();
    
        public Registry()
        {
        }
    
        public void addStudent(Student aStudent)
        {
            Iterator<Student> addIterator = studentList.iterator();
    
            while (addIterator.hasNext())
            {
                Student ob = addIterator.next();
                if (ob.getStudentID() == aStudent.getStudentID())
                {
                    System.out.println("This Student ID "
                            + ""+ aStudent.getStudentID()+ " Is Already Used"
                            + "\n Try Adding Again......");
                    return;
                }
            }
            System.out.println("Student "+ aStudent.getForeName() + " "
                    + "" + aStudent.getSurName() +" "
                    + "Successfully Added To System.....");
    
            studentList.addLast(aStudent);
        }
    
        public void deleteStudent(int studentID)
        {
            Iterator<Student> deleteIterator = studentList.iterator();
            boolean removed = false;
    
            while(deleteIterator.hasNext())
            {
                Student ob = deleteIterator.next();
    
                if(ob.getStudentID() == studentID)
                {
                    deleteIterator.remove();
                    removed = true;
                    System.out.println(ob.getForeName() + " " + ob.getSurName() + " Was Succesffully Removed from System. \n");
                }
            }
            if(!removed)
            {
                System.out.println("Student ID not found");
            }
        }
    
        public String format()
        {
            StringBuilder sB = new StringBuilder();
            Iterator<Student> formatIterator = studentList.iterator();
    
            while(formatIterator.hasNext())
            {
                Student ob = formatIterator.next();
                sB.append(ob.format());      
            }
            return sB.toString();
        }
    
        @Override
        public String toString()
        {
            Iterator<Student> toStringIterator = studentList.iterator();
            StringBuilder sB = new StringBuilder();
    
            while(toStringIterator.hasNext())
            {
                Student ob = toStringIterator.next();
                sB.append(ob.toString()).append("\n");
            }
            return sB.toString();
        }
    }
    

    注册申请

    package student;
    
    public class RegistryApp
    {
    
        public static void main(String[] args)
        {
            Registry theRegistry = new Registry();
            RegistryInterface aRegInterface = new RegistryInterface(theRegistry);
            aRegInterface.doMenu();   
        }
    }
    

    Student.java

    /**
     * @version 1.0
     * @author parry2411
     * Date Created: 18-Mar-2013
     */
    
    package student;
    
    public class Student 
    {
        private String foreName;
        private String surName;
        private int studentID;
    
    
        public Student(String foreName, String surName, int studentID)
        {
            this.foreName = foreName;
            this.surName = surName;
            this.studentID = studentID;
        }
    
        public String getForeName()
        {
            return foreName;
        }
    
        public void setForeName(String foreName)
        {
            this.foreName = foreName;
        }
    
        public String getSurName()
        {
            return surName;
        }
    
        public void setSurName(String surName)
        {
            this.surName = surName;
        }
    
        public int getStudentID()
        {
            return studentID;
        }
    
        public void setStudentID(int studentID)
        {
            this.studentID = studentID;
        }
    
        @Override
        public String toString()
        {
            return getClass().getSimpleName()+"{" + "foreName=" + foreName + ", surName=" + surName + ", studentID=" + studentID + '}';
        }
    
        public String format()
        {
            return String.format("%-5s %-5s \t\t %-5d \n",foreName,surName,studentID);
        }
    }
    

    由于

1 个答案:

答案 0 :(得分:0)

doAddStudent方法改为:

private void doAddStudent()
    {
        String addMore;
        do
        {
            System.out.println("\nAdd New Student\n***********\n");

            try
            {
                System.out.println("Enter Students Forename :>");
                String fName = input.next();//use next() instead of nextLine()

                System.out.println("Enter Student Surname :>");
                String sName = input.next();//use next() instead of nextLine();

                System.out.println("Enter Student ID Number :>");
                int idNum = input.nextInt();

                theRegistry.addStudent(new Student(fName, sName, idNum));

            } catch (Exception e)
            {
                System.out.println("\nERROR OCCURED: Incorect Value Entered"
                        + "\nTry Again... \nStudent Was NOT added");
            }


            System.out.println("\nAdd Another Student (Y/N) : >");
            addMore = input.next();



        } while (!"n".equals(addMore));
    }

由于您使用的是Scanner.nextLine(),您收到了错误。现在问题是为什么

官方文件中描述的Scanner.nextLine()是:

  

使此扫描程序超过当前行并返回该输入   被跳过。此方法返回当前行的其余部分,   排除末尾的任何行分隔符。该职位设定为   下一行的开头。

Scanner.next()未正确终止分配的内存行。因此,在用户输入nextLine()之后String fName = input.nextLine();之后通过addMore = input.next();调用y时,它实际上终止了实际上有值的前一行 - 通过{{1}输入而不是采用新的next()值。这就是它从用户跳过条目的原因。因此,为了继续读取输入的值而不是前一个空白行(因为String返回的值没有终止),您可以使用Scanner.next()根据官方文档声明:< / p>

  

从此扫描仪中查找并返回下一个完整令牌。

更新
您还应该替换:

next()

String userDecission = input.nextLine();