A comprehensive application example of (Java) collection--The realization of student information management

Source: Internet
Author: User

By using ArrayList class and HashMap class and the important methods, the student information is managed to realize the increase, deletion, modification and checking of students ' information.

1. Student Student Information class

public class Student {
		private int stuno;//number
		private string stuname;//name
		private string classno;//class number
		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= "+CL" Assno + "]";
		}
}


2, the Studentutil tool class, encapsulates the student information additions and deletions to check the operation method

Import java.util.*;
	public class Studentutil {private HashMap students=null;
	Public Studentutil () {students=new HashMap ();  ///Search by name for public Student getstudentbyname (String name) {if (Students!=null&&students.size () >0) {Student
			student= (Student) students.get (name);
		return student;
	return null;
		Find public ArrayList Getstudentbyclassno (String classno) {ArrayList result=new ArrayList () by class number;
			For (Object Key:students.keySet ()) {Student stu= (Student) students.get (key);
			if (Stu.getclassno (). IndexOf (Classno)!=-1) {Result.add (STU);
	} return result;
		//Return all student Information public ArrayList getallstudents () {ArrayList result=new ArrayList ();
				For (Object Key:students.keySet ()) {Student stu= (Student) students.get (key);
		Result.add (Stu);
	return result;
	//Add student 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);
			//Delete 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=th
			Is.getstudentbyname (Stuname);
			if (student==null) return false;
			This.students.remove (Stuname);
		return true;
	return false;  }//Modify Student 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 test class, Test Studentutil whether the method can be successfully implemented or deleted to check operation

Import java.util.ArrayList;

Import Java.util.Scanner;
		public class Teststudent {public static void main (string[] args) {System.out.println ("-System initialization started----------");
		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 ("-System initialization End----------");
		Scanner input=new Scanner (system.in);
		Add Student Information System.out.println ("---------Add a Student information----------");
		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);
		Delete Student information System.out.println ("---------Delete a student information----------");
		System.out.println ("Enter the name of the student to be deleted:");
	String Stuname=input.next ();	if (Studentutil.deletestudent (Stuname)) {System.out.println ("delete succeeded.")
			");
		Showstudent (Studentutil); } else{System.out.println ("Delete failed.
		");
		//Modify Student Information System.out.println ("---------Modify a student information----------");
		System.out.println ("Enter the name of the student to be modified:");
		String Name=input.next ();
		Student=studentutil.getstudentbyname (name);
			if (student!=null) {System.out.println ("Enter Student class:");
			Student.setclassno (Input.next ()); if (studentutil.updatestudent (student)) {SYSTEM.OUT.PRINTLN ("modified successfully.")
				");
			Showstudent (Studentutil); } else{System.out.println ("Failed to modify.")
			"); } else{System.out.println ("No this person is found.")
		");
		//Find Student Information System.out.println ("---------Find a Student information----------");
		System.out.println ("Enter the class to find:");
		String Classno=input.next ();
		ArrayList List=studentutil.getstudentbyclassno (CLASSNO);
		System.out.println ("---------Show Student Information----------");
		for (Object obj:list) {System.out.println (obj); }//Show all student information class private static void Showstudent (Studentutil studentutil) {SyStem.out.println ("---------Show Student Information----------");
		ArrayList result=studentutil.getallstudents ();
		for (Object Obj:result) {System.out.println (obj); }
	}
}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.