您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Java怎么實現(xiàn)簡易學籍管理系統(tǒng)的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內容如下
1、我們先來講講什么是泛型化編程
package 學生管理系統(tǒng); import java.util.ArrayList; import java.util.List; /* * 進行泛型化編程 * @autor:pcb * */ public class TestGeneric { //進行泛型化編程,規(guī)定類型。 public List <CourseClass> courses; public TestGeneric() { this.courses = new ArrayList<CourseClass>(); } public void testAdd() { CourseClass cr1 = new CourseClass("1","大學語文"); courses.add(cr1); //泛型集合中不能添加規(guī)定意外的對象 //courses.add("能否添加一些奇怪的東西"); CourseClass cr2 = new CourseClass("2","Java基礎"); courses.add(cr2); } public void testForEach() { //進行泛型之后,可直接利用它規(guī)定的類型及其子類型直接進行訪問 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); } //泛型必須使用包裝類型進行創(chuàng)建,例如: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、我們開始進行管理系統(tǒng)的創(chuàng)建,創(chuàng)建一個學生類和一個課程類
package 學生管理系統(tǒng); 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 學生管理系統(tǒng); 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 學生管理系統(tǒng); import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ListTest { //創(chuàng)建 public List coursesToSelect; //構造器 public ListTest(){ this.coursesToSelect = new ArrayList(); } public void testAdd(){ //加課 CourseClass cr1 = new CourseClass("1","數(shù)據(jù)結構"); 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","離散數(shù)學"),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","高等數(shù)學"),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("成功刪除課程?。?quot;); 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數(shù)組轉化成一個集合傳遞進去 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 學生管理系統(tǒng); import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class SetTest { //創(chuàng)建 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","數(shù)據(jù)結構"); 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","離散數(shù)學"),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","高等數(shù)學"),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("新創(chuàng)建課程:"+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() { //創(chuàng)建一個學生對象 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("新創(chuàng)建課程:"+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 學生管理系統(tǒng); 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; //利用哈希鍵值對進行創(chuàng)建 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已經(jīng)被占用!"); 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 學生管理系統(tǒng); 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 學生管理系統(tǒng); 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("成功添加整數(shù):"+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 學生管理系統(tǒng); public class ChildCourse extends CourseClass { }
感謝各位的閱讀!關于“Java怎么實現(xiàn)簡易學籍管理系統(tǒng)”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。