-2

So I am trying to make a course scheduler. The program would contain all the courses offered in a particular semester. The user will input the courses he wants to take for a particular semester, and the program would generate all possible timetables based on the users input. (For example a particular course maybe taught my multiple instructors in multiple time slots, that is what I mean by ALL possible timetables)

The approach I am taking is the following I have a Course class which stores all the information about a particular course I have a CourseContainer which has an ArrayList of all the courses I am having hard time in generating all possible timetables So far I have a getAllCourse method

public ArrayList<Courses> getAllCourse(String name){
    ArrayList<Courses> all = new ArrayList<>();
    for (Courses  course : container) {
        if (course.getName().equals(name))
            all.add(course);
    }
    return all;
}

This way I have multiple arraylists of all the courses the user wants. I can not figure out how to get the combinations out of this.

I would appreciate if any one can tip me on how to do this or a better way.

Thank you!

Morteza Asadi
  • 1,819
  • 2
  • 22
  • 39
hsnsd
  • 1,728
  • 12
  • 30
  • 1
    Your question is very lacking in information, so there is no way for us to help you. E.g. where is the information about instructors? Where is the information about time slots? Besides, you need to do some research on generating combinations, write some code yourself, then ask a specific question if you run into trouble. For now, this question is "too broad". – Andreas Jan 04 '17 at 04:22

2 Answers2

0

You want to use Map instead.

Create a Map which holds mapping of course name with list of courses, and then use it like this:

private final Map<String, List<Courses>> container = initializeIt();

public ArrayList<Courses> getAllCourse(String name){
    ArrayList<Courses> all = new ArrayList<>();
    List<Courses> courses = container.get(name);
    if(courses != null) all.addAll(courses);
    return all;
}
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
0

I don't fully understand your implementation, but it sounds like you have a list of courses for each time slot.

If you had 3 timeslots, you could use 3 nested loops to create all the possible combinations. I've simplified it slightly and used a String instead of a Course:

List<String> timeslot1 = new ArrayList<String>();
timeslot1.add("Maths");
timeslot1.add("English");

List<String> timeslot2 = new ArrayList<String>();
timeslot2.add("Science");
timeslot2.add("History");

List<String> timeslot3 = new ArrayList<String>();
timeslot3.add("Italian");
timeslot3.add("Geography");

// a list of all the possible course combinations
List<List<String>> timetables = new ArrayList<List<String>>();

// create the course combinations
for(String course1 : timeslot1) {
    for(String course2 : timeslot2) {
        for(String course3 : timeslot3) {
            List<String> timetable = new ArrayList<String>();
            timetable.add(course1);
            timetable.add(course2);
            timetable.add(course3);

            timetables.add(timetable);
        }
    }
}

// print them
for(List<String> timetable : timetables) {
    System.out.println(String.format("%s, %s, %s", timetable.get(0), timetable.get(1), timetable.get(2)));
}

With the following output:

Maths, Science, Italian
Maths, Science, Geography
Maths, History, Italian
Maths, History, Geography
English, Science, Italian
English, Science, Geography
English, History, Italian
English, History, Geography
Matt
  • 3,677
  • 1
  • 14
  • 24