patternjavaModerate
"Course manager" Java program
Viewed 0 times
managerprogramcoursejava
Problem
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 courses = new ArrayList<>();
public static int getIndex(String viewID) {
for(int n = 0 ; n 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
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 courses = new ArrayList<>();
public static int getIndex(String viewID) {
for(int n = 0 ; n 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
Solution
- Method names in java start in lower case:
ChangeNumStudents=>changeNumStudents
- Java is not c, no need to declare all these variables at the top of your main: declare them when you need them, with the smallest possible scope.
-
an alternative to your long series of ifs would be to use a map of actions (using Java 8 syntax - with prior versions it may be a bit cumbersome and possibly not the best approach):
private static final Map> actions = new HashMap<> ();
static {
actions.put("back", c -> { });
actions.put("name", Course::changeName);
actions.put("add student"), CourseManagment::addStudent);
//etc.
}
//in your loop:
Course c = course.get(refIndex);
Consumer action = actions.get(modify);
action.accept(course);An example of the
addStudent method in CourseManagement would be:private static void addStudent(Course course) {
while(true) {
System.out.println("Enter the name of the student you wish to add. Type 'done' when you're finished adding.");
String student = input.nextLine();
if (student.equals("done")) break;
course.addStudent(student);
}
}Code Snippets
private static final Map<String, Consumer<Course>> actions = new HashMap<> ();
static {
actions.put("back", c -> { });
actions.put("name", Course::changeName);
actions.put("add student"), CourseManagment::addStudent);
//etc.
}
//in your loop:
Course c = course.get(refIndex);
Consumer<Course> action = actions.get(modify);
action.accept(course);private static void addStudent(Course course) {
while(true) {
System.out.println("Enter the name of the student you wish to add. Type 'done' when you're finished adding.");
String student = input.nextLine();
if (student.equals("done")) break;
course.addStudent(student);
}
}Context
StackExchange Code Review Q#136258, answer score: 10
Revisions (0)
No revisions yet.