As of right now I'm a beginner with programming and Java and I've only had a few months of practice so far. This is a project I've been working on as practice. It's essentially a basic course manager that a college professor or someone similar could use.
I'd like to get some feedback on any aspect of this program. Things I did wrong, bad habits, or things I could improve, shorten, simplify etc.
package courseManager;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CourseManagment {
public static ArrayList<Course> courses = new ArrayList<>();
public static int getIndex(String viewID) {
for(int n = 0 ; n < courses.size() ; n++) {
if(courses.get(n).getID().equals(viewID)) {
return n;
}
}
return -1;
}
public static int checkYesNo() {
Scanner input = new Scanner(System.in);
String action = "";
while(!action.equalsIgnoreCase("n") || (!action.equalsIgnoreCase("y"))) {
action = input.nextLine();
if(action.equalsIgnoreCase("y")) {
return 1;
}
if(action.equalsIgnoreCase("n")) {
return 2;
}
if(!action.equalsIgnoreCase("y") || !action.equalsIgnoreCase("n")) {
System.out.println("Not a valid action. Enter Y or N. ");
}
}
return 0;
}
public static void main(String[]args) {
// Variables
int refIndex = 0;
String name = "";
String id = "";
int numStudents = 0;
ArrayList<String> roster = new ArrayList<>();
String meetDay = "";
String meetTime = "";
String action = "";
String task = "";
String viewID = "";
String modify = "";
String removeStudent = "";
String addStudent = "";
Scanner input = new Scanner(System.in);
// Main code
while(!task.equals("end")) {
System.out.println("What would you like to do? Add or remove a course, modify an existing course, or view a course? "
+ "Type 'add', 'remove', 'modify', or 'view'. Type 'end' to stop the program. ");
task = input.nextLine();
if(task.equals("end")) {
break;
}
// Create course block
if(task.equals("add")) {
while(!action.equals("done")) {
System.out.println("Enter the name of the course. Type 'done' when you're finished adding. ");
courses.add(new Course(name, id, numStudents, roster, meetDay, meetTime));
System.out.println("Course added. Add another course? Y/N ");
if(checkYesNo() == 2) {
break;
}
}
}
// Remove course block
if(task.equals("remove")) {
while(!action.equals("done")) {
System.out.println("Enter the name of the course you wish to remove. Type 'done' when you're finished removing. ");
viewID = input.nextLine();
if(getIndex(viewID) == -1) {
System.out.println("No course with this ID exists. Enter a valid course ID. \n");
break;
}
if(getIndex(viewID) != -1) {
courses.remove(getIndex(viewID));
System.out.println("Course removed. Remove another course? Y/N ");
if(checkYesNo() == 2) {
break;
}
}
}
}
// Modify block
if(task.equals("modify")) {
System.out.println("Enter the ID of the course you wish to modify. ");
viewID = input.nextLine();
if(getIndex(viewID) == -1) {
System.out.println("Not a valid course ID. ");
}
if(getIndex(viewID) != -1) {
refIndex = getIndex(viewID);
while(!modify.equals("done")) {
System.out.println("What parameter of the course do you wish to modify? Type 'name', 'ID', '#students', 'meet day', or 'meet time'. "
+ "If you wish to add or remove students type 'add student' or 'remove student'. ");
modify = input.nextLine();
if(modify.equals("back")) {
break;
}
if(modify.equals("name")) {
courses.get(refIndex).ChangeName();
}
if(modify.equals("ID")) {
courses.get(refIndex).ChangeID();
}
if(modify.equals("#students")) {
courses.get(refIndex).ChangeNumStudents();
}
if(modify.equals("meet day")) {
courses.get(refIndex).ChangeMeetDay();
}
if(modify.equals("meet time")) {
courses.get(refIndex).ChangeMeetTime();
}
if(modify.equals("add student")) {
while(!addStudent.equals("done")) {
System.out.println("Enter the name of the student you wish to add. Type 'done' when you're finished adding. ");
addStudent = input.nextLine();
if(addStudent.equals("done")) {
break;
}
courses.get(refIndex).AddStudent(addStudent);
}
}
if(modify.equals("remove student")) {
while(!removeStudent.equals("done")) {
System.out.println("Enter the name of the student you wish to remove. Type 'done' when you're finished removing. ");
removeStudent = input.nextLine();
if(removeStudent.equals("done")) {
break;
}
courses.get(refIndex).RemoveStudent(removeStudent);
}
}
}
}
}
// View block
if(task.equals("view")) {
System.out.println("Enter the ID of the course you want to view. ");
viewID = input.nextLine();
if(getIndex(viewID) == -1) {
System.out.println("No course with this ID exists. Enter a different ID. \n");
}
if(getIndex(viewID) != -1) {
System.out.println(courses.get(getIndex(viewID)));
}
}
}
input.close();
}
}
class Course {
// toString override for the Course class
@Override
public String toString() {
return "\n Course name: " + name + "\n Course ID: " + id + "\n Current # of students: " + roster.size() + "/" + numStudents + "\n Student roster: " + roster
+ "\n Meeting day: " + meetDay + "\n Meeting time: " + meetTime + "\n";
}
Scanner input = new Scanner(System.in);
// Course properties
String name;
String id;
int numStudents;
ArrayList<String> roster;
String meetDay;
String meetTime;
String addStudent;
String removeStudent;
// Course constructor
Course(String name, String id, int numStudents, ArrayList<String> roster, String meetDay, String meetTime) {
// User entered course properties
this.name = input.nextLine();
System.out.println("Enter the course ID. ");
this.id = input.nextLine();
System.out.println("Enter the max number of students. ");
while(!input.hasNextInt()) {
try {
this.numStudents = input.nextInt();
input.nextLine();
} catch (InputMismatchException e) {
System.out.println("This value needs to be a number...\nEnter the max number of students. ");
input.nextLine();
}
}
input.nextLine();
this.roster = new ArrayList<String>();
System.out.println("Enter the meeting day of the course. ");
this.meetDay = input.nextLine();
System.out.println("Enter the meeting time of the course. ");
this.meetTime = input.nextLine();
}
// Modify Course methods
String getID() {
return this.id;
}
public void ChangeName() {
System.out.println("Enter the new name of the course. ");
this.name = input.nextLine();
}
public void ChangeID() {
System.out.println("Enter the new ID of the course. ");
this.id = input.nextLine();
}
public void ChangeNumStudents() {
System.out.println("Enter the new max number of students of the course. ");
while(!input.hasNextInt()) {
try {
this.numStudents = input.nextInt();
input.nextLine();
} catch (InputMismatchException e) {
System.out.println("This value needs to be a number...\nEnter the max number of students. ");
input.nextLine();
}
}
input.nextLine();
}
public void ChangeMeetDay() {
System.out.println("Enter the new meeting day of the course. ");
this.meetDay = input.nextLine();
}
public void ChangeMeetTime() {
System.out.println("Enter the new meeting time of the course. ");
this.meetTime = input.nextLine();
}
public void AddStudent(String addStudent) {
this.roster.add(addStudent);
}
public void RemoveStudent(String removeStudent) {
this.roster.remove(removeStudent);
}
}