Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6877591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:37:07+00:00 2026-05-27T04:37:07+00:00

I’m least happy with case 5 as it comprises too much code and allows

  • 0

I’m least happy with case 5 as it comprises too much code and allows duplicates. I heard about Sets but can see how it would work in this example

Here’s my complete source code:

public class MyApplication {

public static void main(String[] args) {
    Control control = new Control();
    control.run();
}

}

Driver/Control Class

import java.util.Scanner;


public class Control {

// Integers represented as module codes 
private static final int UFCE1 = 0;
private static final int UFCE2 = 1;
private static final int UFCE3 = 2;
private static final int UFCE4 = 3;
private static final int UFCE5 = 4;
// Integers represented as students
private static final int STUDENT1 = 0;
private static final int STUDENT2 = 1;
private static final int STUDENT3 = 2;
private static final int STUDENT4 = 3;
private static final int STUDENT5 = 4;

public void run() {

   ...

    while (true) {
        Menu menu = new Menu();
        menu.getMainMenu();

        try {
            Scanner scan = new Scanner(System.in);

            int selection = scan.nextInt();
            switch (selection) {
                case 1:
                    for (Student student : students) {
                        System.out.print(student.getName() + " ");
                    }
                    break;
                case 2:
                    for (Module module : modules) {
                        System.out.print(module.getName() + " ");
                    }
                    break;
                case 3:
                    System.out.println("Search module code: ");
                    scan = new Scanner(System.in);
                    String searchModule = scan.nextLine().trim();

                    for (Module module : modules) {
                        if (searchModule.equalsIgnoreCase(module.getName())) {
                            for (int s = 0; s < module.getStudents().size(); s++) {
                                System.out.print(module.getStudents().get(s) + " ");
                            }
                        }
                    }
                    break;
                case 4:
                    System.out.println("Search students : ");
                    scan = new Scanner(System.in);
                    String searchStudent = scan.nextLine().trim();

                    for (Student student : students) {
                        if (searchStudent.equalsIgnoreCase(student.getName())) {
                            for (int s = 0; s < student.getModules().size(); s++) {
                                System.out.print(student.getModules().get(s) + " ");
                            }
                        }
                    }
                    break;
                case 5:
                    // Print out students 
                    System.out.println("select a student: ");
                    for (int i = 0; i < students.length; i++) {
                        System.out.println(i + " " + students[i]);
                    }

                    selection = scan.nextInt();
                    switch (selection) {

                        case STUDENT1:
                            System.out.println("select a module to enroll " + students[0].getName() + " on to:");
                            for (int i = 0; i < modules.length; i++) {
                                System.out.println(i + " " + modules[i]);
                            }

                            selection = scan.nextInt();
                            switch (selection) {
                                case UFCE1:
                                    System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[0].getName());
                                    students[0].addModule(modules[0].getName());
                                    modules[0].addStudent(students[0].getName());
                                    break;
                                case UFCE2:
                                    System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[1].getName());
                                    students[0].addModule(modules[1].getName());
                                    modules[1].addStudent(students[0].getName());
                                    break;
                                case UFCE3:
                                    System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[2].getName());
                                    students[0].addModule(modules[2].getName());
                                    modules[2].addStudent(students[0].getName());
                                    break;
                                case UFCE4:
                                    System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[3].getName());
                                    students[0].addModule(modules[3].getName());
                                    modules[3].addStudent(students[0].getName());
                                    break;
                                case UFCE5:
                                    System.out.println("You have enrolled " + students[0].getName() + " on to " + modules[4].getName());
                                    students[0].addModule(modules[4].getName());
                                    modules[4].addStudent(students[0].getName());
                                    break;
                                default:
                                    System.out.println("Invalid subject selected. Returning to main menu...");
                            }
                            break;

                        case STUDENT2:
                            System.out.println("select a module to enroll " + students[1].getName() + " on to:");
                            for (int i = 0; i < modules.length; i++) {
                                System.out.println(i + " " + modules[i]);
                            }

                            selection = scan.nextInt();
                            switch (selection) {
                                case UFCE1:
                                    System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[0].getName());
                                    students[1].addModule(modules[0].getName());
                                    modules[0].addStudent(students[1].getName());
                                    break;
                                case UFCE2:
                                    System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[1].getName());
                                    students[1].addModule(modules[1].getName());
                                    modules[1].addStudent(students[1].getName());
                                    break;
                                case UFCE3:
                                    System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[2].getName());
                                    students[1].addModule(modules[2].getName());
                                    modules[2].addStudent(students[1].getName());
                                    break;
                                case UFCE4:
                                    System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[3].getName());
                                    students[1].addModule(modules[3].getName());
                                    modules[3].addStudent(students[1].getName());
                                    break;
                                case UFCE5:
                                    System.out.println("You have enrolled " + students[1].getName() + " on to " + modules[4].getName());
                                    students[1].addModule(modules[4].getName());
                                    modules[4].addStudent(students[1].getName());
                                    break;
                                default:
                                    System.out.println("Invalid subject selected. Returning to main menu...");
                            }
                            break;

                        case STUDENT3:
                            System.out.println("select a module to enroll " + students[2].getName() + " on to:");
                            for (int i = 0; i < modules.length; i++) {
                                System.out.println(i + " " + modules[i]);
                            }

                            selection = scan.nextInt();
                            switch (selection) {
                                case UFCE1:
                                    System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[0].getName());
                                    students[2].addModule(modules[0].getName());
                                    modules[0].addStudent(students[2].getName());
                                    break;
                                case UFCE2:
                                    System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[1].getName());
                                    students[2].addModule(modules[1].getName());
                                    modules[1].addStudent(students[2].getName());
                                    break;
                                case UFCE3:
                                    System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[2].getName());
                                    students[2].addModule(modules[2].getName());
                                    modules[2].addStudent(students[2].getName());
                                    break;
                                case UFCE4:
                                    System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[3].getName());
                                    students[2].addModule(modules[3].getName());
                                    modules[3].addStudent(students[2].getName());
                                    break;
                                case UFCE5:
                                    System.out.println("You have enrolled " + students[2].getName() + " on to " + modules[4].getName());
                                    students[2].addModule(modules[4].getName());
                                    modules[4].addStudent(students[2].getName());
                                    break;
                                default:
                                    System.out.println("Invalid subject selected. Returning to main menu...");
                            }
                            break;
                        case STUDENT4:
                            System.out.println("select a module to enroll " + students[3].getName() + " on to:");
                            for (int i = 0; i < modules.length; i++) {
                                System.out.println(i + " " + modules[i]);
                            }

                            selection = scan.nextInt();
                            switch (selection) {
                                case UFCE1:
                                    System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[0].getName());
                                    students[3].addModule(modules[0].getName());
                                    modules[0].addStudent(students[3].getName());
                                    break;
                                case UFCE2:
                                    System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[1].getName());
                                    students[3].addModule(modules[1].getName());
                                    modules[1].addStudent(students[3].getName());
                                    break;
                                case UFCE3:
                                    System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[2].getName());
                                    students[3].addModule(modules[2].getName());
                                    modules[2].addStudent(students[3].getName());
                                    break;
                                case UFCE4:
                                    System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[3].getName());
                                    students[3].addModule(modules[3].getName());
                                    modules[3].addStudent(students[3].getName());
                                    break;
                                case UFCE5:
                                    System.out.println("You have enrolled " + students[3].getName() + " on to " + modules[4].getName());
                                    students[3].addModule(modules[4].getName());
                                    modules[4].addStudent(students[3].getName());
                                    break;
                                default:
                                    System.out.println("Invalid subject selected. Returning to main menu...");
                            }
                            break;
                        case STUDENT5:
                            System.out.println("select a module to enroll " + students[4].getName() + " on to:");
                            for (int i = 0; i < modules.length; i++) {
                                System.out.println(i + " " + modules[i]);
                            }

                            selection = scan.nextInt();
                            switch (selection) {
                                case UFCE1:
                                    System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[0].getName());
                                    students[4].addModule(modules[0].getName());
                                    modules[0].addStudent(students[4].getName());
                                    break;
                                case UFCE2:
                                    System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[1].getName());
                                    students[4].addModule(modules[1].getName());
                                    modules[1].addStudent(students[4].getName());
                                    break;
                                case UFCE3:
                                    System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[2].getName());
                                    students[4].addModule(modules[2].getName());
                                    modules[2].addStudent(students[4].getName());
                                    break;
                                case UFCE4:
                                    System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[3].getName());
                                    students[4].addModule(modules[3].getName());
                                    modules[3].addStudent(students[4].getName());
                                    break;
                                case UFCE5:
                                    System.out.println("You have enrolled " + students[4].getName() + " on to " + modules[4].getName());
                                    students[4].addModule(modules[4].getName());
                                    modules[4].addStudent(students[4].getName());
                                    break;
                                default:
                                    System.out.println("Invalid subject selected. Returning to main menu...");
                            }
                            break;
                        default:
                            System.out.println("Invalid student selected. Returning to main menu...");
                    }
                    break;
                case 6:
                    System.out.println("Goodbye!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid option selected. You must enter a number between 1 & 6!");
            } // end switch

        } catch (Exception e) {
            System.out.println("Invalid entry. You must enter a number between 1 & 6");
        }
    } // end while

}

}

