2016-10-20 6 views
1

私のプログラムは、学生の名前、件名、電話番号を収集してデータベースに追加する簡単な学生管理データベースです。私は、データベース内の学生の追加、削除、検索といった主要な論理操作を達成することができました。 HashMapコレクションに追加する際に、例えば「英語」、「数学」、「コンピューティング」などの承認された科目のみを入力するように学生を制限することはできません。どんな助けもありがとう。HashMapで特定のエントリを受け入れる条件を追加する

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.util.HashMap; 
import java.util.Scanner; 

public class Menu { 
    private HashMap<String, Student> students; 

    public Menu() { 
     students = new HashMap<String, Student>(); 
    } 

    private void eventLoop() { 
     Scanner scanner = new Scanner(System.in); 
     int choice = 0; 
     boolean exit = false; 
     this.readFromFile(); 

     while (!exit) { 
      System.out.println("Welcome to Student Management System"); 
      System.out.println("=============================="); 
      System.out.println("(1) Add new student"); 
      System.out.println("(2) Delete a student"); 
      System.out.println("(3) Find Student By Name"); 
      System.out.println("(4) List Students By Subject"); 
      System.out.println("(5) List All Students"); 
      System.out.println("(6) Exit System"); 
      System.out.println("Choose an option: "); 

      try { 
       choice = Integer.parseInt(scanner.nextLine()); 
       System.out.print('\u000C'); 
       if (choice < 1 || choice > 6) { 
        System.err.println("Error : Choose an option between 1 and 6"); 
        choice = 0; 
       } 
      } catch (NumberFormatException e) { 
       System.err.println("Error : Choose an option between 1 and 6"); 
       choice = 0; 
      } 

      switch (choice) { 
       case 1: 
       this.addStudent(scanner); 
       break; 
       case 2: 
       this.deleteStudent(scanner); 
       break; 
       case 3: 
       this.findStudentByName(scanner); 
       break; 
       case 4: 
       this.findStudentsBySubject(scanner); 
       break; 
       case 5: 
       this.listStudents(); 
       break; 
       case 6: 
       this.writeToFile(); 
       exit = true; 
      } 
     } 
     scanner.close(); 
    } 

    private void findStudentsBySubject(Scanner scanner) { 
     System.out.println("Enter the exact name of the subject:"); 
     String subjectStr = scanner.nextLine(); 
     boolean atleastOne = false; 
     for (String name : students.keySet()) { 
      if (students.get(name).getSubject().getName().toLowerCase().equals(subjectStr.toLowerCase())) { 
       System.out.println(students.get(name)); 
       atleastOne = true; 
      } 
     } 
     if (!atleastOne) { 
      System.err.println("No students have enrolled for this subject."); 
     } 
    } 

    private void findStudentByName(Scanner scanner) { 
     System.out.println("Enter the exact name of the Student to search:"); 
     String name = scanner.nextLine(); 
     if (students.get(name.toLowerCase()) != null) { 
      System.out.println("Student details:"); 
      System.out.println(students.get(name.toLowerCase())); 
     } else { 
      System.err.println(name + " not found in the database."); 
     } 
    } 

    private void deleteStudent(Scanner scanner) { 
     System.out.println("Enter the exact name of the Student to delete:"); 
     String name = scanner.nextLine(); 
     if (students.get(name.toLowerCase()) != null) { 
      students.remove(name.toLowerCase()); 
      System.err.println("Student " + name + " deleted from the database."); 
     } else { 
      System.err.println(name + " not found in the database."); 
     } 
    } 

    private void addStudent(Scanner scanner) { 
     System.out.println("The information should be comma separated and in a single line."); 
     System.out.println("If the name is not unique, the system will throw an error."); 
     System.out.println("Enter the name, phone and subject of the new student."); 

     String line = scanner.nextLine(); 
     System.out.print('\u000C'); 
     String[] info = line.split(","); 
     if (info.length != 3) { 
      System.err.println("Please enter the information in the proper format."); 
      return; 
     } 
     String name = info[0]; 
     String phone = info[1]; 
     String subjectStr = info[2]; 

     if (students.get(name.toLowerCase()) != null) { 
      System.err.println("This student already exists in the database."); 
      return; 
     } 
     if (phone.length() != 9) { 
      System.err.println("The phone number must contain exactly 9 digits."); 
      return; 
     } 
     if (phone.charAt(0) != '9') { 
      System.err.println("The phone number must start with '9'."); 
      return; 
     } 
     if (!phone.matches("^[0-9]*$")) { 
      System.err.println("The phone number must contain only numbers."); 
      return; 
     } 
     students.put(name.toLowerCase(), new Student(name, new Subject(subjectStr), phone)); 
     System.err.println("Student added successfully"); 
    } 

    private void listStudents() { 
     for (String name : this.students.keySet()) { 
      System.out.println(this.students.get(name)); 
     } 
    } 

    private void readFromFile() { 
     try { 
      BufferedReader br = new BufferedReader(new FileReader(new File("./students.txt"))); 
      String line; 
      while ((line = br.readLine()) != null) { 
       String[] info = line.split(","); 
       String name = info[0]; 
       String phone = info[1]; 
       String subjectName = info[2]; 

       if (students.get(name.toLowerCase()) == null) { 
        Subject subject = new Subject(subjectName); 
        students.put(name.toLowerCase(), new Student(name, subject, phone)); 
       } else { 
        System.err.println("There seems to be a duplicate student in the file."); 
       } 
      } 
      br.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void writeToFile() { 
     try { 
      BufferedWriter bw = new BufferedWriter(new FileWriter(new File("./students.txt"))); 
      for (String name : students.keySet()) { 
       bw.write(students.get(name).toString()); 
       bw.newLine(); 
      } 
      bw.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     Menu menu = new Menu(); 
     menu.eventLoop(); 
    } 

} 

Subjectクラス:

streamを使用して
public class Subject { 
    private String name; 
    public Subject(String subjectName) { 
     this.setName(subjectName); 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String toString() { 
     return this.getName(); 
    } 
} 

答えて

1

あなたが可能な値の制限されたリストを持っている場合は、あなたが列挙型などを使用することができます

enum Subject { 
    English, Maths, Computing 
} 

これらのコレクションは、ちょうどEnumSetのであり、あなたはそれがあなたの入力のための

EnumSet<Subject> subjects = EnumSet.of(Subject.class); 

Subject s = Subject.valueOf(subjectName); 
subjects.add(s); 
+0

入力いただきありがとうございます。この私をSubjectクラスにどのように追加するのですか?私は自分の質問を更新し、すでに持っている科目を追加しました。 –

1

filterあなたが最もαで得ようとしていることを確認している場合は、簡単にこの

    students.entrySet() 
         .stream() 
         .filter(.. some predicate...) 
         .map(Map.Entry::getValue) 
         .collect(Collectors.toList()) 

を達成することができますここでの主なコードですフィルタを通過した単一の要素(フィルタによって保証されています)は、findFirstを使用できます。

フィルタは、複数のリストと一致する可能性がある場合一般的なケースでは210
Optional<List> o = students.entrySet() 
         .stream() 
         .filter(e -> e.getKey() == 1) 
         .map(Map.Entry::getValue) 
         .findFirst(); 

は、あなたはリストのリストにそれらを収集することができます。

List<List> list = students.entrySet() 
        .stream() 
        .filter(.. some predicate...) 
        .map(Map.Entry::getValue) 
        .collect(Collectors.toList()); 
+0

感謝を呼び出すことにより、値の確認することができます。あなたが私の質問で答えを編集して、私がそれをよりよく理解できるようにしてください。 –

関連する問題