私は、学生クラスといくつかの方法(リスト、作成、ファイルへの書き込み、アイテムの追加、アイテムの追加、検索と並べ替えアイテム)を行うメインクラスのいくつかの方法で簡単なプログラムを作ろうとしています。 それはうまく動作していますが、 私はそれをもっと「クリーンな方法」や「オブジェクト指向の」方法で表現したいと考えています。 少し改善する方法はありますか? 以下はそのコードです。 ありがとうございました!単純なプログラムでオブジェクト指向のアプローチ
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", "[email protected]"));
students.add(new Student("michael", "[email protected]"));
students.add(new Student("peter", "[email protected]"));
students.add(new Student("andrew", "[email protected]"));
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;
}
}
}
}
http://codereview.stackexchange.com/ – Idos
作業中のプログラムの批評が必要な場合は、CodeReview SEの方が適しています。 –