2
\$\begingroup\$

I've made a small project from my institute. I've got 5 classes, in 4 of the classes (Teachers, Students, Group, Subject). I've got a constructor and get&set methods. In the main class I've got an all functional realization. How can I make this code better and easier to use?

package com.company;

import java.util.*;

public class Main {

    static Main main = new Main();
    public static List<Teachers> tch = new ArrayList<Teachers>();
    public static List<Students> std = new ArrayList<Students>();
    public static List<Group> grp = new ArrayList<Group>();
    public static List<Subject> sbj = new ArrayList<Subject>();

    static Scanner sc = new Scanner(System.in);

    public static void io(String input){
        String surname, name, address, subjname, gradeBook;
        if(input.equals("addtch")) {
            do {
                System.out.println("Enter teacher surname: ");
                surname = sc.nextLine();
                System.out.println("Enter teacher name: ");
                name = sc.nextLine();
                tch.add(new Teachers(surname, name));
                System.out.println("Add more teachers? y/n");
            } while (sc.nextLine().startsWith("y"));
        }

        if (input.equals("addstd")){ do {
            try {
                System.out.println("Enter student surname: ");
                surname = sc.nextLine();
                System.out.println("Enter student name: ");
                name = sc.nextLine();
                System.out.println("Enter student adress: ");
                address = sc.nextLine();
                System.out.println("Enter student gradebook: ");
                gradeBook = sc.nextLine();
                System.out.println("Enter student course: ");
                int course = sc.nextInt();
                System.out.println("Enter student phone number: ");
                long phoneNumb = sc.nextLong();
                std.add(new Students(surname, name, course, gradeBook, address, phoneNumb));
            } catch (Exception e) {
                System.out.println("Wrong input");
                sc.reset();
            }
            } while (sc.nextLine().startsWith("y"));
        }

        if (input.equals("addgrp")){ do { try {
            surname = null;
            name = null;
            subjname = null;
            System.out.println("Enter group number: ");
            int groupnmb = sc.nextInt();
            grp.add(new Group(surname, name, subjname, groupnmb));
        } catch (Exception e) {
            System.out.println("Wrong input");
            sc.reset();
        }
        } while(sc.nextLine().startsWith("y"));
        }

        if(input.equals(("addsbj"))){ do {
            System.out.println("Enter name of subject: ");
            subjname = sc.nextLine();
            surname = null;
            name = null;
            sbj.add(new Subject(surname, name, subjname));
            System.out.println("Input again?: y/n");
        } while(sc.nextLine().startsWith("y"));
        }
    }

    public static void edit(String input) {
        Iterator<Teachers> teacherIT = tch.iterator();
        Iterator<Students> studentIT = std.iterator();
        Iterator<Group> groupIT = grp.iterator();
        Teachers teacher = teacherIT.next();
        Students student = studentIT.next();
        Group group = groupIT.next();
        String surnameToFound, newSurname, newName, newAddress, newGradeBook, groupToFound;
        long newPhoneNumb;
        int newCourse, newGroupNumb;
        if (input.equals("edittch")) {
            System.out.println("Enter teacher surname to found: ");
            surnameToFound = sc.nextLine();
            while (teacherIT.hasNext()) {
                teacher = teacherIT.next();
                if(teacher.getSurname().equals(surnameToFound)) {
                    System.out.println("Enter new teacher surname: ");
                    newSurname = sc.nextLine();
                    teacher.setSurname(newSurname);
                }
            }
        }

        if (input.equals("editstd")){
            System.out.println("Enter student surname to found: ");
            surnameToFound = sc.nextLine();
            while (studentIT.hasNext()) {
                student = studentIT.next();
                if(student.getSurname().equals(surnameToFound)) {
                    System.out.println("Enter new student surname: ");
                    newSurname = sc.nextLine();
                    System.out.println("Enter new student name: ");
                    newName = sc.nextLine();
                    System.out.println("Enter new student address: ");
                    newAddress = sc.nextLine();
                    System.out.println("Enter new student gradebook: ");
                    newGradeBook = sc.nextLine();
                    System.out.println("Enter new student course: ");
                    newCourse = sc.nextInt();
                    System.out.println("Enter new student phone numb: ");
                    newPhoneNumb = sc.nextLong();
                    student.setSurname(newSurname);
                    student.setName(newName);
                    student.setAddress(newAddress);
                    student.setGradeBook(newGradeBook);
                    student.setCourse(newCourse);
                    student.setPhoneNumb(newPhoneNumb);
                }
            }
        }

        if (input.equals("editgrp")){
            System.out.println("Enter group to found: ");
            groupToFound = sc.nextLine();
            while (groupIT.hasNext()){
                group = groupIT.next();
                String strInt = Integer.toString(group.getGroupNumb());
                if(groupToFound.equals(strInt)){
                    System.out.println("Enter new group numb: ");
                    newGroupNumb = sc.nextInt();
                    group.setGroupNumb(newGroupNumb);
                }
            }
        }
    }

