1 //利用順序表完成一個學生成績管理系統 2 package classwork_5; 3 4 public class student { 5 private String name;//名字 6 private int id;//學號 7 private String state;//是否在籍 8 private double gpa;//在校績點成績 9 public student() { 10 11 } 12 public student(String name, int id, String state, double gpa) { 13 this.name = name; 14 this.id = id; 15 this.state = state; 16 this.gpa = gpa; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public int getId() { 25 return id; 26 } 27 public void setId(int id) { 28 this.id = id; 29 } 30 public String getState() { 31 return state; 32 } 33 public void setState(String state) { 34 this.state = state; 35 } 36 public double getGpa() { 37 return gpa; 38 } 39 public void setGpa(double d) { 40 this.gpa = d; 41 } 42 @Override 43 public String toString() { 44 return "" + name + "\t\t" + id + "\t\t" + state + "\t\t" + gpa; 45 } 46 47 } 48 49 50 package classwork9; 51 52 import java.util.ArrayList; 53 import java.util.List; 54 import java.util.Scanner; 55 56 import classwork_5.student; 57 58 public class stumanager { 59 private Scanner in = new Scanner(System.in); 60 private List<student> stu = new ArrayList<student>(); 61 62 // 初始化,先往學生管理系統裏面存入一些學生信息 63 public void init() { 64 student s1 = new student("小明", 2019216588, "在籍", 4.4); 65 student s2 = new student("小紅", 2019219888, "在籍", 1.2); 66 student s3 = new student("小剛", 2019212188, "在籍", 3.4); 67 stu.add(s1); 68 stu.add(s2); 69 stu.add(s3); 70 } 71 72 public void menu() { 73 System.out.println("*******歡迎使用學生管理系統*******"); 74 System.out.println("請根據提示選擇"); 75 System.out.println("1.添加學生信息"); 76 System.out.println("2.刪除學生信息"); 77 System.out.println("3.修改學生信息"); 78 System.out.println("4.查詢學生並輸出"); 79 System.out.println("5.全部學生信息"); 80 81 int choice = in.nextInt(); 82 switch (choice) { 83 case 1: 84 addstu(); 85 break; 86 case 2: 87 delstu(); 88 break; 89 case 3: 90 amendstu(); 91 break; 92 case 4: 93 findstu(); 94 break; 95 case 5: 96 show(); 97 break; 98 default: 99 } 100 } 101 102 public void iscontinuemenu() { 103 System.out.println("是否返回到主菜單(y/n)"); 104 String iscon = in.next(); 105 if ("y".equals(iscon)) { 106 menu(); 107 } 108 } 109 110 private void addstu() { 111 System.out.println("增長學生信息"); 112 System.out.println("請輸入學生名"); 113 String name = in.next(); 114 System.out.println("請輸入學生學號"); 115 int id = in.nextInt(); 116 System.out.println("請輸入學生的入學年份"); 117 int year = in.nextInt(); 118 String y = ""; 119 if (year <= 2020 && year >= 2017) { 120 y = "在籍"; 121 } else { 122 y = "不在籍"; 123 } 124 System.out.println("請輸入學生績點"); 125 double gpa = in.nextDouble(); 126 student st = new student(name, id, y, gpa); 127 stu.add(st); 128 System.out.println("已添加完畢請返回主菜單查看"); 129 iscontinuemenu(); 130 } 131 132 // 刪除學生信息,根據學號刪除 133 private void delstu() { 134 System.out.println("請輸入要刪除的學號"); 135 int a = in.nextInt(); 136 for (int loop = 0; loop < stu.size(); loop++) { 137 if (((student) stu.get(loop)).getId() == a) { 138 stu.remove(loop); 139 System.out.println("刪除成功!"); 140 break; 141 } 142 } 143 144 System.out.println("已刪除完畢請返回主菜單查看"); 145 iscontinuemenu(); 146 } 147 148 // 修改學生信息amend 149 150 private void amendstu() { 151 System.out.println("請輸入要修改學生信息的學號"); 152 int a = in.nextInt(); 153 int loop; 154 for (loop = 0; loop < stu.size(); loop++) { 155 if (((student) stu.get(loop)).getId() == a) { 156 break; 157 } 158 } 159 160 System.out.println("請輸入新名字,學號,績點"); 161 String name1 = in.next(); 162 int id = in.nextInt(); 163 double gpa = in.nextDouble(); 164 ((student) stu.get(loop)).setName(name1); 165 ((student) stu.get(loop)).setId(id); 166 ((student) stu.get(loop)).setGpa(gpa); 167 System.out.println("已修改完畢請返回主菜單查看"); 168 iscontinuemenu(); 169 } 170 171 // 查詢學生信息,根據學號查詢 172 private void findstu() { 173 System.out.println("請輸入要查詢學生的學號"); 174 175 int a = in.nextInt(); 176 int loop; 177 for (loop = 0; loop < stu.size(); loop++) { 178 if (((student) stu.get(loop)).getId() == a) { 179 System.out.println(((student) stu.get(loop))); 180 break; 181 } 182 } 183 iscontinuemenu(); 184 185 } 186 187 // 查看全部學生信息 188 private void show() { 189 System.out.println("姓名\t\t學號\t\t\t在籍狀態\t\t在校成績"); 190 191 for (student x : stu) { 192 System.out.print(x + " "); 193 System.out.println(); 194 } 195 iscontinuemenu(); 196 } 197 198 } 199 200 package classwork9; 201 202 public class stuman_test { 203 204 public static void main(String[] args) { 205 stumanager a=new stumanager(); 206 a.init(); 207 a.menu(); 208 209 } 210 211 }