<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Java實現簡易學籍管理系統的具體程式碼,供大家參考,具體內容如下
1、我們先來講講什麼是泛型化程式設計
package 學生管理系統; import java.util.ArrayList; import java.util.List; /* * 進行泛型化程式設計 * @autor:pcb * */ public class TestGeneric { //進行泛型化程式設計,規定型別。 public List <CourseClass> courses; public TestGeneric() { this.courses = new ArrayList<CourseClass>(); } public void testAdd() { CourseClass cr1 = new CourseClass("1","大學語文"); courses.add(cr1); //泛型集合中不能新增規定意外的物件 //courses.add("能否新增一些奇怪的東西"); CourseClass cr2 = new CourseClass("2","Java基礎"); courses.add(cr2); } public void testForEach() { //進行泛型之後,可直接利用它規定的型別及其子型別直接進行存取 for(CourseClass cr:courses) { System.out.println(cr.id+":"+cr.name); } } //泛型集合可以新增泛型的子型別的物件範例 public void testChild() { ChildCourse ccr = new ChildCourse(); ccr.id = "3"; ccr.name = "我是子類的課程範例物件"; courses.add(ccr); } //泛型必須使用包裝型別進行建立,例如:int和integer public void testBaic() { List<Integer> list = new ArrayList<Integer>(); list.add(1); System.out.println("基本型別必須使用包裝類作為泛型!"+list.get(0)); } public static void main(String[] args) { TestGeneric tg = new TestGeneric(); tg.testAdd(); tg.testForEach(); tg.testChild(); tg.testForEach(); tg.testBaic(); //tg.testForEach(); } }
2、我們開始進行管理系統的建立,建立一個學生類和一個課程類
package 學生管理系統; import java.util.HashSet; import java.util.Set; public class StudentsClass implements Comparable <StudentsClass> { public String id; public String name; public Set<CourseClass> courses; public StudentsClass(String id,String name){ this.id=id; this.name=name; this.courses=new HashSet<CourseClass>(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof StudentsClass)) return false; StudentsClass other = (StudentsClass) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public int compareTo(StudentsClass o) { return this.id.compareTo(o.id); } } package 學生管理系統; public class CourseClass { public String id; public String name; public CourseClass(String id,String name){ this.id=id; this.name=name; } public CourseClass() { } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof CourseClass)) return false; CourseClass other = (CourseClass) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
3、利用List進行增加課程,刪除課程,修改課程等的操作
package 學生管理系統; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ListTest { //建立 public List coursesToSelect; //構造器 public ListTest(){ this.coursesToSelect = new ArrayList(); } public void testAdd(){ //加課 CourseClass cr1 = new CourseClass("1","資料結構"); coursesToSelect.add(cr1); CourseClass temp = (CourseClass)coursesToSelect.get(0); System.out.println("新增了課程:"+temp.id+":"+temp.name); CourseClass cr2 = new CourseClass("2","c語言"); coursesToSelect.add(0, cr2); CourseClass temp2 = (CourseClass)coursesToSelect.get(0); System.out.println("新增了課程:"+temp2.id+":"+temp2.name); CourseClass[] Course={new CourseClass("3","離散數學"),new CourseClass("4","組合語言")}; coursesToSelect.addAll(Arrays.asList(Course)); CourseClass temp3 = (CourseClass)coursesToSelect.get(2); CourseClass temp4 = (CourseClass)coursesToSelect.get(3); System.out.println("新增了兩門課程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name); CourseClass[] Course1= {new CourseClass("5","高等數學"),new CourseClass("6","大學英語")}; coursesToSelect.addAll(2, Arrays.asList(Course1)); CourseClass temp5 = (CourseClass)coursesToSelect.get(2); CourseClass temp6 = (CourseClass)coursesToSelect.get(3); System.out.println("新增了兩門課程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name); } public void test() { int size = coursesToSelect.size(); for(int i=0;i<size;i++) { //進行強制型別轉化 CourseClass cr = (CourseClass)coursesToSelect.get(i); System.out.println("課程"+i+":"+cr.id+":"+cr.name); } } //迭代器輸入方法 public void testIterator() { Iterator it = coursesToSelect.iterator(); System.out.println("可選擇的課程(通過迭代器存取):"); while(it.hasNext()) { CourseClass cr = (CourseClass) it.next(); System.out.println("課程"+":"+cr.id+":"+cr.name); } } //利用foreach語句進行遍歷 public void testForEach() { System.out.println("可選擇的課程(通過FOREACH存取):"); for(Object obj:coursesToSelect) { CourseClass cr = (CourseClass) obj; System.out.println("課程"+":"+cr.id+":"+cr.name); } } //進行修改操作 public void testModify() { coursesToSelect.set(4, new CourseClass("7","毛概")); } //利用物件進行刪除操作 public void testRmove1() { System.out.println("通過物件進行刪除"); CourseClass cr = (CourseClass) coursesToSelect.get(4); System.out.println("我是課程:"+cr.id+":"+cr.name); coursesToSelect.remove(cr); System.out.println("成功刪除課程!!"); testForEach(); } //利用索引值進行刪除操作 public void testRmove2() { // CourseClass cr = (CourseClass) coursesToSelect.get(4); // System.out.println("我是課程:"+cr.id+":"+cr.name); System.out.println("通過索引值進行刪除"); coursesToSelect.remove(4); System.out.println("成功刪除課程!!"); testForEach(); } //進行刪除,刪除多位置的方法操作 public void testRmove3() { System.out.println("刪除4位元置和5位置的課程"); CourseClass [] Course={(CourseClass) coursesToSelect.get(4),(CourseClass) coursesToSelect.get(5)}; //將Course陣列轉化成一個集合傳遞進去 coursesToSelect.removeAll(Arrays.asList(Course)); System.out.println("成功刪除課程!!"); testForEach(); } public static void main(String[] args) { ListTest it = new ListTest(); it.testAdd(); it.test(); //it.testIterator(); //it.testForEach(); //it.testModify(); //it.testForEach(); //it.testRmove2(); it.testRmove3(); } }
4、利用set集合進行刪除,新增,查詢操作
package 學生管理系統; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class SetTest { //建立 public List <CourseClass> coursesToSelect; //向螢幕讀取 private Scanner console; public StudentsClass student; //進行構造 public SetTest() { coursesToSelect = new ArrayList<CourseClass>(); console = new Scanner(System.in); } public void testAdd(){ //加課 CourseClass cr1 = new CourseClass("1","資料結構"); coursesToSelect.add(cr1); CourseClass temp = (CourseClass)coursesToSelect.get(0); //System.out.println("新增了課程:"+temp.id+":"+temp.name); CourseClass cr2 = new CourseClass("2","c語言"); coursesToSelect.add(0, cr2); CourseClass temp2 = (CourseClass)coursesToSelect.get(0); //System.out.println("新增了課程:"+temp2.id+":"+temp2.name); CourseClass[] Course={new CourseClass("3","離散數學"),new CourseClass("4","組合語言")}; coursesToSelect.addAll(Arrays.asList(Course)); CourseClass temp3 = (CourseClass)coursesToSelect.get(2); CourseClass temp4 = (CourseClass)coursesToSelect.get(3); //System.out.println("新增了兩門課程:"+temp3.id+":"+temp3.name+";"+temp4.id+":"+temp4.name); CourseClass[] Course1= {new CourseClass("5","高等數學"),new CourseClass("6","大學英語")}; coursesToSelect.addAll(2, Arrays.asList(Course1)); CourseClass temp5 = (CourseClass)coursesToSelect.get(2); CourseClass temp6 = (CourseClass)coursesToSelect.get(3); //System.out.println("新增了兩門課程:"+temp5.id+":"+temp5.name+";"+temp6.id+":"+temp6.name); } public void testForEach() { System.out.println("可選擇的課程(通過FOREACH存取):"); for(Object obj:coursesToSelect) { CourseClass cr = (CourseClass) obj; System.out.println("課程"+":"+cr.id+":"+cr.name); } } //查詢是否包含此課程 public void testListContains() { CourseClass course = coursesToSelect.get(0); System.out.println("取得課程:"+course.name); System.out.println("備選課程是否包含課程:"+course.name+"," +coursesToSelect.contains(course)); System.out.println("請輸入課程名稱:"); String name = console.next(); CourseClass course2 = new CourseClass(); course2.name = name; System.out.println("新建立課程:"+course2.name); System.out.println("備選課程是否包含課程:"+course2.name+"," +coursesToSelect.contains(course2)); //indexof來求出索引 if(coursesToSelect.contains(course2)) { System.out.println("課程:"+course2.name+"的索引位置為:"+ coursesToSelect.indexOf(course2)); } } public void creatStudentAndSelect() { //建立一個學生物件 student = new StudentsClass("1","小明"); System.out.println("歡迎學生:"+student.name+"選課!"); //從鍵盤輸入課程 Scanner console = new Scanner(System.in); for(int i = 0;i<3;i++) { System.out.println("請輸入課程ID"); String courseId = console.next(); for(CourseClass cr :coursesToSelect) { if(cr.id.equals(courseId)) { student.courses.add(cr); student.courses.add(null); student.courses.add(cr); } } } } //測試Set的contains方法 public void testSetContains() { System.out.println("請輸入學生已選的課程名:"); String name = console.next(); CourseClass course2 = new CourseClass(); course2.name = name; System.out.println("新建立課程:"+course2.name); System.out.println("備選課程是否包含課程:"+course2.name+"," +student.courses.contains(course2)); } public static void main(String[] args) { // TODO Auto-generated method stub SetTest st = new SetTest(); st.testAdd(); st.testForEach(); st.testListContains(); // st.creatStudentAndSelect(); // st.testSetContains(); } }
5、利用Map進行增加,刪除,查詢,修改操作
package 學生管理系統; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class MapTest { public Map<String,StudentsClass>students; //利用雜湊鍵值對進行建立 public MapTest() { this.students = new HashMap<String,StudentsClass>(); } //進行學生的插入操作 public void testPut() { Scanner console = new Scanner (System.in); int i = 0; while(i<3) { System.out.println("請輸入學生ID:"); String ID = console.next(); StudentsClass st = students.get(ID); if(st == null) { System.out.println("請輸入學生姓名:"); String name = console.next(); StudentsClass newStudent = new StudentsClass(ID,name); students.put(ID,newStudent); System.out.println("成功新增學生:"+students.get(ID).name); i++; }else { System.out.println("該學生ID已經被佔用!"); continue; } } } //利用keyset方法將key值放在一個集合中,然後利用key值進行遍歷 public void testKeySet() { Set<String>keySet = students.keySet(); //進行遍歷 for(String stuID:keySet) { StudentsClass st = students.get(stuID); if(st != null) { System.out.println("學生:"+st.name); } } } //進行刪除操作 public void testRmove() { Scanner console = new Scanner(System.in); while(true) { System.out.println("請輸入要刪除的學生ID!"); String ID = console.next(); StudentsClass st = students.get(ID); if(st == null) { System.out.println("該ID不存在!"); continue; } students.remove(ID); System.out.println("成功刪除學生:"+st.name); break; } } //通過entryset來遍歷Map public void testEntrySet() { Set<Entry<String,StudentsClass>>entrySet = students.entrySet(); for(Entry<String,StudentsClass> entry:entrySet) { System.out.println("取得鍵:"+entry.getKey()); System.out.println("對應的值為:"+entry.getValue().name); } } //進行修改操作 public void testModify() { System.out.println("請輸入要修改的學生ID:"); Scanner console = new Scanner(System.in); while(true) { String stuID = console.next(); StudentsClass student = students.get(stuID); if(student == null) { System.out.println("該ID不存在!請重新輸入!"); continue; } System.out.println("當前學生ID,所對應的學生為:"+student.name); System.out.println("請輸入新的學生姓名:"); String name = console.next(); StudentsClass newStudent = new StudentsClass(stuID,name); students.put(stuID,newStudent); System.out.println("修改成功"); } } //通過鍵值或值進行查詢操作 public void testContainsKeyOrValue() { System.out.println("請輸入要查詢的學生ID:"); Scanner console = new Scanner(System.in); String id = console.next(); System.out.println("您輸入的學生ID為:"+id+"在學生對映表中是否存在:"+ students.containsKey(id) ); if(students.containsKey(id)) { System.out.println("對應的學生為:"+students.get(id).name); } System.out.println("請輸入要查詢的學生姓名:"); String name = console.next(); if(students.containsValue(new StudentsClass(null,name))) System.out.println("在學生對映表中確實包含學生:"+name); else System.out.println("在學生對映表中不存在該學生"); } public static void main(String[] args) { // TODO Auto-generated method stub MapTest mt = new MapTest(); mt.testPut(); mt.testKeySet(); // mt.testRmove(); // mt.testEntrySet(); // mt.testModify(); // mt.testEntrySet(); mt.testContainsKeyOrValue(); } }
6、進行過載比較
package 學生管理系統; import java.util.Comparator; public class StudentComparator implements Comparator<StudentsClass> { @Override public int compare(StudentsClass o1, StudentsClass o2) { // TODO Auto-generated method stub return o1.name.compareTo(o2.name); } }
7、進行排序操作
package 學生管理系統; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class CollectionTest { //利用Collection內部方法進行排序操作 public void testSort1() { //進行整形排序 List<Integer>intergerList = new ArrayList<Integer>(); Random random = new Random(); Integer k; for(int i = 0;i < 10; i++) { do { k = random.nextInt(100); }while(intergerList.contains(k)); intergerList.add(k); System.out.println("成功新增整數:"+k); System.out.println("--------排序前-------"); for(Integer integer:intergerList) { System.out.println("元素:"+integer); } Collections.sort(intergerList); System.out.println("--------排序後-------"); for(Integer integer:intergerList) System.out.println("元素:"+integer); } } //進行String排序 public void testSort2() { List<String>stringList = new ArrayList<String>(); stringList.add("microsoft"); stringList.add("google"); stringList.add("lenovo"); System.out.println("--------排序前-------"); for(String string : stringList) { System.out.println("元素:"+string); } Collections.sort(stringList); System.out.println("--------排序後-------"); for(String string : stringList) { System.out.println("元素:"+string); } } //對StudentsClass類進行排序,這裡就需要進行equal和hashcode的過載 public void testSort3() { List<StudentsClass>StudentList = new ArrayList<StudentsClass>(); Random random = new Random(); StudentList.add(new StudentsClass(random.nextInt(1000)+"","mike")); StudentList.add(new StudentsClass(random.nextInt(1000)+"","lucy")); StudentList.add(new StudentsClass(random.nextInt(1000)+"","angel")); System.out.println("--------排序前-------"); for(StudentsClass student:StudentList) { System.out.println("學生:"+student.id+student.name); } Collections.sort(StudentList); System.out.println("--------排序後-------"); for(StudentsClass student:StudentList) { System.out.println("學生:"+student.id+student.name); } Collections.sort(StudentList,new StudentComparator()); System.out.println("----------按照姓名排序----------"); for(StudentsClass student:StudentList) { System.out.println("學生:"+student.id+student.name); } } public static void main(String[] args) { // TODO Auto-generated method stub CollectionTest ct = new CollectionTest(); //ct.testSort1(); //ct.testSort2(); ct.testSort3(); } }
8、輔助類。
package 學生管理系統; public class ChildCourse extends CourseClass { }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45