    public static void del(String input){
        Iterator<Teachers> teacherIT = tch.iterator();
        Iterator<Students> studentIT = std.iterator();
        Iterator<Group> groupIT = grp.iterator();
        Teachers teacher = teacherIT.next();
        Students student = studentIT.next();
        Group group = groupIT.next();

        if (input.equals("deltch")) {
            System.out.println("Enter teacher surname to delete: ");
            String str = sc.nextLine();
            while (teacherIT.hasNext()) {
                teacher = teacherIT.next();
                if (str.equals(teacher.getSurname())) {
                    teacherIT.remove();
                }
            }
        }


        if (input.equals("delstd")){
            System.out.println("Enter student surname to delete: ");
            String str = sc.nextLine();
            while (studentIT.hasNext()){
                student = studentIT.next();
                if (str.equals(student.getSurname())){
                    studentIT.remove();
                }
            }
        }

        if (input.equals("delgrp")){
            System.out.println("Enter group number to delete: ");
            String str = sc.nextLine();
            while (groupIT.hasNext()){
                group = groupIT.next();
                String strLong = Long.toString(group.getGroupNumb());
                if (str.equals(strLong)){
                    groupIT.remove();
                }
            }
        }
    }

    public static void control(String input){
        Iterator<Group> groupIT = grp.iterator();
        Iterator<Subject> subjectIT = sbj.iterator();
        Subject subject = subjectIT.next();
        String groupToFound, newSubj, subjToRemove, teacherSurname, teacherName, subjToFound, studentSurname, studentName;

        if (input.equals("sbjtogrp")) {
            System.out.println("Enter group to found: ");
            groupToFound = sc.nextLine();
            System.out.println("Enter subject name: ");
            newSubj = sc.nextLine();
            while (groupIT.hasNext()){
                Group group = groupIT.next();
                String strInt = Integer.toString(group.getGroupNumb());
                if (groupToFound.equals(strInt) && newSubj.equals(subject.getSubjName())) {
                    group.setSubjName(newSubj);
                }
            }
        }

        if(input.equals("sbjtogrpRemove")){
            System.out.println("Enter group to found: ");
            groupToFound = sc.nextLine();
            System.out.println("Enter subject te remove from group: ");
            subjToRemove = sc.nextLine();
            while (groupIT.hasNext()){
                Group group = groupIT.next();
                String strInt = Integer.toString(group.getGroupNumb());
                if (groupToFound.equals(strInt) && subjToRemove.equals(subject.getSubjName())){
                    group.setSubjName(null);
                }
            }
        }
        //4.3.  Можливість додавання викладача даного предмета вивчення
        if(input.equals("tchtosbj")){
            System.out.println("Enter subject name: ");
            subjToFound = sc.nextLine();
            while (groupIT.hasNext()){
                Group group = groupIT.next();
                if(subjToFound.equals(subject.getSubjName())){
                    System.out.println("Enter teacher surname: ");
                    teacherSurname = sc.nextLine();
                    System.out.println("Enter teacher name: ");
                    teacherName = sc.nextLine();
                    group.setSurname(teacherSurname);
                    group.setName(teacherName);
                }
            }
        }

        //4.4.  Можливість заміни викладача даного предмета вивчення
        if(input.equals("tchtosbjEdit")){
            System.out.println("Enter subject name: ");
            subjToFound = sc.nextLine();
            System.out.println("Enter teacher surname: ");
            teacherSurname = sc.nextLine();
            System.out.println("Enter teacher name: ");
            teacherName = sc.nextLine();
            while (groupIT.hasNext()){
                Group group = groupIT.next();
                if (subjToFound.equals(subject.getSubjName()) || teacherSurname.equals(group.getSurname()) || teacherName.equals(group.getName())){
                    group.setSurname(null);
                    group.setName(null);
                }
            }
        }

        if(input.equals("stdtogrp")){
            System.out.println("Enter group to found: ");
            groupToFound = sc.nextLine();
            System.out.println("Enter student surname: ");
            studentSurname = sc.nextLine();
            System.out.println("Enter student name: ");
            studentName = sc.nextLine();
            while (groupIT.hasNext()){
                Group group = groupIT.next();
                String strInt = Integer.toString(group.getGroupNumb());
                if (groupToFound.equals(strInt)) {
                    group.setStudList(studentSurname, studentName);
                }
            }
        }

        if(input.equals("stdtogrpRemove")){
            System.out.println("Enter group to found: ");
            groupToFound = sc.nextLine();
            System.out.println("Enter student surname to remove: ");
            studentSurname = sc.nextLine();
            while (groupIT.hasNext()){
                Group group = groupIT.next();
                String strInt = Integer.toString(group.getGroupNumb());
                if (groupToFound.equals(strInt) || studentSurname.equals(group.getStudList())) {
                    group.setStudList(null, null);
                }
            }
        }

        if (input.equals("findstdfromgrp")){
            System.out.println("Enter group to found: ");
            groupToFound = sc.nextLine();
            System.out.println("Enter student surname to remove: ");
            studentSurname = sc.nextLine();
            while (groupIT.hasNext()){
                Group group = groupIT.next();
                String strInt = Integer.toString(group.getGroupNumb());
                if (groupToFound.equals(strInt) || studentSurname.equals(group.getStudList())) {
                    System.out.println("Student exist in list");
                }
            }
        }

        if (input.equals("findstdfromsbj")){

        }

        if (input.equals("findstdfromtch")){

        }

    }

