Java code about Enrollment System Please help. So this is the code: import java.util.Scanner; class User { String username; String password; String role; public User(String username, String password, String role) { this.username = username; this.password = password; this.role = role; } public String getUsername() { return username; } public String getPassword() { return password; } public String getRole() { return role; } } class Course { String courseCode; String courseTitle; int units; public Course(String courseCode, String courseTitle, int units) { this.courseCode = courseCode; this.courseTitle = courseTitle; this.units = units; } public String getCourseCode() { return courseCode; } public String getCourseTitle() { return courseTitle; } public int getUnits() { return units; } } class EnrollmentSystem { User[] users = new User[10]; int userCount = 0; Course[] courses = new Course[10]; int courseCount = 0; Scanner sc = new Scanner(System.in); public void registerAccount() { System.out.print("Enter username: "); String username = sc.nextLine(); System.out.print("Enter password: "); String password = sc.nextLine(); System.out.print("Enter role (student/admin): "); String role = sc.nextLine(); users[userCount++] = new User(username, password, role); System.out.println("Account registered successfully!"); } public boolean login(String username, String password) { for (int i = 0; i < userCount; i++) { if (users[i].getUsername().equals(username) && users[i].getPassword().equals(password)) { return true; } } return false; } public void addCourse() { System.out.print("Enter course code: "); String courseCode = sc.nextLine(); System.out.print("Enter course title: "); String courseTitle = sc.nextLine(); System.out.print("Enter units: "); int units = sc.nextInt(); courses[courseCount++] = new Course(courseCode, courseTitle, units); System.out.println("Course added successfully!"); } public void viewCourses() { System.out.println("List of Courses: "); for (int i = 0; i < courseCount; i++) { System.out.println(courses[i].getCourseCode() + " - " + courses[i].getCourseTitle() + " (" + courses[i].getUnits() + " units)"); } } public void enroll() { System.out.print("Enter course code: "); String courseCode = sc.nextLine(); int courseIndex = -1; for (int i = 0; i < courseCount; i++) { if (courses[i].getCourseCode().equals(courseCode)) { courseIndex = i; break; } } if (courseIndex == -1) { System.out.println("Course not found."); return; } System.out.println("You have enrolled in: " + courses[courseIndex].getCourseTitle()); } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); EnrollmentSystem es = new EnrollmentSystem(); while (true) { System.out.println("\nWelcome to the Enrollment System"); System.out.println("[1] Register Account"); System.out.println("[2] Login"); System.out.println("[3] Exit"); System.out.print("Enter choice: "); int choice = sc.nextInt(); if (choice == 1) { es.registerAccount(); } else if (choice == 2) { System.out.print("Enter username: "); String username = sc.next(); System.out.print("Enter password: "); String password = sc.next(); boolean isValid = es.login(username, password); if (!isValid) { System.out.println("Invalid username or password."); } else { User user = null; for (int i = 0; i < es.userCount; i++) { if (es.users[i].getUsername().equals(username) && es.users[i].getPassword().equals(password)) { user = es.users[i]; break; } } if (user.getRole().equals("admin")) { while (true) { System.out.println("\nWelcome, Admin"); System.out.println("[1] Add Course"); System.out.println("[2] View Courses"); System.out.println("[3] Logout"); System.out.print("Enter choice: "); int adminChoice = sc.nextInt(); if (adminChoice == 1) { es.addCourse(); } else if (adminChoice == 2) { es.viewCourses(); } else if (adminChoice == 3) { break; } } } else if (user.getRole().equals("student")) { while (true) { System.out.println("\nWelcome, Student"); System.out.println("[1] Enroll"); System.out.println("[2] View Courses"); System.out.println("[3] Logout"); System.out.print("Enter choice: "); int studentChoice = sc.nextInt(); if (studentChoice == 1) { es.enroll(); } else if (studentChoice == 2) { es.viewCourses(); } else if (studentChoice == 3) { break; } } } } } else if (choice == 3) { System.out.println("Goodbye!"); break; } } } } The problem here is: When I add a 2nd course as an admin, the "Enter course code:" and the "Enter course title:" began to join as one line, and that goes on and on. Please, kindly edit and fix it. Another problem here is: When I'm about to enroll in a course as a student, the "Enter course code:" and the "You have enrolled in:" joined as one line and I'm not yet inputting something that I enrolled in a certain course which "csit2 programming2" appeared. Please, kindly edit and fix it. this is just a school activity. I just need a fixed code(no explanation needed) with the problems I encounter with my code nothing else is required.
Java code about Enrollment System
Please help.
So this is the code:
import java.util.Scanner;
class User {
String username;
String password;
String role;
public User(String username, String password, String role) {
this.username = username;
this.password = password;
this.role = role;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getRole() {
return role;
}
}
class Course {
String courseCode;
String courseTitle;
int units;
public Course(String courseCode, String courseTitle, int units) {
this.courseCode = courseCode;
this.courseTitle = courseTitle;
this.units = units;
}
public String getCourseCode() {
return courseCode;
}
public String getCourseTitle() {
return courseTitle;
}
public int getUnits() {
return units;
}
}
class EnrollmentSystem {
User[] users = new User[10];
int userCount = 0;
Course[] courses = new Course[10];
int courseCount = 0;
Scanner sc = new Scanner(System.in);
public void registerAccount() {
System.out.print("Enter username: ");
String username = sc.nextLine();
System.out.print("Enter password: ");
String password = sc.nextLine();
System.out.print("Enter role (student/admin): ");
String role = sc.nextLine();
users[userCount++] = new User(username, password, role);
System.out.println("Account registered successfully!");
}
public boolean login(String username, String password) {
for (int i = 0; i < userCount; i++) {
if (users[i].getUsername().equals(username) && users[i].getPassword().equals(password)) {
return true;
}
}
return false;
}
public void addCourse() {
System.out.print("Enter course code: ");
String courseCode = sc.nextLine();
System.out.print("Enter course title: ");
String courseTitle = sc.nextLine();
System.out.print("Enter units: ");
int units = sc.nextInt();
courses[courseCount++] = new Course(courseCode, courseTitle, units);
System.out.println("Course added successfully!");
}
public void viewCourses() {
System.out.println("List of Courses: ");
for (int i = 0; i < courseCount; i++) {
System.out.println(courses[i].getCourseCode() + " - " + courses[i].getCourseTitle() + " (" + courses[i].getUnits() + " units)");
}
}
public void enroll() {
System.out.print("Enter course code: ");
String courseCode = sc.nextLine();
int courseIndex = -1;
for (int i = 0; i < courseCount; i++) {
if (courses[i].getCourseCode().equals(courseCode)) {
courseIndex = i;
break;
}
}
if (courseIndex == -1) {
System.out.println("Course not found.");
return;
}
System.out.println("You have enrolled in: " + courses[courseIndex].getCourseTitle());
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
EnrollmentSystem es = new EnrollmentSystem();
while (true) {
System.out.println("\nWelcome to the Enrollment System");
System.out.println("[1] Register Account");
System.out.println("[2] Login");
System.out.println("[3] Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
if (choice == 1) {
es.registerAccount();
} else if (choice == 2) {
System.out.print("Enter username: ");
String username = sc.next();
System.out.print("Enter password: ");
String password = sc.next();
boolean isValid = es.login(username, password);
if (!isValid) {
System.out.println("Invalid username or password.");
} else {
User user = null;
for (int i = 0; i < es.userCount; i++) {
if (es.users[i].getUsername().equals(username) && es.users[i].getPassword().equals(password)) {
user = es.users[i];
break;
}
}
if (user.getRole().equals("admin")) {
while (true) {
System.out.println("\nWelcome, Admin");
System.out.println("[1] Add Course");
System.out.println("[2] View Courses");
System.out.println("[3] Logout");
System.out.print("Enter choice: ");
int adminChoice = sc.nextInt();
if (adminChoice == 1) {
es.addCourse();
} else if (adminChoice == 2) {
es.viewCourses();
} else if (adminChoice == 3) {
break;
}
}
} else if (user.getRole().equals("student")) {
while (true) {
System.out.println("\nWelcome, Student");
System.out.println("[1] Enroll");
System.out.println("[2] View Courses");
System.out.println("[3] Logout");
System.out.print("Enter choice: ");
int studentChoice = sc.nextInt();
if (studentChoice == 1) {
es.enroll();
} else if (studentChoice == 2) {
es.viewCourses();
} else if (studentChoice == 3)
{
break;
}
}
}
}
} else if (choice == 3) {
System.out.println("Goodbye!");
break;
}
}
}
}
The problem here is: When I add a 2nd course as an admin, the "Enter course code:" and the "Enter course title:" began to join as one line, and that goes on and on. Please, kindly edit and fix it.
Another problem here is: When I'm about to enroll in a course as a student, the "Enter course code:" and the "You have enrolled in:" joined as one line and I'm not yet inputting something that I enrolled in a certain course which "csit2 programming2" appeared. Please, kindly edit and fix it.
this is just a school activity. I just need a fixed code(no explanation needed) with the problems I encounter with my code nothing else is required.
Step by step
Solved in 4 steps with 9 images