0
1
テーマ、知りたいこと
java初心者です。
Mapを練習する過程で作ったコードが以下になります。
本職の方から見れば冗長だったり可読性が低いと思うので、改善案があれば教えて下さい。
背景、状況
chatGPTに出された問題の回答として作りました。以下問題文。
問題 4: 学生の年齢と成績を管理
問題: 学生IDをキーとして、名前と複数の成績を管理するプログラムを作成しなさい。
- 学生IDをキーとし、名前と複数の成績を持つクラス(オブジェクト)を値とする構造にする。
- 学生情報を追加する。
- 学生IDを指定してその名前と成績を表示する。
- すべての学生の情報を表示する。
コード
java
1import java.util.*; 2 3class Front{ 4 private StudentsScore studentsScore; 5 private Scanner sc; 6 7 Front(){ 8 studentsScore = new StudentsScore(); 9 sc = new Scanner(System.in); 10 } 11 12 public void add(){ 13 System.out.println("Put name : "); 14 String name = sc.nextLine(); 15 int id = makeID(); 16 17 while(studentsScore.getStudentsScore().containsKey(id)){ 18 id = makeID(); 19 } 20 21 System.out.println("The name is " + name + ". \nYour ID is " + id +". \nHow many subject do you add? "); 22 23 int numOfScore = 0; 24 try{ 25 numOfScore = sc.nextInt(); 26 String blank = sc.nextLine(); 27 }catch(Exception e){ 28 System.out.println("Invalid number"); 29 } 30 31 StudentScore studentScore = new StudentScore(name); 32 for(int i = 0; i < numOfScore; i++){ 33 System.out.println("what's subject?"); 34 String sub = sc.nextLine(); 35 System.out.println("what's score ?"); 36 int score = -1; 37 try{ 38 score = sc.nextInt(); 39 }catch(Exception e){ 40 System.out.println("Invalid number"); 41 } 42 43 if(score >= 0) { 44 studentScore.setScore(sub,score); 45 System.out.println(studentScore.getScore()); 46 String blank = sc.nextLine(); 47 } 48 49 else{ 50 System.out.println("try again? y/n"); 51 String ans = sc.next(); 52 String blank = sc.nextLine(); 53 54 if(ans.equals("y")) numOfScore++; 55 else numOfScore = 0; 56 } 57 } 58 if(!studentScore.getScore().isEmpty()) studentsScore.makeStudent(id,studentScore); 59 } 60 61 public void delete(){ 62 System.out.println("type id: "); 63 int id = -1; 64 try{ 65 id = sc.nextInt(); 66 String blank = sc.nextLine(); 67 }catch(Exception e){ 68 System.out.println("Invalid num"); 69 } 70 71 if(id >= 0){ 72 StudentScore studentScore = studentsScore.getStudentsScore().get(id); 73 System.out.println( "id is " + id + ",\nthis student is " + studentScore.getName() + ". \ndo you delete this info? y/n"); 74 String ans = sc.next(); 75 if(ans.equals("y")){ 76 studentsScore.deleteStudent(id); 77 System.out.println("Successfully deleted."); 78 }else{ 79 System.out.println("you said no -> back to menu."); 80 } 81 } 82 83 84 } 85 86 public void find(){ 87 System.out.println("type id: "); 88 int id = -1; 89 try{ 90 id = sc.nextInt(); 91 String blank = sc.nextLine(); 92 }catch(Exception e){ 93 System.out.println("Invalid num"); 94 } 95 96 if(studentsScore.getStudentsScore().containsKey(id)) { 97 StudentScore studentScore = studentsScore.getStudentsScore().get(id); 98 System.out.println("you are " + studentScore.getName() + ". \nand your id is " + id + ".\nand your score :"); 99 for(Map.Entry<String,Integer> score : studentScore.getScore().entrySet()){ 100 System.out.println(" -" + score.getKey() + " : " + score.getValue()); 101 } 102 } 103 } 104 105 public void list(){ 106 System.out.println("this is the list : "); 107 for(Map.Entry<Integer,StudentScore> score : studentsScore.getStudentsScore().entrySet()){ 108 System.out.println(" - " + score.getValue().getName() + " : " + score.getKey()); 109 } 110 } 111 112 private int makeID(){ 113 double random = Math.random() * 10000; 114 return (int) random; 115 } 116} 117 118 119class StudentsScore{ 120 private Map<Integer,StudentScore> studentsScore; 121 StudentsScore(){ 122 // instance StudentScore? 123 studentsScore = new HashMap<>(); 124 } 125 126 public void makeStudent(Integer iD,StudentScore score){ 127 studentsScore.put(iD,score); 128 } 129 130 public Map<Integer,StudentScore> getStudentsScore(){ 131 return studentsScore; 132 } 133 134 public void deleteStudent(int id){ 135 if(studentsScore.containsKey(id)) studentsScore.remove(id); 136 } 137} 138 139 class StudentScore{ 140 private final String name; 141 private final Map<String,Integer> score; 142 143 StudentScore(String name){ 144 this.name = name; 145 score = new HashMap<>(); 146 } 147 148 public String getName(){ 149 return this.name; 150 } 151 152 public void setScore(String subject,Integer score){ 153 this.score.put(subject,score); 154 155 } 156 157 public Map<String,Integer> getScore(){ 158 return score; 159 } 160 161} 162 163public class Main{ 164 public static void main(String[] args) { 165 Front front = new Front(); 166 Scanner sc = new Scanner(System.in); 167 boolean notLeave = true; 168 169 while (notLeave){ 170 System.out.println("---------------------------------\n-MENU \n -1: add student and their score \n -2: delete student info \n -3: find student based on id \n -4: list all student \n -any: end"); 171 String ans = sc.next(); 172 173 switch(ans){ 174 case "1": front.add();break; 175 case "2": front.delete();break; 176 case "3": front.find();break; 177 case "4": front.list();break; 178 default: notLeave = false; 179 } 180 } 181 } 182} 183
回答3件
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。