提问人:Johnn 提问时间:11/8/2023 最后编辑:AbraJohnn 更新时间:11/8/2023 访问量:26
不一致的 GUI 弹出窗口 - 弹出一些 GUI,其余预期的 GUI 不会弹出,而是在命令行中输入
Inconsistent GUI pop ups - some GUIs pop up, the rest of the expected GUIs do not pop up and inputs are taken inside the command line instead
问:
我正在尝试开发一个学生宿舍系统,只有在弹出的 GUI 方面起作用的行是......
consoleIO.outputString("-------Initializing the system-------");
但其余的弹出窗口不起作用。我已经在另一个 Java 类中实现了方法(、、 和 )并对其进行了测试,它们工作得很好。readString
readInt
readChoice
outputString
DialogueIO.java
public class ResidenceSystemA5Q2{
/**
* One Scanner for all methods
*/
private static Scanner consoleIn = new Scanner(System.in);
/**
* The keyed dictionary of all students.
*/
private Map<String, Student> students;
/**
* The keyed dictionary of all managers.
*/
private Map<String, Manager> managers;
/**
* The residence to be handled.
*/
private Residence residence;
/**
* The ConsoleIO to be handled.
*/
private DialogueIO consoleIO = new DialogueIO();
/**
* Initialize an instance of the residence management residence
* relies on user-input to get the relavent information
*/
public ResidenceSystemA5Q2() {
students = new TreeMap<String, Student>();
managers = new TreeMap<String, Manager>();
// get the residence information
consoleIO.outputString("-------Getting Residence information-------");
String name = consoleIO.readString("Enter the name of the Residence: ");
int firstBedNum = consoleIO.readInt("Enter the integer label of the first bed: ");
int lastBedNum = consoleIO.readInt("Enter the integer label of the last bed: ");
residence = new Residence(name, firstBedNum, lastBedNum);
}
/**
* Read the information for a new student and then add the student
* to the dictionary of all students.
*/
public void addStudent() {
consoleIO.outputString("-------Adding Student to Residence-------");
String name = consoleIO.readString("Enter the name of the student: ");
String sSIN = consoleIO.readString("Enter the social insurance number of the student: ");
String sNSID = consoleIO.readString("Enter the NSID of the student: ");
if (students.containsKey(sNSID)) {
throw new IllegalStateException("Student with NSID " + sNSID + " already exists");
}
Student st = new Student(name, sSIN, sNSID);
Student result = students.put(sNSID, st);
// checking to make sure the key was unique
if (result != null) {
students.put(sNSID, result); // put the original student back
throw new IllegalStateException("NSID in the student dictionary even "
+ "though containsKey failed. Student " + name + " not entered.");
}
}
/**
* Read the information for a new manager and then add the manager
* to the dictionary of all managers.
*/
public void addManager() {
consoleIO.outputString("-------Adding Manager to Residence-------");
String mName = consoleIO.readString("Enter the manager's name: ");
String mSIN = consoleIO.readString("Enter the manager's SIN: ");
String mEN = consoleIO.readString("Enter the manager's employee number: ");
if (managers.containsKey(mEN))
throw new IllegalStateException("Manager not added as there already "
+ "is a manager with the employee number " + mEN);
String response = consoleIO.readString("Is the manager a consultant? (yes or no): ");
Manager mgr;
if (response.charAt(0) == 'y' || response.charAt(0) == 'Y')
mgr = new Consultant(mName, mSIN, mEN);
else
mgr = new Manager(mName, mSIN, mEN);
// check to make sure the manager name doesn't already exist
Manager sameNumbermanager = managers.put(mEN, mgr);
if (sameNumbermanager != null) {
// if put() returns a reference, then a manager was already stored with the same EN,
// so put it back, and signal an error.
managers.put(mEN, sameNumbermanager); // put the original manager back
throw new IllegalStateException("Employee number in the manager dictionary even though "
+ "containsKey failed. Manager " + mName + " not entered.");
}
}
/**
* Assign a manager to take care of a student.
*/
public void assignManagerToStudent() {
consoleIO.outputString("-------Assigning a new Manager-Student Association-------");
String sNSID = consoleIO.readString("Enter the NSID of the student: ");
Student st = students.get(sNSID);
if (st == null)
throw new NoSuchElementException("There is no student with NSID " + sNSID);
String mEN = consoleIO.readString("Enter the employee number of the manager: ");
Manager mgr = managers.get(mEN);
if (mgr == null)
throw new NoSuchElementException("There is no manager with employee number " + mEN);
else {
st.addManager(mgr);
mgr.addStudent(st);
}
}
/**
* Assign a student to a specific bed.
*/
public void assignBed() {
consoleIO.outputString("-------Assigning a Student to a Bed-------");
String sNSID = consoleIO.readString("Enter the NSID of the student: ");
Student st = students.get(sNSID);
if (st == null)
throw new NoSuchElementException("There is no student with NSID " + sNSID);
if (st.getBedLabel() != -1)
throw new IllegalStateException(" Student " + st
+ " is already in a bed so cannot be assigned a new bed");
int bedNum = consoleIO.readInt("Enter the bed number for the student: ");
if (bedNum < residence.getMinBedLabel() || bedNum > residence.getMaxBedLabel())
throw new IllegalArgumentException("Bed label " + bedNum + " is not valid, as "
+ "the value must be between " + residence.getMinBedLabel()
+ " and " + residence.getMaxBedLabel());
if (residence.isOccupied(bedNum)) {
throw new IllegalStateException("There is already a different Student in that bed.");
} else {
st.setBedLabel(bedNum);
residence.assignStudentToBed(st, bedNum);
}
}
/**
* Drop the association between a manager and a student.
*/
public void dropAssociation() {
consoleIO.outputString("-------Dropping a new Manager-Student Association-------");
String sNSID = consoleIO.readString("Enter the NSID of the student: ");
Student st = students.get(sNSID);
if (st == null)
throw new NoSuchElementException("There is no student with NSID " + sNSID);
String mEN = consoleIO.readString("Enter the employee number of the manager: ");
// check if the manager is known
Manager mgr = managers.get(mEN);
if (mgr == null)
throw new NoSuchElementException("There is no manager with employee number " + mEN);
String sNSID2 = st.getNSID();
if(!sNSID.equals(sNSID2))
throw new IllegalStateException("NSIDs are not equal for a studnet: " + sNSID + " " + sNSID2);
// check if the manager records the student
if (!mgr.hasStudent(sNSID2))
throw new IllegalStateException("This manager is not associated with this student:" + sNSID2);
// check if the student records the manager
if (!st.hasManager(mEN))
throw new IllegalStateException("This manager and this student are incorrectly "
+ "associated. The manager has the student, "
+ "but the student does not have the manager");
// the student and manager are associated, so we remove each from the other's record
st.removeManager(mEN);
mgr.removeStudent(sNSID);
}
/**
* Displays the system state
*/
public void systemState() {
consoleIO.outputString(this.toString());
}
/**
* Display the empty beds for the residence.
*/
public void displayEmptyBeds() {
System.out.println(residence.availableBeds());
}
/**
* Release a student from their assigned bed.
*/
public void releaseStudent() {
String sNSID = consoleIO.readString("Enter Student NSID: ");
//check if NSID is stored inside Map
if (!students.containsKey(sNSID))
throw new NoSuchElementException("There is no student with that NSID assigned to a bed");
//Create student object that holds inputted NSID
Student student = students.get(sNSID);
residence.freeBed(student.getBedLabel());
//reassign/set student bed label to -1 to indicate unassigned bed
student.setBedLabel(-1);
}
/**
* Return a string representation of the ResidenceSystem
*
* @return a string representation of the ResidenceSystem
*/
public String toString() {
String result = "\nThe students in the system are:";
Collection<Student> patientsColl = students.values();
for (Student p : patientsColl)
result = result + p;
result = result + "\n-------\nThe managers in the system are:";
Collection<Manager> managersColl = managers.values();
for (Manager d : managersColl)
result = result + d;
result = result + "\n-------\nThe residence is " + residence;
return result;
}
/**
* Run the residence management system.
*
* @param args not used
*/
public static void main(String[] args) {
DialogueIO consoleIO = new DialogueIO();
int task = -1;
ResidenceSystemA5Q1 sys;
consoleIO.outputString("-------Initializing the system-------");
while (true) {
// keep trying until the user enters the data correctly
try {
sys = new ResidenceSystemA5Q1();
break;
} catch (RuntimeException e) {
consoleIO.outputString(e.getMessage());
}
}
consoleIO.outputString("-------Running the system-------");
while (task != 1) {
try {
consoleIO.outputString("Options:"
+ "\n\t1: Quit"
+ "\n\t2: Add a new student"
+ "\n\t3: Add a new manager"
+ "\n\t4: Assign a manager to a student"
+ "\n\t5: Display the empty beds of the residence"
+ "\n\t6: Assign a student a bed"
+ "\n\t7: Release a student"
+ "\n\t8: Drop manager-student association"
+ "\n\t9: Display current system state");
task = consoleIO.readInt("Enter your selection {1-9}: ");
consoleIn.nextLine(); // consume any junk at the end of the line
if (task == 1) sys.systemState();
else if (task == 2) sys.addStudent();
else if (task == 3) sys.addManager();
else if (task == 4) sys.assignManagerToStudent();
else if (task == 5) sys.displayEmptyBeds();
else if (task == 6) sys.assignBed();
else if (task == 7) sys.releaseStudent();
else if (task == 8) sys.dropAssociation();
else if (task == 9) sys.systemState();
else consoleIO.outputString("Invalid option, try again.");
}
catch (InputMismatchException e) {
// thrown by the Scanner if the user types something unexpected
consoleIO.outputString("Use an integer to choose a menu item!");
}
catch (RuntimeException e) {
// No matter what other exception is thrown, this catches it
// Dealing with it means discarding whatever went wrong
// and starting the loop over. Easy for the programmer,
// tedious for the user.
consoleIO.outputString(e.getMessage());
}
}
consoleIO.outputString("-------System terminated-------");
}
每行都应该有一个 GUI 弹出窗口,但只有一个弹出,其余的都是从命令行中获取的。consoleIO
答: 暂无答案
上一个:调用数组中对象的属性
下一个:我怎样才能停止太空中的物体?
评论
DialogueIO
DialogueIO
DialogueIO