部分的に機能しているレコードシステムがありますが、メニューインターフェイスに問題があります。基本的に、メニューでは次のことができるようになります。
- 生徒を追加する
- 生徒を削除する
- 学生を印刷する
- 終了する
これでメニューは正常に機能しますが、[生徒の追加]セクションで問題が発生します。彼らが学生を追加するとき、彼らは必要とします:
- 名のプロンプト
- 姓のプロンプト
- ID番号の入力を求めるプロンプト
これを行うと、別のものを追加するかどうかを尋ねられます。「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);
}
}
ありがとう