    public static void search(String input){
        Iterator<Teachers> teacherIT = tch.iterator();
        Iterator<Students> studentIT = std.iterator();
        if (input.equals("findtch")) {
            System.out.println("Enter surname teacher to find in list");
            String find = sc.nextLine();
            while (teacherIT.hasNext()) {
                Teachers teacher = teacherIT.next();
                if (find.equals(teacher.getSurname())) {
                    System.out.println("Teacher exist in list");
                }
            }
        }

        if (input.equals("findstd")){
            System.out.println("Enter Student surname: ");
            String surname = sc.nextLine();
            System.out.println("Enter Student name");
            String name = sc.nextLine();
            while (studentIT.hasNext()) {
                Students student = studentIT.next();
                if (surname.equals(student.getSurname()) && name.equals(student.getName())) {
                    System.out.println("Student exist in list");
                }
            }
        }

    }

    public static void elementOut(String input){
        Iterator<Teachers> teacherIT = tch.iterator();
        Iterator<Students> studentIT = std.iterator();
        Iterator<Group> groupIT = grp.iterator();
        String elementOut;
        if (input.equals("tchout")){
            System.out.println("Enter teacher surname: ");
            elementOut = sc.nextLine();
            while (teacherIT.hasNext()) {
                Teachers teacher = teacherIT.next();
                if (elementOut.equals(teacher.getSurname())){
                    System.out.println(teacher.toString());
                }
            }
        }

        if(input.equals("stdout")){
            System.out.println("Enter student name: ");
            elementOut = sc.nextLine();
            while (studentIT.hasNext()) {
                Students student = studentIT.next();
                if (elementOut.equals(student.getSurname())){
                    System.out.println(student.toString());
                }
            }
        }

        if(input.equals("grpout")){
            System.out.println("Enter group number");
            elementOut = sc.nextLine();
            while (groupIT.hasNext()) {
                Group group = groupIT.next();
                String strInt = Integer.toString(group.getGroupNumb());
                if (elementOut.equals(strInt)){
                    System.out.println(group.toString());
                }
            }

        }
    }

    public static void out(String input){
        if (input.equals("addtch") || input.equals("delnew") || input.equals("edittch") || input.equals("showtch"))
        for(Teachers t : tch) {
            System.out.println(t.toString());
        }

        if (input.equals("addstd") || input.equals("delstd") || input.equals("editstd") || input.equals("showstd"))
            for(Students s : std) {
                System.out.println(s.toString());
            }

        if (input.equals("addgrp") || input.equals("delgrp") || input.equals("editgrp") || input.equals("showgrp"))
            for(Group g : grp) {
                System.out.println(g.grpOut());
            }
        if (input.equals("sbjtogrp") || input.equals("sbjtogrpRemove") || input.equals("tchtosbj") || input.equals("tchtosbjEdit")){
            for (Group groupL : grp){
                System.out.println(groupL.toString());
            }
        }

        if (input.equals("stdtogrp") || input.equals("stdtogrpRemove")){
            for (Group groupL : grp){
                System.out.println(groupL.getStudListOut());
            }
        }
        if (input.equals("findstdfromgrp")){}

    }