Menu Class

public class Menu {
private String Menu;

public String getMainMenu() {
    System.out.println("\n\nSelect an option:");
        System.out.println(
                "1 List all student names"
                + "\n2 List all module details"
                + "\n3 List all students on a module"
                + "\n4 List all modules attached to a student"
                + "\n5 Attach a valid student to a valid module"
                + "\n6 Exit\n");
    return Menu;
}

}

Module Class

import java.util.ArrayList;


public class Module {
private String name;
private ArrayList<String> students;

public Module(String name) {
    this.name = name;
    students = new ArrayList<String>();
}

public String getName() {
    return name;
}

public ArrayList<String> getStudents() {
    return students;
}

public void addStudent(String student){
    students.add(student);
}

@Override
public String toString() {
    return name; 
}

}

Student Class

import java.util.ArrayList;

public class Student {
private String name;
private ArrayList<String> modules;

public Student(String name) {
    this.name = name;
    modules = new ArrayList<String>();
}

public String getName() {
    return name;
}

public ArrayList<String> getModules() {
    return modules;
}

public void addModule(String module){
    modules.add(module);
}

@Override
public String toString() {
    return name;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:37:07+00:00Added an answer on May 27, 2026 at 4:37 am
    1. replace your integers with enums
    2. extract every switch block to a separate well named method
    3. extract every case block into a separate method as well.

    Then come back with what you have

    In case 5 you set mutual references between modules and students.
    You can reduce code duplication there by declaring one of the two classes as the leading one, lets say the module and inside the addStudent Method you call addModule(this)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.