0

The program I created is not printing the menu function from the run method in the ClassRosterController class. I ran the debugger in Netbeans but I was not able to identify what the issue was. Any help would be much appreciated.

    package classroster;

    import ClassRosterController.ClassRosterController;

    public class App {


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

    }

        package ClassRosterController;

    import ClassRoster.dao.ClassRosterDao;
    import ClassRoster.dao.ClassRosterDaoImpl;
    import ClassRoster.dto.Student;
    import ClassRoster.ui.ClassRosterView;
    import ClassRoster.ui.UserIO;
    import ClassRoster.ui.UserIOConsoleImpl;
    import java.util.List;

    public class ClassRosterController {

    ClassRosterView view = new ClassRosterView();
    ClassRosterDao dao = new ClassRosterDaoImpl();
    UserIO io = new UserIOConsoleImpl();

    public void run() {
        boolean keepGoing = true;
        int menuSelection = 0;
        while (keepGoing) {

            menuSelection = getMenuSelection();
            io.print("Main Menu");
            io.print("1. List Student IDs");
            io.print("2. Create New Student");
            io.print("3. View a Student");
            io.print("4. Remove a Student");
            io.print("5. Exit");

            menuSelection = io.readInt("Please select from the above choices.", 1, 5);

            switch (menuSelection) {
                case 1:
                    listStudents();
                    break;
                case 2:
                    createStudent();
                    break;
                case 3:
                    io.print("VIEW STUDENT");
                    break;
                case 4:
                    io.print("REMOVE STUDENT");
                    break;
                case 5:
                    keepGoing = false;
                    break;
                default:
                    io.print("UNKNOWN COMMAND");
            }
        }
        io.print("GOOD BYE");

    }

    private void listStudents(){
        List<Student> studentList = dao.getAllStudents();

        view.displayStudentList(studentList);
    }

    private int getMenuSelection() {
        return view.printMenuAndGetSelection();
    }

    private void createStudent(){
        view.displayCreatesStudentBanner();
        Student newStudent = view.getNewStudentInfo();
        dao.addStudent(newStudent.getStudentId(), newStudent);
        view.displayCreateSuccessBanner();
    }
    }


        package ClassRoster.ui;

    import ClassRoster.dto.Student;
    import java.util.List;


    public class ClassRosterView {
    private UserIO io = new UserIOConsoleImpl();

    public int printMenuAndGetSelection() {
        io.print("");
        io.print("");
        io.print("");
        io.print("");
        io.print("");
        io.print("");
        return io.readInt("Please select from the above choices.", 1, 5);
    }

    public Student getNewStudentInfo(){
        String studentId = io.readString("Please enter Student ID");
        String firstName = io.readString("Please enter First Name");
        String lastName = io.readString("Please enter Last Name");
        String cohort = io.readString("Please enter Cohort");
        Student currentStudent = new Student(studentId);
        currentStudent.setFirstName(firstName);
        currentStudent.setLastName(lastName);
        currentStudent.setCohort(cohort);
        return currentStudent;
    }

    public void displayCreatesStudentBanner(){
        io.print("=== Create Student ===");

    }

    public void displayCreateSuccessBanner(){
        io.readString("Student successfully created. Please hit enter to continue.");
    }

    public void displayStudentList(List<Student> studentList){
        for (Student currentStudent: studentList){
            io.print(currentStudent.getStudentId() + ": "
                    +currentStudent.getFirstName() + " "
                    +currentStudent.getLastName());

        }
        io.readString("Please hit enter to continue.");
    }

    public void displayAllBanner(){
        io.print("=== Display All Students ===");
    }
    }



    package ClassRoster.ui;


    public interface UserIO {
        void print(String msg);

    double readDouble(String prompt);

    double readDouble(String prompt, double min, double max);

    float readFloat(String prompt);

    float readFloat(String prompt, float min, float max);

    int readInt(String prompt);

    int readInt(String prompt, int min, int max);

    long readLong(String prompt);

    long readLong(String prompt, long min, long max);

    String readString(String prompt);
    }


package ClassRoster.ui;

import java.util.Scanner;


public class UserIOConsoleImpl implements UserIO {
    Scanner userInput = new Scanner(System.in);

    @Override
    public void print(String msg) {
        System.out.println(msg);
    }

    @Override
    public double readDouble(String prompt) {

        System.out.println(prompt);
        double inputNumber = userInput.nextDouble();
        return inputNumber;

    }

