简单程序中面向对象的方法

时间:2017-01-05 14:05:42

标签: java

我正在尝试使用Student类和主类中的几个方法来创建一个简单的程序(列表,创建和写入文件,del项目,添加项目,搜索和排序项目)。 它运行良好但是, 我希望以更“干净的方式”或“面向对象”的方式拥有它。 任何想法如何改善它? 下面是代码。 谢谢你的任何建议!

public class SimpleDB {

static FileWriter fstream;
static BufferedWriter out;
public static ArrayList<Student> students;
static Scanner menuSc;
static File file;
static Scanner nameOfFile;
static FileWriter wr;
static Scanner addNameAndEmail;
static Scanner sort;
static Scanner search;

public static void createWriteToFile() {
    try {

        String aktDir = System.getProperty("user.dir");// the directory where app (java) was started (working dir)
        //System.out.println("Aktual dir>" + aktDir);
        System.out.println("Please enter name of file ...\n");
        nameOfFile = new Scanner(System.in);
        String nof = nameOfFile.nextLine();

        file = new File(nof);
        if (!file.exists()) {
            System.out.println("Creating new file : " + nof);
        }
        try {    //create new file
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(SimpleDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Writing to file " + nof);
        try {
            wr = new FileWriter(nof);
        } catch (IOException ex) {
            Logger.getLogger(SimpleDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (Student stu : students) {
            try {
                wr.write(stu.toString());
            } catch (IOException ex) {
                Logger.getLogger(SimpleDB.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        wr.close();
    } catch (IOException ex) {
        Logger.getLogger(SimpleDB.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void sort() {
    boolean b = true;
    while (true) {
        System.out.println("Sorting> by name: type N, by email: type E, for exit: type X \n");
        sort = new Scanner(System.in);
        String s = sort.nextLine();
        switch (s) {

            case "N":
                System.out.println("Sorting by name....: \n");
                students.sort((Student s1, Student s2) -> {
                    return s1.getName().compareToIgnoreCase(s2.getName());
                });

                System.out.println("Sorted by name> \n" + students);
                break;

            case "E":
                System.out.println("Sorting by mail....: \n");
                students.sort((Student s1, Student s2) -> {
                    return s1.getEmail().compareToIgnoreCase(s2.getEmail());
                });
                System.out.println("Sorted list> \n" + students);
                break;
            case "X":
                System.out.println("Returning to main menu...");
                b = false;
                return;

            default:
                System.out.println("Please enter correct choice! ");
                break;

        }

    }
}

public static void search() {
    System.out.println("Enter a name you want to search>  \n");
    search = new Scanner(System.in);
    boolean bol = false;
    String se = search.next();
    for (int i = 0; i < students.size(); i++) {
        if (se.equalsIgnoreCase(students.get(i).getName())) {
            bol = true;
            break;
        }
    }
    if (bol) {
        System.out.println("found");
    } else {
        System.out.println("not found");

    }
}

private static void add() {
    addNameAndEmail = new Scanner(System.in);
    System.out.println("Please enter a name: ");
    String n = addNameAndEmail.nextLine();

    System.out.println("Please enter an email: ");
    String e = addNameAndEmail.nextLine();

    students.add(new Student(n, e));
    System.out.println("\n" + "new student " + students);
}

public static void list() {
    System.out.println("List of Students> ");
    String l = null;
    for (Student stu : students) {
        System.out.println(stu);

    }

}

public static char menu() {
    System.out.println(""
            + " 'A' list, 'B' add, 'C' save to file, 'D' search, 'E' sort data, 'F' exit from program > ");
    menuSc = new Scanner(System.in);
    String c = menuSc.nextLine();
    if (c.isEmpty()) {
        return ' ';
    } //Files.copy(null, null, options) 
    return c.toUpperCase().charAt(0);
}

public static void main(String[] args) throws IOException {

    // some students added
    students = new ArrayList<>();
    students.add(new Student("alan", "alan@gmail.com"));
    students.add(new Student("michael", "michael@gmail.com"));
    students.add(new Student("peter", "peter@gmail.com"));
    students.add(new Student("andrew", "andrew@gmail.com"));
    boolean a = true;
    while (a = true) {

        char c = menu();

        switch (c) {

            case 'A':
                list();
                break;
            case 'B':
                add();
                break;
            case 'C':
                createWriteToFile();
                break;
            case 'E':
                sort();
                break;
            case 'D':
                search();
                break;
            case 'F':
                System.out.println("Good Bye!");
                a = false;
                return;

        }

    }

}
}

1 个答案:

答案 0 :(得分:2)

如前所述,您可能想在codereview.stackexchange.com上提出这个问题,因为它更适合您的问题。

在高级概述中,您应该创建一些Object!因此OOP。

Student

  • name - String
  • 电子邮件 - 字符串

StudentService

  • 学生 - 列表
  • sort() - 方法
  • 添加(学生) - 方法
  • list() - 方法
  • 等...

Main

在此处添加学生,将其放入列表中,并将列表提供给StudentService班级实例。然后,您可以根据需要调用studentService对象的方法。

这是一个简单的例子,你可以随意扩展它。

相关问题