(Java)集合的综合运用实例——实现学生信息管理

通过运用ArrayList类和和HashMap类及其中重要方法,对学生信息进行管理,实现对学生信息的增、删、改、查操作。

1、Student学生信息类

public class Student {
		private int stuNo;//学号
		private String stuName;//姓名
		private String classNo;//班级号
		public Student(){
		}
		public String getClassNo() {
			return classNo;
		}
		public void setClassNo(String classNo) {
			this.classNo = classNo;
		}
		public int getStuNo() {
			return stuNo;
		}
		public void setStuNo(int stuNo) {
			this.stuNo = stuNo;
		}
		public String getStuName() {
			return stuName;
		}
		public void setStuName(String stuName) {
			this.stuName = stuName;
		}
		public Student(int stuNo, String stuName) {
			super();
			this.stuNo = stuNo;
			this.stuName = stuName;
		}
		public Student(int stuNo, String strName, String classNo) {
			super();
			this.stuNo = stuNo;
			this.stuName = strName;
			this.classNo = classNo;
		}
		@Override
		public String toString() {
			return "Student [strName=" + stuName + ", stuNo=" + stuNo + ", classNo="+classNo +"]";
		}
}


2、StudentUtil工具类,封装了对学生信息增删改查的操作方法

import java.util.*;
public class StudentUtil {
	private HashMap students=null;
	public StudentUtil(){
		students=new HashMap();
	}
	
	//按姓名查找
	public Student getStudentByName(String name){
		if(students!=null&&students.size()>0){
			Student student=(Student)students.get(name);
			return student;
		}
		return null;
	}
	//按班级号查找
	public ArrayList getStudentByClassNo(String classNo){
		ArrayList result=new ArrayList();
		for(Object key:students.keySet()){
			Student stu=(Student)students.get(key);
			if(stu.getClassNo().indexOf(classNo)!=-1){
				result.add(stu);
			}
		}
		return result;
	}
	//返回所有学生信息
	public ArrayList getAllStudents(){
		ArrayList result=new ArrayList();
		for(Object key:students.keySet()){
			Student stu=(Student)students.get(key);
				result.add(stu);
		}
		return result;
	}
	
	//添加学生
	public void addStudent(Student student){
		this.students.put(student.getStuName(), student);
	}
	public void addStudent(int stuNo,String stuName,String classNo){
		Student student=new Student(stuNo,stuName,classNo);
		addStudent(student);
	}
	
	//删除学生
	public boolean deleteStudent(Student student){
			String stu=student.getStuName();
			return this.deleteStudent(stu);
	}
	public boolean deleteStudent(String stuName){
		if(students!=null&&students.size()>0){
			Student student=this.getStudentByName(stuName);
			if(student==null)
				return false;
			this.students.remove(stuName);
			return true;
		}
		return false;
	}
	
	//修改学生
	public boolean updateStudent(Student student){
		if(students!=null&&students.size()>0){
			Student stu=this.getStudentByName(student.getStuName());
			if(stu==null)
				return false;
			this.students.put(stu.getStuName(), stu);
			return true;
		}
		return false;
	}
	public boolean updateStudent(int stuNo,String stuName,String classNo){
		Student stu=this.getStudentByName(stuName);
		stu.setStuName(stuName);
		stu.setStuNo(stuNo);
		stu.setClassNo(classNo);
		return updateStudent(stu);
	}
}

3、testStudent测试类,测试StudentUtil中方法是否能成功实现增删改查操作

import java.util.ArrayList;
import java.util.Scanner;

public class testStudent {
	public static void main(String[] args) {
		System.out.println("-系统初始化开始----------");
		StudentUtil studentutil=new StudentUtil();
		Student student=null;
		for(int i=1;i<=10;i++){
			if(i<=5)
				student=new Student(i,"student"+i,"s100"+i);
			else
				student=new Student(i,"student"+i,"s200"+i);
			studentutil.addStudent(student);
		}
		System.out.println("-系统初始化结束----------");
		
		Scanner input=new Scanner(System.in);
		//添加学生信息
		System.out.println("---------添加一个学生信息----------");
		student=new Student();
		System.out.println("No:");
		student.setStuNo(input.nextInt());
		System.out.println("Name:");
		student.setStuName(input.next());
		System.out.println("Class:");
		student.setClassNo(input.next());
		studentutil.addStudent(student);
		showStudent(studentutil);
		//删除学生信息
		System.out.println("---------删除一个学生信息----------");
		System.out.println("输入要删除的学生姓名:");
		String stuName=input.next();
		if(studentutil.deleteStudent(stuName)){
			System.out.println("删除成功!");
			showStudent(studentutil);
		}
		else{
			System.out.println("删除失败!");
		}
		//修改学生信息
		System.out.println("---------修改一个学生信息----------");
		System.out.println("输入要修改的学生姓名:");
		String name=input.next();
		student=studentutil.getStudentByName(name);
		if(student!=null){
			System.out.println("输入学生班级:");
			student.setClassNo(input.next());
			if(studentutil.updateStudent(student)){
				System.out.println("修改成功!");
				showStudent(studentutil);
			}
			else{
				System.out.println("修改失败!");
			}
		}
		else{
			System.out.println("查无此人!");
		}
		//查找学生信息
		System.out.println("---------查找改一个学生信息----------");
		System.out.println("输入要查找的班级:");
		String classNo=input.next();
		ArrayList list=studentutil.getStudentByClassNo(classNo);
		System.out.println("---------显示学生信息----------");
		for(Object obj:list){
			System.out.println(obj);
		}
	}
	//显示所有学生信息类
	private static void showStudent(StudentUtil studentutil) {
		System.out.println("---------显示学生信息----------");
		ArrayList result=studentutil.getAllStudents();
		for(Object obj:result){
			System.out.println(obj);
		}
	}
}






你可能感兴趣的:(Java)