    public static void main(String[] args) {
        boolean flag;
        System.out.println("Enter 'help'");
        for (;;) {
            String str = sc.nextLine();
            flag = false;
            if (str.equals("help")){
                System.out.println("Add/Remove/Edit/Find/Out(only 1)/show(all list) teacher - addtch/deltch/edittch/findtch/tchout/showtch \n" +
                        "Add/Remove/Edit/Find/Out(only 1)/show(all list)/FindStudentFromGroup student - addstd/delstd/editstd/findstd/stdout/showstd/findstdfromgrp \n" +
                        "Add/Remove/Edit/show(all list) group - addgrp/delgrp/editgrp/grpout/showgrp \n" +
                        "AddSubjectToGroup/RemoveSubjectFromGroup/AddTeacherToSubject/EditTeacherFromSubject/AddStudentToGroup/RemoveStudentFromGroup/ - sbjtogrp/sbjtogrpRemove/tchtosbj/tchtosbjEdit/stdtogrp/stdtogrpRemove");
                flag=true;
            }

            if(str.equals("addtch") || str.equals("addstd") || str.equals("addgrp") || str.equals("addsbj")){
                main.io(str);
                main.out(str);
                flag=true;
            }
            if (str.equals("deltch") || str.equals("delstd") || str.equals("delgrp")){
                main.del(str);
                main.out(str);
                flag=true;
            }
            if (str.equals("edittch") || str.equals("editstd") || str.equals("editgrp")){
                main.edit(str);
                main.out(str);
                flag=true;
            }
            if (str.equals("findtch") | str.equals("findstd")){
                main.search(str);
                flag=true;
            }
            if (str.equals("tchout") || str.equals("stdout") || str.equals("grpout")){
                main.elementOut(str);
                flag=true;
            }
            if (str.equals("showtch") || str.equals("showstd") || str.equals("showgrp")){
                main.out(str);
                flag=true;
            }
            if (str.equals("sbjtogrp") || str.equals("sbjtogrpRemove") || str.equals("tchtosbj") || str.equals("tchtosbjEdit") || str.equals("stdtogrp") || str.equals("stdtogrpRemove") || str.equals("findstdfromgrp")){
                main.control(str);
                main.out(str);
                flag=true;
            }
            if (str.equals("exit")){
                break;
            }
            if (!flag)
                System.out.println("There is no such command: "+str);
        }
    }
}
\$\endgroup\$
4
\$\begingroup\$

Variable Names

There is no good reason to abbreviate variable and method names, but it does make your code a lot harder to read.

  • tch should be teachers, std should be students, etc.
  • subjname should be subjectName.
  • sc should be scanner.
  • io should probably be named something like add (it only adds, it doesn't delete, etc, and it definitely doesn't just perform input/output).

The same is also true for input strings. For example, nobody will be able to remember addtch.

Code length

Your main class is way too big. A first step to fixing this would be to extract duplicate code. For example, you have this very often:

System.out.println("Some message");
someVar = sc.nextLine();

If you extract it to a method:

private static String getInput(String message) {
        System.out.println(message);
        return sc.nextLine();
} 

You can use it like this:

someVar = getInput("Some message");

Yes, it only saves one line, but you really do have it in a lot of places, so it's worth it.

Indentation

Proper indentation is important for readability. For example this code:

    } catch (Exception e) {
        System.out.println("Wrong input");
        sc.reset();
    }
    } while(sc.nextLine().startsWith("y"));
    }

It's really hard to see where what ends. You can fix this very easily with any IDE.

Duplicate ifs

It is odd that you ask in your main method if(str.equals("addtch") || str.equals("addstd") || str.equals("addgrp") || str.equals("addsbj")), and then call main.io(str), where you again ask if(input.equals("addtch")), etc.

Just ask once, and create separate methods for addTeacher, etc.

Misc

  • Your many if with the flag to see if there was a valid input could easily be written as a switch.
  • static Main main = new Main(); isn't needed at all. As all your methods are static, you can just to Main.io(), or just io().
  • define variables in as small a scope as possible. Eg address is only needed in one of the ifs, teacherIT is also only used in a single if, but defined at function level, etc.
  • instead of for (;;) it is customary to use while(true).
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.