    @Override
    public double readDouble(String prompt, double min, double max) {
        System.out.println(prompt);
        double inputNumber = userInput.nextDouble();
        boolean loop = true;
        do{
            if(inputNumber < min || inputNumber > max){
            System.out.println("Please enter a number between " + min +" and " + max);
        }
            else {
                loop = false;
            }

        }while(loop);


        return inputNumber;
    }

    @Override
    public float readFloat(String prompt) {
        System.out.println(prompt);
        float inputNumber = userInput.nextFloat();


        return inputNumber; 
    }

    @Override
    public float readFloat(String prompt, float min, float max) {
        System.out.println(prompt);
        float inputNumber = userInput.nextFloat();
        boolean loop = true;
        do{
            if(inputNumber < min || inputNumber > max){
            System.out.println("Please enter a number between " + min +" and " + max);
        }
            else {
                loop = false;
            }

        }while(loop);



        return inputNumber; 
    }

    @Override
    public int readInt(String prompt) {
        System.out.println(prompt);

        int inputNumber = userInput.nextInt();


        return inputNumber;  
    }

    @Override
    public int readInt(String prompt, int min, int max) {
        System.out.println(prompt);
        int inputNumber = userInput.nextInt();
        boolean loop = true;
        do{
            if(inputNumber < min || inputNumber > max){
            System.out.println("Please enter a number between " + min +" and " + max);
        }
            else {
                loop = false;
            }

        }while(loop);



        return inputNumber; 
    }

    @Override
    public long readLong(String prompt) {
        System.out.println(prompt);
        long inputNumber = userInput.nextLong();


        return inputNumber; 
    }

    @Override
    public long readLong(String prompt, long min, long max) {
        System.out.println(prompt);
        long inputNumber = userInput.nextLong();
        boolean loop = true;
        do{
            if(inputNumber < min || inputNumber > max){
            System.out.println("Please enter a number between " + min +" and " + max);
        }
            else {
                loop = false;
            }

        }while(loop);



        return inputNumber; 
    }

    @Override
    public String readString(String prompt) {
       System.out.println(prompt);
       String inputString = userInput.nextLine();


        return inputString; 
    }

}


package ClassRoster.dto;

public class Student {
    private String firstName;
    private String lastName;
    private String studentId;
    private String cohort;

public Student(String studentId){
    this.studentId = studentId;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getStudentId() {
    return studentId;
}

public String getCohort() {
    return cohort;
}

public void setCohort(String cohort) {
    this.cohort = cohort;
}




}



package ClassRoster.dao;

import ClassRoster.dto.Student;
import java.util.List;


public interface ClassRosterDao {


    Student addStudent(String studentId, Student student);


    List<Student> getAllStudents();

    Student getStudent(String studentId);

    Student removeStudent(String studentId);


}

package ClassRoster.dao;

import ClassRoster.dto.Student;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class ClassRosterDaoImpl implements ClassRosterDao {

    private Map<String, Student> students = new HashMap<>();

    @Override
    public Student addStudent(String studentId, Student student) {
       Student newStudent = students.put(studentId, student);
       return newStudent;
    }

    @Override
    public List<Student> getAllStudents() {
        return new ArrayList<Student>(students.values());
    }

    @Override
    public Student getStudent(String studentId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Student removeStudent(String studentId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
statsguyz
  • 323
  • 2
  • 8
  • 24

1 Answers1

1

As a first, your do-while loop in readInt() is broken. while(loop = true) would set loop to true indefinitely. Replace it with while(loop). Secondly, what happens if the inputNumber doesn't fall in the range? You never prompt the user for input again in that loop! So if they don't have the correct input first time round that loop will run indefinitely.

Thirdly, why is your prompt being printed after your loop. Don't you want to prompt the user before?

Dana
  • 310
  • 2
  • 12
  • Thanks for the info. I will make those modifications now and see if it works. – statsguyz Dec 23 '16 at 03:38
  • Made the adjustments. Now I'm having issues with the readString method. If I change it to next(), it allows me to enter the StudentID. However, it will no longer allow me to exit after "Please hit enter to continue." If I keep nextLine() in the readString method, it will skip over "Please enter Student ID" – statsguyz Dec 23 '16 at 04:47
  • @statsguyz there are several issues in your code that might be giving you a weird result. I tested your code and if you hit enter to exit and then enter a number, it should work. I think you're seeing this issue because as I said previously, you are printing your prompts *after* you get your input which confuses you. You need to put them before. I think you're probably doing this due to the new line issue that sometimes comes with Scanner (See [here](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo)). – Dana Dec 23 '16 at 05:28