/**
* This is the solution for Week 3, 3.7 Rolling Project Part 2
* The Enrolment class stores student and unit enrollment details.
* It provides methods to accept user input and display enrollment information.
*
* @author Jimmy
* @version 1.1
*/
public class Enrolment {
// Fields
private String date;
private Student student;
private Unit units;
// Constructors
/**
* Default constructor that initializes date, student, units
*/
public Enrolment() {
this.date = "";
this.student = null;
this.units = null;
}
/**
* Constructs an Enrolment object with the given date, student, and unit.
*
* @param date The enrolment date.
* @param student The enrolled student.
* @param units The enrolled unit.
*/
public Enrolment(String date, Student student, Unit units) {
this.date = date;
this.student = student;
this.units = units;
}
// Methods
/**
* Displays enrolment details.
*/
public void display() {
System.out.println("Date: " + date);
if (student != null) {
student.display();
} else {
System.out.println("No student enrolled.");
}
if (units != null) {
units.display();
} else {
System.out.println("No unit assigned.");
}
}
/**
* Getter for date.
*
* @return The current enrolment date.
*/
public String getDate() {
return date;
}
/**
* Getter for student.
*
* @return The enrolled student.
*/
public Student getStudent() {
return student;
}
/**
* Getter for units.
*
* @return The enrolled unit.
*/
public Unit getUnits() {
return units;
}
/**
* Accept all the required unit information from the user.
*/
public void inputUnitDetails() {
Input input = new Input();
String uniCode = input.acceptStringInput("Enter unique code: ");
String unitDesc = input.acceptStringInput("Enter description: ");
int creditPoints = input.acceptIntegerInput("Enter credit points: ");
this.units = new Unit(uniCode, unitDesc, creditPoints);
}
/**
* Accept all the required student information from the user.
*/
public void inputStudentDetails() {
Input input = new Input();
String name = input.acceptStringInput("Enter student name: ");
String address = input.acceptStringInput("Enter address: ");
String phone = input.acceptStringInput("Enter phone number: ");
String email = input.acceptStringInput("Enter email: ");
this.student = new Student(name, address, email, phone);
}
/**
* Main method to run the enrolment program.
*
* @param args Command line arguments (not used).
*/
public static void main(String[] args) {
Enrolment enrolment = new Enrolment();
enrolment.inputUnitDetails();
enrolment.inputStudentDetails();
enrolment.display();
}
/**
* Setter for date.
*
* @param date The enrolment date to set.
*/
public void setDate(String date) {
this.date = date;
}
/**
* Setter for student.
*
* @param student The student to set.
*/
public void setStudent(Student student) {
this.student = student;
}
/**
* Setter for units.
*
* @param units The unit to set.
*/
public void setUnits(Unit units) {
this.units = units;
}
/**
*
* @return Enrolment information.
*/
@Override
public String toString() {
return "Enrolment [date = " + date +
", student = " + (student != null ? student.toString() : "None") +
", units = " + (units != null ? units.toString() : "None") + "]";
}
}/**
* This class provides a simple input utility for accepting
* string, integer, double, and character input from the user.
* It is used across multiple components of the Rolling Project.
*
* @author Jimmy
* @version 1.1
*/
import java.util.Scanner;
public class Input {
// Field
private Scanner scanner;
// Constructors
/**
* Default constructor.
*/
public Input() {
scanner = new Scanner(System.in);
}
// Methods
/**
* Accepts and returns a single character input from the user.
*
* @param msg The prompt message.
* @return The first character of the input, or '\0' if empty.
*/
public char acceptCharInput(String msg) {
String input = acceptStringInput(msg);
return (input != null && !input.isEmpty()) ? input.charAt(0) : '\0';
}
/**
* Accepts and returns a double input from the user.
*
* @param msg The prompt message.
* @return The parsed double value.
*/
public double acceptDoubleInput(String msg) {
String input = acceptStringInput(msg);
return Double.parseDouble(input);
}
/**
* Accepts and returns an integer input from the user.
*
* @param msg The prompt message.
* @return The parsed integer value.
*/
public int acceptIntegerInput(String msg) {
String input = acceptStringInput(msg);
// do validation
int number = Integer.parseInt(input);
return number;
}
/**
* Accepts and returns a string input from the user.
*
* @param msg The prompt message.
* @return The trimmed input string.
*/
public String acceptStringInput(String msg) {
System.out.print(msg);
String input = scanner.nextLine().trim();
return input;
}
}
/**
* The Student class represents a student with basic contact information,
* including name, address, phone number, and email. It provides constructors,
* accessors, mutators, a display method, and a string representation.
*
* @author Jimmy
* @version 1.1
*/
public class Student {
// Fields
private String name;
private String address;
private String phoneNo;
private String email;
// Constructors
/**
* No-argument constructor that initializes all fields to "unknown".
*/
public Student() {
this.name = "unknown";
this.address = "unknown";
this.phoneNo = "unknown";
this.email = "unknown";
}
/**
* Parameterized constructor to initialize all fields with user-provided values.
*
* @param name The name of the student.
* @param address The address of the student.
* @param phoneNo The phone number of the student.
* @param email The email address of the student.
*/
public Student(String name, String address, String phoneNo, String email) {
this.name = name;
this.address = address;
this.phoneNo = phoneNo;
this.email = email;
}
// Methods
/**
* Displays the student information by printing the string representation.
*/
public void display() {
System.out.println(this);
}
/**
* Returns the address of the student.
*
* @return The address.
*/
public String getAddress() {
return address;
}
/**
* Returns the email address of the student.
*
* @return The email address.
*/
public String getEmail() {
return email;
}
/**
* Returns the name of the student.
*
* @return The name.
*/
public String getName() {
return name;
}
/**
* Returns the phone number of the student.
*
* @return The phone number.
*/
public String getPhoneNo() {
return phoneNo;
}
/**
* Sets the address of the student.
*
* @param address The new address.
*/
public void setAddress(String address) {
this.address = address;
}
/**
* Sets the email address of the student.
*
* @param email The new email address.
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Sets the name of the student.
*
* @param name The new name.
*/
public void setName(String name) {
this.name = name;
}
/**
* Sets the phone number of the student.
*
* @param phoneNo The new phone number.
*/
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
/**
* Returns a string representation of the student object.
*
* @return A string containing student details.
*/
@Override
public String toString() {
return "=========================\n" +
" Student Info \n" +
"=========================\n" +
"Name : " + name + "\n" +
"Address : " + address + "\n" +
"Phone No : " + phoneNo + "\n" +
"Email : " + email + "\n" +
"=========================";
}
}
/**
* The Unit class represents an academic unit offered by the university.
* Each unit has a code, description, and number of credit points.
*
* @author Jimmy
* @version 1.1
*/
public class Unit {
// Fields
private String unitCode;
private String unitDescription;
private int creditPoints;
// Constructors
/**
* No-argument constructor that initializes all fields to default values.
*/
public Unit() {
this.unitCode = "unknown";
this.unitDescription = "unknown";
this.creditPoints = 0;
}
/**
* Parameterized constructor to initialize all fields.
*
* @param unitCode The code of the unit.
* @param unitDescription The description of the unit.
* @param creditPoints The number of credit points for the unit.
*/
public Unit(String unitCode, String unitDescription, int creditPoints) {
this.unitCode = unitCode;
this.unitDescription = unitDescription;
this.creditPoints = creditPoints;
}
// Methods
/**
* Displays the unit's information by printing its string representation.
*/
public void display() {
System.out.println(this);
}
/**
* Returns the number of credit points for the unit.
*
* @return The credit points.
*/
public int getCreditPoints() {
return creditPoints;
}
/**
* Returns the code of the unit.
*
* @return The unit code.
*/
public String getUnitCode() {
return unitCode;
}
/**
* Returns the description of the unit.
*
* @return The unit description.
*/
public String getUnitDescription() {
return unitDescription;
}
/**
* Sets the number of credit points for the unit.
*
* @param creditPoints The new number of credit points.
*/
public void setCreditPoints(int creditPoints) {
this.creditPoints = creditPoints;
}
/**
* Sets the code of the unit.
*
* @param unitCode The new unit code.
*/
public void setUnitCode(String unitCode) {
this.unitCode = unitCode;
}
/**
* Sets the description of the unit.
*
* @param unitDescription The new unit description.
*/
public void setUnitDescription(String unitDescription) {
this.unitDescription = unitDescription;
}
/**
* Returns a string representation of the unit.
*
* @return A string with unit details.
*/
@Override
public String toString() {
return "=========================\n" +
" Unit Info \n" +
"=========================\n" +
"Unit Code : " + unitCode + "\n" +
"Description : " + unitDescription + "\n" +
"Credit Points : " + creditPoints + "\n" +
"=========================";
}
}
课程目录