學生資訊管理系統(ArrayLIST的使用)
阿新 • • 發佈:2022-05-23
- 要點:
- 類的建立與賦值
- ArrayList儲存類的內容
- 遍歷/查詢/刪除類中的資料
- 類的建立
package CAK1; public class Student { private String name; private String age; private String sno; public Student(String name, String age, String sno) { this.name = name; this.age = age; this.sno = sno; }public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSno() { return sno; }public void setSno(String sno) { this.sno = sno; } }
- 主函式
package CAK1; import com.sun.xml.internal.messaging.saaj.soap.FastInfosetDataContentHandler; import java.sql.Struct; import java.util.ArrayList; import java.util.Scanner; public class StudentDemo { public static void main(String[] args) { ArrayList<Student> array=new ArrayList<Student>(); boolean ct = true; while(ct){ zhuye(); Scanner sc = new Scanner(System.in); String line = sc.nextLine(); switch (line) { case "1" : System.out.println("1.新增學生"); addStudent(array); break; case "2": System.out.println("2.刪除學生"); delStudent(array); break; case "3": System.out.println("3.修改學生"); fixStudent(array); break; case "4": System.out.println("4.檢視學生列表"); findStudent(array); break; case "5": System.out.println("謝謝使用"); ct = false; break;//System.exit( 0 );//break; } } } public static void zhuye(){ System.out.println("-----------學生資訊管理系統----------"); System.out.println("1.新增學生"); System.out.println("2.刪除學生"); System.out.println("3.修改學生"); System.out.println("4.查詢學生"); System.out.println("5.退出"); System.out.println("請輸入選擇序號:"); } public static void addStudent(ArrayList<Student> array){ Scanner sc = new Scanner(System.in); System.out.println("輸入學生姓名"); String name = sc.nextLine(); System.out.println("輸入學生年齡"); String age = sc.nextLine(); System.out.println("輸入學生學號"); String sno = sc.nextLine(); Student s = new Student(name,age,sno); array.add(s); System.out.println("新增成功"); } public static void delStudent(ArrayList<Student> array){ boolean nostu = false; Scanner sc = new Scanner(System.in); System.out.println("輸入刪除的學生學號"); String sno = sc.nextLine(); for(int i=0 ;i< array.size();i++){ Student s = array.get(i); if(s.getSno().equals(sno)){//字串比較用equals array.remove(i); if(i == (array.size()-1)){ nostu = true; } break; } } if(nostu){ System.out.println("查無此號"); } else{ System.out.println("刪除成功"); } } public static void fixStudent(ArrayList<Student> array){ boolean nostu = true; System.out.println("請輸入該生的學號"); Scanner sc = new Scanner(System.in); String sno = sc.nextLine(); for(int i=0;i< array.size();i++){ Student s =array.get(i); if(s.getSno().equals(sno)){ nostu = false; array.remove(i); System.out.println("輸入新姓名"); String name = sc.nextLine(); System.out.println("輸入年齡"); String age = sc.nextLine(); Student s2 = new Student(name,age,sno); array.add(s2); System.out.println("修改成功"); } } if(nostu){ System.out.println("查無此號"); } } public static void findStudent(ArrayList<Student> array){ if(array.size() == 0){ System.out.println("無資訊"); } else { System.out.println("姓名 年齡 學號"); for(int i = 0;i<array.size();i++){ Student s = array.get(i); System.out.println(s.getName() +"\t" + s.getAge() + "歲\t" + s.getSno()); } } } }