/**
* This is the solution for Week 4, 4.3 Rolling Project Part 3
* The Enrolment class stores student and unit enrollment details.
* It provides methods to accept user input and display enrollment information.
*
* @author Jimmy
* @version 1.2
*/
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 student information from the user.
*/
public void inputStudentDetails()
{
Input input = new Input();
Validation validation = new Validation();
String name;
do
{
name = input.acceptStringInput("Enter student name: ");
if (validation.isBlank(name) || !validation.stringLengthInRange(name, 3, 12))
{
System.out.println("Invalid student name. Name must be 3-12 characters and not blank.");
}
}
while (validation.isBlank(name) || !validation.stringLengthInRange(name, 3, 12));
String address;
do
{
address = input.acceptStringInput("Enter address: ");
if (validation.isBlank(address) || !validation.stringLengthInRange(address, 25, 100))
{
System.out.println("Invalid address. Address must be 25-100 characters and not blank.");
}
}
while (validation.isBlank(address) || !validation.stringLengthInRange(address, 25, 100));
String phone;
do
{
phone = input.acceptStringInput("Enter phone number: ");
if (validation.isBlank(phone) || !validation.stringLengthInRange(phone, 10, 10))
{
System.out.println("Invalid phone number. Phone number must be 10 characters and not blank.");
}
}
while (validation.isBlank(phone) || !validation.stringLengthInRange(phone, 10, 10));
String email;
do
{
email = input.acceptStringInput("Enter email: ");
if (!validation.isValidEmail(email))
{
System.out.println("Invalid email. Email must be include '@' and '.' and not blank.");
}
}
while (!validation.isValidEmail(email));
this.student = new Student(name, address, phone, email);
this.student.display();
}
/**
* Accept all the required unit information from the user.
*/
public void inputUnitDetails()
{
Input input = new Input();
Validation validation = new Validation();
String unitCode;
do
{
unitCode = input.acceptStringInput("Enter unit code: ");
if (validation.isBlank(unitCode) || !validation.stringLengthInRange(unitCode, 7, 7))
{
System.out.println("Invalid unitCode. Unit code must be 7 characters and not blank.");
}
}
while (validation.isBlank(unitCode) || !validation.stringLengthInRange(unitCode, 7, 7));
String unitDesc;
do
{
unitDesc = input.acceptStringInput("Enter description: ");
if (validation.isBlank(unitDesc) || !validation.stringLengthInRange(unitDesc, 1, 250))
{
System.out.println("Invalid description. Description must be more than 250 characters and not blank.");
}
}
while (validation.isBlank(unitDesc) || !validation.stringLengthInRange(unitDesc, 1, 250));
int creditPoints = input.acceptIntegerInput("Enter credit points: ");
this.units = new Unit(unitCode, unitDesc, creditPoints);
this.units.display();
}
/**
* 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.startProgram();
}
/**
* 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;
}
/**
* startProgram method to run the enrolment program.
*
*/
public void startProgram()
{
Input input = new Input();
boolean running = true;
while (running)
{
System.out.println("Welcome to Enrolment System!.");
System.out.println("1. Enter student details");
System.out.println("2. Enter unit details");
System.out.println("3. Exit");
int choice = input.acceptIntegerInput("Enter choice: ");
switch (choice)
{
case 1:
inputStudentDetails();
break;
case 2:
inputUnitDetails();
break;
case 3:
running = false;
System.out.println("Exiting program...");
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice. Please choose 1, 2 or 3.");
}
}
}
/**
*
* @return Enrolment information.
*/
@Override
public String toString()
{
return "=========================\n" +
" Enrolment Details \n" +
"=========================\n" +
"Date : " + date + "\n" +
"Student : " + (student != null ? student.getName() : "None") + "\n" +
"Units : " + (units != null ? units.toString() : "None") + "\n" +
"=========================";
}
}/**
* 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.2
*/
import java.util.Scanner;
public class Input
{
// Constructors
/**
* Default constructor.
*/
public Input()
{
}
// 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);
Validation validation = new Validation();
while (!validation.isInteger(input))
{
System.out.println("Invalid integer input, try again.");
input = acceptStringInput(msg);
}
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)
{
Scanner scanner = new Scanner(System.in);
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.2
*/
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.2
*/
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" +
"=========================";
}
}
/**
* The Validation class provides reusable validation methods for user input.
* This helps ensure input integrity and prevents unexpected behaviors.
*
* @author Jimmy
* @version 1.0
*/
public class Validation
{
// Default constructor
public Validation()
{
}
// methods
/**
* Check if the input string is blank (empty or only whitespace).
* @param input the input string to be checked.
* @return true if the input is null or consists solely of whitespace; false otherwise.
*/
public boolean isBlank(String input)
{
return input == null || input.trim().isEmpty();
}
/**
* Check if the input string is a valid integer.
* This method checks if the input can be parsed as an integer.
* @param input the input string
* @return true if input is a valid integer, false otherwise.
*/
public boolean isInteger(String input)
{
if (input == null)
{
return false;
}
input = input.trim();
if (input.isEmpty())
{
return false;
}
int start = 0;
char firstChar = input.charAt(0);
if (firstChar == '-' || firstChar == '+')
{
if (input.length() == 1)
{
return false;
}
start = 1;
}
for (int i = start; i < input.length(); i++)
{
if (!Character.isDigit(input.charAt(i)))
{
return false;
}
}
return true;
}
/**
* Check if the input string is a valid email address.
* This simple validation checks that the input is not blank, contains an '@' symbol,
* and contains at least one dot ('.') after the '@'.
* @param input the input string representing an email
* @return true if input appears to be a valid email address, false otherwise.
*/
public boolean isValidEmail(String input)
{
if (isBlank(input))
{
return false;
}
int atIndex = input.indexOf("@");
int dotIndex = input.lastIndexOf(".");
return atIndex > 0 && dotIndex > atIndex;
}
/**
* Checks if the length of the input string is within the specified range (inclusive).
*
* @param input the input string to be validated.
* @param min the minimum allowed length.
* @param max the maximum allowed length.
* @return true if the input is not null and its length is between min and max (inclusive); false otherwise.
*/
public boolean stringLengthInRange(String input, int min, int max)
{
return input != null && input.length() >= min && input.length() <= max;
}
}课程目录