- #1
ver_mathstats
- 260
- 21
- Homework Statement
- Make the required changes to the code so that it reads the input from the terminal. Your code should be able to handle an arbitrary but valid input (for example if we replace the order of the first two lines of the above example we still have a valid input). But if we have repetitive course names, or a typo in the command, like COURRSE, then the input is not valid and we don't check your code for such cases.
- Relevant Equations
- Java
Right now I am learning coding on Java using IntelliJ IDEA. I have coded before on C++ and then Python, so I understand a little on Java. I'm not really sure how exactly I would change code so that it would read input from the terminal? Would I have to use a Scanner for this? I can post the actual code I have, although there are four more parts to the code but I assume this is the window where we change the code:
Any help would be appreciated, thank you.
Java:
import java.util.ArrayList;
public class Registrar {
ArrayList<Student> students;
ArrayList<Course> courses;
public Registrar() {
students = new ArrayList<>();
courses = new ArrayList<>();
}
/* Hardcoded example. Instead, you need to implement new functions that read
the input from the terminal and print the corresponding outputs. */
public void runExampleCommands() {
Course course1 = new Course("CS2XYZ");
Course course2 = new Course("CS1ABC");
Student student1 = new Student("EMMA", 23345);
Student student2 = new Student("DAVID", 123345);
course1.addSection(new Section("C01", 10));
course2.addSection(new Section("C02", 1));
boolean enrollmentResult = course1.enrollStudent(student1, "C01");
TerminalPrinter.printEnrollmentResult(enrollmentResult, student1.getName(),
course1.getUniqueName(), "C01");
enrollmentResult = course1.enrollStudent(student2, "C01");
TerminalPrinter.printEnrollmentResult(enrollmentResult, student2.getName(),
course1.getUniqueName(), "C01");
enrollmentResult = course2.enrollStudent(student1, "C02");
TerminalPrinter.printEnrollmentResult(enrollmentResult, student1.getName(),
course2.getUniqueName(), "C02");
enrollmentResult = course2.enrollStudent(student2, "C02");
TerminalPrinter.printEnrollmentResult(enrollmentResult, student2.getName(),
course2.getUniqueName(), "C02");
enrollmentResult = course2.unenrollStudent(student1, "C02");
TerminalPrinter.printUnenrollmentResult(enrollmentResult, student1.getName(),
course2.getUniqueName(), "C02");
enrollmentResult = course2.unenrollStudent(student2, "C02");
TerminalPrinter.printUnenrollmentResult(enrollmentResult, student2.getName(),
course2.getUniqueName(), "C02");
enrollmentResult = course2.enrollStudent(student2, "C02");
TerminalPrinter.printEnrollmentResult(enrollmentResult, student2.getName(),
course2.getUniqueName(), "C02");
}
}
Any help would be appreciated, thank you.