import java.util.Arrays;
/**
* 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, and units.
*/
public Enrolment()
{
this.date = "";
this.student = null;
this.units = new Unit[4];
}
/**
* Constructs an Enrolment object with the given date, student, and units.
*
* @param date The enrolment date.
* @param student The enrolled student.
* @param units The enrolled units.
*/
public Enrolment(String date, Student student, Unit[] units)
{
this.date = date;
this.student = student;
this.units = units;
}
// Methods (alphabetically sorted)
/**
* 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)
{
for (int i = 0; i < units.length; i++)
{
if (units[i] != null)
{
System.out.println("Unit " + (i + 1) + ":");
units[i].display();
}
else
{
System.out.println("No units enrolled.");
}
}
}
else
{
System.out.println("No unit assigned.");
}
}
/**
* Returns the enrolment date.
*
* @return the current enrolment date.
*/
public String getDate()
{
return date;
}
/**
* Returns a specific unit based on index.
*
* @param index the index of the unit.
* @return the unit at the specified index, or null if index is invalid.
*/
public Unit getSpecificUnit(int index)
{
if (index >= 0 && index < units.length)
{
return units[index];
}
else
{
System.out.println("Invalid index. Must be between 0 and " + (units.length - 1) + ".");
return null;
}
}
/**
* Returns the enrolled student.
*
* @return the student.
*/
public Student getStudent()
{
return student;
}
/**
* Returns the array of enrolled units.
*
* @return the units.
*/
public Unit[] getUnits()
{
return units;
}
/**
* Accepts 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 include '@' and '.' and not be blank.");
}
}
while (!validation.isValidEmail(email));
this.student = new Student(name, address, phone, email);
this.student.display();
}
/**
* Accepts all the required unit information from the user.
*/
public void inputUnitDetails()
{
Input input = new Input();
Validation validation = new Validation();
for (int i = 0; i < units.length; i++)
{
System.out.println("Entering details for unit " + (i + 1) + ":");
String unitCode;
do
{
unitCode = input.acceptStringInput("Enter unit code: ");
if (validation.isBlank(unitCode) || !validation.stringLengthInRange(unitCode, 7, 7))
{
System.out.println("Invalid unit code. 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 1-250 characters and not blank.");
}
}
while (validation.isBlank(unitDesc) || !validation.stringLengthInRange(unitDesc, 1, 250));
int creditPoints = input.acceptIntegerInput("Enter credit points: ");
setSpecificUnit(i, unitCode, unitDesc, creditPoints);
}
}
/**
* 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();
}
/**
* Sets the enrolment date.
*
* @param date the enrolment date to set.
*/
public void setDate(String date)
{
this.date = date;
}
/**
* Sets a specific unit with the given details.
*
* @param index the index of the unit to set.
* @param unitCode the unit code.
* @param unitDesc the unit description.
* @param creditPoints the credit points.
*/
public void setSpecificUnit(int index, String unitCode, String unitDesc, int creditPoints)
{
if (index >= 0 && index < units.length)
{
units[index] = new Unit(unitCode, unitDesc, creditPoints);
}
else
{
System.out.println("Invalid index. Must be between 0 and " + (units.length - 1) + ".");
}
}
/**
* Sets the enrolled student.
*
* @param student the student to set.
*/
public void setStudent(Student student)
{
this.student = student;
}
/**
* Sets the enrolled units.
*
* @param units the units to set.
*/
public void setUnits(Unit[] units)
{
this.units = units;
}
/**
* Starts 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. Display enrolment details");
System.out.println("4. Exit");
int choice = input.acceptIntegerInput("Enter choice: ");
switch (choice)
{
case 1:
inputStudentDetails();
break;
case 2:
inputUnitDetails();
break;
case 3:
display();
break;
case 4:
running = false;
System.out.println("Exiting program...");
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice. Please choose 1, 2 or 3.");
}
}
}
/**
* Returns a string representation of the enrolment information.
*
* @return enrolment information as a string.
*/
@Override
public String toString()
{
return "=========================\n" +
" Enrolment Details \n" +
"=========================\n" +
"Date : " + date + "\n" +
"Student : " + (student != null ? student.getName() : "None") + "\n" +
"Units : " + (units != null ? Arrays.toString(units) : "None") + "\n" +
"=========================";
}
}import RollingProjectV3.Validation;
import java.util.Scanner;
/**
* 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
*/
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" +
"=========================";
}
}
import java.util.ArrayList;
/**
* University class that manages student enrolments
*/
public class University
{
// Field declaration
private ArrayList<String> enrolments;
/**
* Default constructor
*/
public University()
{
enrolments = new ArrayList<String>();
}
/**
* Non-default constructor
*
* @param enrolments ArrayList of enrolments
*/
public University(ArrayList<String> enrolments)
{
this.enrolments = enrolments;
}
/**
* Display method to print all enrolments
*/
public void display()
{
System.out.println("University Enrolments:");
for (int i = 0; i < enrolments.size(); i++)
{
System.out.println((i + 1) + ". " + enrolments.get(i));
}
}
/**
* Get the entire enrolments ArrayList
*
* @return ArrayList containing all enrolments
*/
public ArrayList<String> getEnrolments()
{
return enrolments;
}
/**
* Set the entire enrolments ArrayList
*
* @param enrolments New ArrayList of enrolments
*/
public void setEnrolments(ArrayList<String> enrolments) {
this.enrolments = enrolments;
}
/**
* Get the number of enrolments
*
* @return Size of the enrolments ArrayList
*/
public int getEnrolmentSize()
{
return enrolments.size();
}
/**
* Add a new enrolment to the ArrayList
*
* @param enrolment The enrolment to add
*/
public void addEnrolment(String enrolment)
{
enrolments.add(enrolment);
}
/**
* Remove an enrolment from the ArrayList
*
* @param enrolment The enrolment to remove
* @return true if the enrolment was removed, false otherwise
*/
public boolean removeEnrolment(String enrolment)
{
return enrolments.remove(enrolment);
}
/**
* Get a specific enrolment from the ArrayList
*
* @param index The index of the enrolment to get
* @return The enrolment at the specified index
*/
public String getSpecificEnrolment(int index)
{
if (index >= 0 && index < enrolments.size())
{
return enrolments.get(index);
}
else
{
System.out.println("Index out of bounds");
return null;
}
}
/**
* Set a specific enrolment in the ArrayList
*
* @param index The index of the enrolment to set
* @param enrolment The new enrolment value
*/
public void setSpecificEnrolment(int index, String enrolment)
{
if (index >= 0 && index < enrolments.size())
{
enrolments.set(index, enrolment);
}
else
{
System.out.println("Index out of bounds");
}
}
}/**
* The Validation class provides reusable validation methods for user input.
* This helps ensure input integrity and prevents unexpected behaviors.
*
* @author Jimmy
* @version 1.2
*/
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;
}
}课程目录