簡單的學(xué)生管理系統(tǒng)
給一個學(xué)生數(shù)組,要求實現(xiàn)增刪改查.
按照面向?qū)ο蟮乃悸穪砜?br>
需求分析:
1要有一個學(xué)生類(用來存儲學(xué)生的信息)
2要有一個管理類(實現(xiàn)各種方法,即講個中方法封裝放到里面)
3要有一個測試類(就是調(diào)用管理類的類)
如果你是新手,你可以理解為,我們要管理一些學(xué)生.分為:
1.要有一群學(xué)生
2.管理學(xué)生的方法
首先先創(chuàng)建一個學(xué)生類
package StudentManager;
public class Student {
static int num = 0;
String name;
int xh;
boolean sex;
public Student() {
this.xh = num++;
}
public String toString() {
String temp = "男";
if (!sex) {
temp = "女";
}
String str = "學(xué)號=" + this.xh + "姓名=" + this.name + "性別=" + temp;
return str;
}
}
創(chuàng)建管理類
package StudentManager;
public class Control {
int index= 0;
//
Student[] stu = new Student[3];
//初始化
void init(){
Student student = new Student();
student.name = "張三";
student.sex = true;
Student student1 = new Student();
student1.name = "王五";
student.sex = false;
stu[0]=student;
stu[1]=student1;
this.index = 2;
System.out.println("=====學(xué)生信息展示=======");
}
//重寫構(gòu)造方法
public Control(){
this.init();
}
//查看
void show(){
System.out.println("--------------------");
//遍歷存儲學(xué)生
for (int i = 0; i < this.index; i++) {
//打印學(xué)生的信息
System.out.println(stu[i]);
}
}
//根據(jù)id查找學(xué)生
Student selectStudent(int xh){
for (int i = 0; i < index; i++) {
if (this.stu[i].xh == xh) {
System.out.println(this.stu[i]);
return this.stu[i];
}
}
return null;
}
//根據(jù)id刪除學(xué)生
Student deleteByXh(int xh){
if (xh < 0) {
return null;
}
//先找學(xué)生
int tag=-1;
for (int i = 0; i < index; i++) {
if (xh==this.stu[i].xh) {
tag = i;
break;
}
}
if (tag== -1) {//證明沒找到學(xué)生
return null;
}
//記錄要刪除的元素
Student temp = this.stu[tag];
for (int i = tag; i < index-1; i++) {
this.stu[i] = this.stu[i+1];
}
index--;
return temp;
}
//4增加學(xué)生
void addStudent(Student stu){
if (index <this.stu.length) {
this.stu[index] = stu;
index ++;
}else {
//擴(kuò)展數(shù)組
this.expand();
//添加
this.addStudent(stu);
}
}
//擴(kuò)展
void expand(){
Student[] stus = new Student[this.stu.length + 2];
for (int i = 0; i < index; i++) {
stus[i] = this.stu[i];
}
this.stu = stus;//把新數(shù)組賦值給數(shù)組
}
//5 修改學(xué)生信息 stu
//要修改學(xué)生的id
void upDateStudentByStudent(Student stu){
Student student = this.selectStudent(stu.xh);
//修改
student.name = stu.name;
student.sex = stu.sex;
}
}
測試類
package StudentManager;
public class test {
public static void main(String[] args) {
Control control = new Control();
control.selectStudent(0);
control.show();
control.deleteByXh(1);
control.show();
Student stu1 = new Student();
stu1.name = "老王";
stu1.sex = true;
control.addStudent(stu1);
control.show();
stu1.name = "老張";
control.upDateStudentByStudent(stu1);
}
}