Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class _08CSVDatabase {
- private static final String STUDENTS_PATH = "resources/students.txt";
- private static final String GRADES_PATH = "resources/grades.txt";
- private static File studentsFile = new File(STUDENTS_PATH);
- private static File gradesFile = new File(GRADES_PATH);
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String command;
- System.out.println("Insert one of these commands (add student,add grades, search, delete, update, exit): ");
- while (!(command = sc.nextLine().toLowerCase()).equals("exit")) {
- if (command.equals("add student")) {
- System.out.println("Insert full name,age and hometown in the following format(full name,age,hometown): ");
- command = sc.nextLine();
- String[] commandArgs = command.split(",");
- insertStudent(commandArgs[0], commandArgs[1], commandArgs[2]);
- }else if(command.equals("add grades")){
- System.out.println("Insert id of the student and what grade to insert in format(id,grade): ");
- command = sc.nextLine();
- String[] commandArgs = command.split(",");
- insertGradesById(commandArgs[0], commandArgs[1]);
- } else if (command.equals("search")) {
- System.out.println("Search by full name or id in: ");
- command = sc.nextLine();
- if (command.equals("full name")) {
- System.out.println("Insert name: ");
- command = sc.nextLine().toLowerCase();
- searchByIdOrName(command, "full name");
- } else if (command.equals("id")) {
- System.out.println("Insert id: ");
- command = sc.nextLine();
- searchByIdOrName(command, "id");
- } else {
- System.out.println("No such search demand");
- }
- } else if (command.equals("delete")) {
- System.out.println("Insert id to delete: ");
- command = sc.nextLine();
- deleteById(command);
- } else if (command.equals("update")) {
- System.out.println("Insert id to update: ");
- String id = sc.nextLine();
- System.out.println("Insert what to update information or grades: ");
- String updateDemand = sc.nextLine();
- if (updateDemand.equals("information")) {
- System.out.println("Type what information to update name,age or hometown: ");
- String whatToUpdate = sc.nextLine();
- String updateInfo = "";
- if (whatToUpdate.equals("name")) {
- System.out.println("Type first and last name separated with white space: ");
- updateInfo = sc.nextLine();
- } else if (whatToUpdate.equals("age")) {
- System.out.println("Type new age: ");
- updateInfo = sc.nextLine();
- } else if (whatToUpdate.equals("hometown")) {
- System.out.println("Type new hometown: ");
- updateInfo = sc.nextLine();
- } else {
- System.out.println("There is no such field !");
- }
- updateById(id, "info", whatToUpdate + " " + updateInfo);
- } else if (updateDemand.equals("grades")) {
- System.out.println("Type what subject and grade to update in format (subject grade):");
- command = sc.nextLine();
- updateById(id, "grades", command);
- } else {
- System.out.println("You can only update student information and grades");
- }
- } else {
- System.out.println("Wrong command try again !");
- }
- System.out.println("---------------------------");
- System.out.println("Insert one of these commands (add student,add grades, search, delete, update, exit): ");
- }
- // insertStudent("Stefka Koynova" , 50 , "Ruse" );
- // insertGradesById("3", "Music 6.00");
- // searchByIdOrName("Desislava Koynova", "full name");
- // searchByIdOrName("2", "id");
- // deleteById("5");
- // updateById("2", "grades", "Math 5.59");
- // updateById("2", "grades", "Science 5.59");
- // updateById("2", "info", "name Kalin Vesov");
- // updateById("2", "info", "age 20");
- // updateById("1", "info", "hometown Sofia");
- }
- private static void insertStudent(String fullName, String age, String homeTown) {
- try (BufferedWriter bw = new BufferedWriter(new FileWriter(studentsFile, true))) {
- int studentId;
- BufferedReader br = new BufferedReader(new FileReader(studentsFile));
- String firstLine = br.readLine();
- boolean noStudents = firstLine == null || firstLine.equals("");
- if (noStudents) {
- studentId = 1;
- } else {
- String line;
- String last = firstLine;
- while ((line = br.readLine()) != null && !line.equals("")) {
- last = line;
- }
- studentId = Integer.parseInt(last.split(",")[0]) + 1;
- }
- br.close();
- String firstName = fullName.split(" ")[0];
- String lastName = fullName.split(" ")[1];
- String currentStudent = String.format("%d,%s,%s,%s,%s"
- , studentId, firstName, lastName, age, homeTown);
- PrintWriter pw = new PrintWriter(bw, true);
- pw.println(currentStudent);
- System.out.println("Insert completed !");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void insertGradesById(String studentId, String grade) {
- BufferedReader br;
- try {
- br = new BufferedReader(new FileReader(studentsFile));
- boolean studentExist = false;
- String line;
- while ((line = br.readLine()) != null) {
- String id = line.split(",")[0];
- if (studentId.equals(id)) {
- studentExist = true;
- break;
- }
- }
- if (studentExist) {
- br = new BufferedReader(new FileReader(gradesFile));
- String currentLine;
- boolean gradesLineExist = false;
- while ((currentLine = br.readLine()) != null) {
- String id = currentLine.split(",")[0];
- if (studentId.equals(id)) {
- gradesLineExist = true;
- break;
- }
- }
- if (gradesLineExist) {
- Map<String, ArrayList<String>> currentGrades = new LinkedHashMap<>();
- String[] gradesArgs = currentLine.split(",");
- for (int i = 1; i < gradesArgs.length; i++) {
- String[] subjectAndGrades = gradesArgs[i].split(" ");
- String subject = subjectAndGrades[0];
- if (!currentGrades.containsKey(subject)) {
- currentGrades.put(subject, new ArrayList<>());
- }
- for (int j = 1; j < subjectAndGrades.length; j++) {
- currentGrades.get(subject).add(subjectAndGrades[j]);
- }
- }
- if (!currentGrades.containsKey(grade.split(" ")[0])) {
- currentGrades.put(grade.split(" ")[0], new ArrayList<>());
- }
- currentGrades.get(grade.split(" ")[0]).add(grade.split(" ")[1]);
- StringBuilder inputGrades = new StringBuilder();
- inputGrades = inputGrades.append(studentId).append(",");
- for (Map.Entry subject : currentGrades.entrySet()) {
- inputGrades = inputGrades.append(subject.getKey()).append(" ");
- for (String objectGrade : (ArrayList<String>) subject.getValue()) {
- inputGrades = inputGrades.append(objectGrade).append(" ");
- }
- String trim = inputGrades.toString().trim();
- inputGrades = new StringBuilder(trim);
- inputGrades = inputGrades.append(",");
- }
- inputGrades = inputGrades.deleteCharAt(inputGrades.length() - 1);
- br = new BufferedReader(new FileReader(gradesFile));
- String readLine;
- List<String> allGrades = new ArrayList<>();
- while ((readLine = br.readLine()) != null) {
- String id = readLine.split(",")[0];
- if (studentId.equals(id)) {
- allGrades.add(inputGrades.toString());
- } else {
- allGrades.add(readLine);
- }
- }
- Collections.sort(allGrades);
- BufferedWriter bw = new BufferedWriter(new FileWriter(gradesFile, false));
- PrintWriter pw = new PrintWriter(bw, true);
- allGrades.forEach(pw::println);
- bw.close();
- } else {
- BufferedWriter bw = new BufferedWriter(new FileWriter(gradesFile, true));
- PrintWriter pw = new PrintWriter(bw, true);
- String output = String.format("%s,%s", studentId, grade);
- pw.println(output);
- bw.close();
- }
- System.out.println("Insert completed !");
- br.close();
- } else {
- System.out.println("Student does not exist");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void searchByIdOrName(String searchWord, String searchTerm) {
- String firstName = "";
- String lastName = "";
- String searchedId = "";
- if (searchTerm.equals("full name")) {
- firstName = searchWord.split(" ")[0];
- lastName = searchWord.split(" ")[1];
- } else {
- searchedId = searchWord;
- }
- try {
- BufferedReader br = new BufferedReader(new FileReader(studentsFile));
- String line;
- String[] searchedStudent = new String[0];
- boolean studentExist = false;
- while ((line = br.readLine()) != null) {
- String[] lineArgs = line.split(",");
- if ((lineArgs[1].toLowerCase().equals(firstName.toLowerCase())
- && lineArgs[2].toLowerCase().equals(lastName.toLowerCase())
- && searchTerm.equals("full name"))
- || lineArgs[0].equals(searchedId) && searchTerm.equals("id")) {
- studentExist = true;
- searchedStudent = lineArgs;
- break;
- }
- }
- if (studentExist) {
- System.out.printf("%s %s (age: %s, town: %s)\n"
- , searchedStudent[1], searchedStudent[2], searchedStudent[3], searchedStudent[4]);
- br = new BufferedReader(new FileReader(gradesFile));
- String gradesLine;
- String[] searchedGrades = new String[0];
- boolean gradesExist = false;
- while ((gradesLine = br.readLine()) != null) {
- if (gradesLine.split(",")[0].equals(searchedStudent[0])) {
- searchedGrades = gradesLine.split(",");
- gradesExist = true;
- break;
- }
- }
- if (gradesExist) {
- for (int i = 1; i < searchedGrades.length; i++) {
- String[] grades = searchedGrades[i].split(" ");
- StringBuilder sb = new StringBuilder();
- sb = sb.append(grades[0]).append(": ");
- for (int j = 1; j < grades.length; j++) {
- sb = sb.append(grades[j]).append(", ");
- }
- sb = sb.delete(sb.length() - 2, sb.length());
- System.out.println("# " + sb.toString());
- }
- } else {
- System.out.println("There is no grades for this student");
- }
- } else {
- System.out.println("Student does not exist");
- }
- br.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void deleteById(String studentId) {
- BufferedReader br;
- try {
- br = new BufferedReader(new FileReader(studentsFile));
- String line;
- StringBuilder newStudents = new StringBuilder();
- boolean studentExist = false;
- while ((line = br.readLine()) != null) {
- if (!line.split(",")[0].equals(studentId)) {
- newStudents = newStudents.append(line).append(System.lineSeparator());
- } else {
- studentExist = true;
- }
- }
- newStudents = newStudents.delete(newStudents.length() - 2, newStudents.length());
- if (studentExist) {
- BufferedWriter bw = new BufferedWriter(new FileWriter(studentsFile));
- PrintWriter pw = new PrintWriter(bw, true);
- pw.println(newStudents);
- br = new BufferedReader(new FileReader(gradesFile));
- StringBuilder newGrades = new StringBuilder();
- while ((line = br.readLine()) != null) {
- if (!line.split(",")[0].equals(studentId)) {
- newGrades = newGrades.append(line).append(System.lineSeparator());
- }
- }
- newGrades = newGrades.delete(newGrades.length() - 2, newGrades.length());
- bw = new BufferedWriter(new FileWriter(gradesFile));
- pw = new PrintWriter(bw, true);
- pw.println(newGrades);
- System.out.println("Delete completed !");
- bw.close();
- } else {
- System.out.println("Student does not exist");
- }
- br.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void updateById(String studentId, String whatToUpdate, String update) {
- if (whatToUpdate.equals("grades")) {
- insertGradesById(studentId, update);
- } else {
- BufferedReader br;
- try {
- br = new BufferedReader(new FileReader(studentsFile));
- String line;
- StringBuilder newStudents = new StringBuilder();
- boolean studentExist = false;
- while ((line = br.readLine()) != null) {
- String[] lineArgs = line.split(",");
- if (!lineArgs[0].equals(studentId)) {
- newStudents = newStudents.append(line).append(System.lineSeparator());
- } else {
- String toUpdate = update.split(" ")[0];
- if (toUpdate.equals("name")) {
- String firstName = update.split(" ")[1];
- String lastName = update.split(" ")[2];
- String updatedLine = String.format("%s,%s,%s,%s,%s"
- , lineArgs[0], firstName, lastName, lineArgs[3], lineArgs[4]);
- newStudents = newStudents.append(updatedLine).append(System.lineSeparator());
- } else if (toUpdate.equals("age")) {
- String age = update.split(" ")[1];
- String updatedLine = String.format("%s,%s,%s,%s,%s"
- , lineArgs[0], lineArgs[1], lineArgs[2], age, lineArgs[4]);
- newStudents = newStudents.append(updatedLine).append(System.lineSeparator());
- } else if (toUpdate.equals("hometown")) {
- String hometown = update.split(" ")[1];
- String updatedLine = String.format("%s,%s,%s,%s,%s"
- , lineArgs[0], lineArgs[1], lineArgs[2], lineArgs[3], hometown);
- newStudents = newStudents.append(updatedLine).append(System.lineSeparator());
- }
- studentExist = true;
- }
- }
- newStudents = newStudents.delete(newStudents.length() - 2, newStudents.length());
- if (studentExist) {
- BufferedWriter bw = new BufferedWriter(new FileWriter(studentsFile));
- PrintWriter pw = new PrintWriter(bw, true);
- pw.println(newStudents);
- System.out.println("Update completed !");
- bw.close();
- } else {
- System.out.println("Student does not exist");
- }
- br.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement