共计 12497 个字符,预计需要花费 32 分钟才能阅读完成。
导读 | 这篇文章主要为大家详细介绍了 Java 实现简易学籍管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 |
本文实例为大家分享了 Java 实现简易学籍管理系统的具体代码,供大家参考,具体内容如下
1、我们先来讲讲什么是泛型化编程
package 学生管理系统;
import java.util.ArrayList;
import java.util.List;
/*
* 进行泛型化编程
* @autor:pcb
* */
public class TestGeneric {
// 进行泛型化编程,规定类型。public List courses;
public TestGeneric() {this.courses = new ArrayList ();}
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 list = new ArrayList ();
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 {
public String id;
public String name;
public Set courses;
public StudentsClass(String id,String name){
this.id=id;
this.name=name;
this.courses=new HashSet ();}
@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 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 coursesToSelect;
// 向屏幕读取
private Scanner console;
public StudentsClass student;
// 进行构造
public SetTest() {coursesToSelect = new ArrayList ();
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 Mapstudents;
// 利用哈希键值对进行创建
public MapTest() {this.students = new HashMap();}
// 进行学生的插入操作
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() {SetkeySet = 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>entrySet = students.entrySet();
for(Entry 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 {
@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() {
// 进行整形排序
ListintergerList = new ArrayList ();
Random random = new Random();
Integer k;
for(int i = 0;i stringList = new ArrayList();
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() {ListStudentList = new ArrayList ();
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 {}
以上就是本文的全部内容,希望对大家的学习有所帮助。
正文完
星哥玩云-微信公众号