I made a simple program with main class, Student class and several methods in main class doing job (list, create and Write to file, del item, add item, search and sort items). it's running good but, I wish to have it in more 'clean way' or 'object oriented' way, so to speak, e.g. to create another class Control where will be editing methods from main class. Below is the code. Thanks for any suggestions!
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
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");
System.out.println("Actual 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> \n\n"+
students.toString().replaceAll("\\[|\\]", "").replaceAll("," ,""));
}
}
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;
}
}
}
}