学生管理系统增删改查

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

完成一个学生信息管理系统
完成以下功能:
1、添加学生信息:学号、姓名、年龄、性别、地址、联系方式
2、删除指定学生:
(1)可根据学号查找并删除
(2)可根据姓名查询并删除
(3)可根据年龄段查询并删除(年龄是要输入范围,例如10~15岁之间)
(4)全部清空
3、修改指定学生信息:
(1)根据学号查询学生并提供多个信息的修改
(2)根据姓名查询学生并提供多个信息的修改
(3)根据性别查询学生并提供多个信息的修改
(4)根据年龄段查询学生并提供多个信息的修改(年龄是要输入范围,例 如10~15岁之间)
4、查询学生信息:
(1)查询所有学生
(2)根据某条件查询(可以根据姓名、学号、年龄段、性别查询)
完成步骤:
1、先做一个类对学生信息进行封装:Student
2、增删改查分别对应四个方法:
class ManagerStudent{}
1、添加学生信息:
public void addStudent(Student s);
2、删除指定学生:
public boolean removeStudent(int stuNo);通过学号删除
public boolean removeStudent(String name);通过姓名删除
public boolean removeStudent(int beginAge,int endAge);通过年龄段删除
public boolean removeStudent();//清空
3、修改指定学生信息:
public boolean editStudent(Student s);//通过学号修改
4、查询学生信息:
public ArrayList getAllStudents();//获取全部
public ArrayList getStudentsByName(String name);//根据姓名查询
public ArrayList getStudentsByNo(int no);//根据学号查询
public ArrayList getStudentsBySex(String sex);//根据性别查询
public ArrayList getStudentsByAge(int beginAge,int endAge);//根据年龄段查询








package 集合项目;
import java.util.ArrayList;
public class ManagerStudents
{
public ArrayList al=new ArrayList();
public void addStudent(Student s) //添加学生信息
{
al.add(s);
}


public boolean editStudentXuehao(Student s) //根据学号 然后修改它的别的属性
{
boolean flag=false;
for(int i=0;i{
Student st=(Student)al.get(i);
if(st.getXuehao()==s.getXuehao())
{
al.set(i,s);
flag=true;
break;
}
}
return flag;
}


public ArrayList getStudentsXuehao(int xuehao) //根据学号查询
{
ArrayList ay=new ArrayList();
for(int i=0;i{
Student st =(Student)al.get(i);
if(st.xuehao == xuehao)
{
ay.add(st);
}
}
return ay;
}


public boolean removeStudentXuehao(int xuehao) //根据学号删除
{
ArrayList ay = getStudentsXuehao(xuehao);
return al.removeAll(ay);
}


public ArrayList getStudentsName(String name) //根据姓名查询
{
ArrayList ay=new ArrayList();
for(int i=0;i{
Student st =(Student) al.get(i);
if(.equals(name))
{
ay.add(st);
}

}
return ay;
}


public boolean removeStudentName(String name) //根据姓名删除
{
ArrayList ay = getStudentsName(name);
return al.removeAll(ay);
}


public ArrayList getStudentSex(String sex) //根据性别查询
{
ArrayList ay=new ArrayList();
for(int i=0;i{
Student st =(Student) al.get(i);
if(st.sex.equals(sex))
{
ay.add(st);
}
}
return ay;
}


public ArrayList getStudentsByAge(int beginAge,int endAge)//根据年龄段查询
{
ArrayList ay=new ArrayList();
for(int i=0;i{
Student st=(Student)al.get(i);
if(st.age>=beginAge & st.age<=endAge)
{
ay.add(st);
}
}
return ay;
}


public boolean removeStudentByAge(int beginAge,int endAge)//根据年龄段删除
{
ArrayList ay = getStudentsByAge(beginAge,endAge);
return al.removeAll(ay);
}

public ArrayList getAllStudents() //获取全部
{
return al;
}
public boolean removeStudentshuoyou() //清空所有
{
boolean flag = false;
al.clear();
flag = true;
return flag;
}
}





package 集合项目;
public class Student
{
int xuehao;
String name;
int age;
String sex;
String classes;
int lianxi;
public Student(){}
public Student(int xuehao,String name,int age,String sex,String classes,int lianxi)
{
this.xuehao=xuehao;
=name;
this.age=age;
this.sex=sex;
this.classes=classes;
this.lianxi=lianxi;
}
public void setXuehao(int xuehao)
{
this.xuehao=xuehao;
}
public int getXuehao()
{
return xuehao;
}
public void setName(String name)
{
=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
public void setSex(String sex)
{
this.sex=sex;
}
public String getSex()
{
return sex;
}
public void setClasses(String classes)
{
this.classes=classes;
}
public String getClasses()
{
return classes;
}
public void setLianxi(int lianxi)
{
this.lianxi=lianxi;
}
public int getLianxi()
{
return lianxi;
}
}





package 集合项目;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentMain
{
public static void main(String[] args)
{
Scanner read=new Scanner(System.in);
ManagerStudents xx=new ManagerStudents();
int num; //用户选择做什么事情
String dnf;
System.out.print("请选择你要添加多少个学生:");
int temp=read.nextInt(); //用户选择要填写多少个人的信息
Student st=new Student();
for(int i=1;i<=temp;i++)
{
System.out.println("请输入第:"+(i)+"个学生的学号,姓名,年龄,性别,班级,电话");
st=new Student(read.nextInt(),read.next(),read.nextInt(),read.next(),read.next(),read.nextInt());
xx.addStudent(st);
}
do
{
System.out.print("请选择你要的的操

作1:(添加学生)、2:(删除学生)、3:(查询学生)、4:(修改学生):");
num=read.nextInt();
switch(num)
{
case 1:
System.out.println("你选择了添加学生!");
System.out.println("请输入你要添加多少个:");
int er=read.nextInt();
for(int i=1;i<=er;i++)
{
System.out.println("学生的学号,姓名,年龄,性别,班级,电话");
st=new Student(read.nextInt(),read.next(),read.nextInt(),read.next(),read.next(),read.nextInt());
xx.al.add(st);
}
break;
case 2:
System.out.println("你选择了删除学生!请选择(1/学号)(2/姓名)(3/年龄段)(4/全部删除)");
int b=read.nextInt();
if(b==1)
{
System.out.println("你选择用学号删除!你要删除的学号是:");
int qq=read.nextInt();
boolean a1=xx.removeStudentXuehao(qq);
if(a1==true)
{
System.out.println("删除成功!"+a1);
}else
{
System.out.println("删除失败!"+a1);
}
}else if(b==2)
{
System.out.println("你选择用姓名删除!你要删除的姓名是:");
String ww=read.next();
boolean a2=xx.removeStudentName(ww);
if(a2==true)
{
System.out.println("删除成功!"+a2);
}else
{
System.out.println("删除失败!"+a2);
}
}else if(b==3)
{
System.out.println("你选择了用年龄段删除!你要删除的年龄段是:");
int ee=read.nextInt();
int tt=read.nextInt();
boolean a3=xx.removeStudentByAge(ee,tt);
if(a3==true)
{
System.out.println("删除成功!"+a3);
}else
{
System.out.println("删除失败!"+a3);
}
}else if(b==4)
{
System.out.println("你选择了删除全部");
boolean a4=xx.removeStudentshuoyou();
if(a4==true)
{
System.out.println("删除成功!"+a4);
}else
{
System.out.println("删除失败!"+a4);
}
}else
{
System.out.println("你选择错误!");
}
break;
case 3:
System.out.println("你选择了查询学生!请选择(1/学号)(2/姓名)(3/性别)(4/年龄段)(5/全部)");
int a=read.nextInt();
if(a==1)
{
System.out.println("你选择了用学号查询");
System.out.println("你要查询的学号是:");
int aa=read.nextInt(); //学号
ArrayList s1=xx.getStudentsXuehao(aa);
for(int i=0;i{
Student f=(Student)s1.get(i);
System.out.println("学号:"+f.xuehao+",姓名:"++",年龄:"+f.age+",性别:"+f.sex+",地址:"+f.classes+",联系电话:"+f.lianxi);
}
}
else if(a==2)
{
System.out.println("你选择了用姓名查询");
System.out.println("你要查询的姓名是:");
String bb=read.next(); //姓名
ArrayList s2=xx.getStudentsName(bb);
for(int i=0;i{
Student f=(Student)s2.get(i);
System.o

ut.println("学号:"+f.xuehao+",姓名:"++",年龄:"+f.age+",性别:"+f.sex+",地址:"+f.classes+",联系电话:"+f.lianxi);
}
}
else if(a==3)
{
System.out.println("你选择了用性别查询");
System.out.println("你要查询的性别是:");
String cc=read.next(); //性别
ArrayList s3=xx.getStudentSex(cc);
for(int i=0;i{
Student f=(Student)s3.get(i);
System.out.println("学号:"+f.xuehao+",姓名:"++",年龄:"+f.age+",性别:"+f.sex+",地址:"+f.classes+",联系电话:"+f.lianxi);
}
}
else if(a==4)
{
System.out.println("你选择了年龄段查询!");
System.out.println("你要查询的年龄段是:");
int dd=read.nextInt(); //最小年龄
int ee=read.nextInt(); //最大年龄
ArrayList s4=xx.getStudentsByAge(dd,ee);
for(int i=0;i{
Student f=(Student)s4.get(i);
System.out.println("学号:"+f.xuehao+",姓名:"++",年龄:"+f.age+",性别:"+f.sex+",地址:"+f.classes+",联系电话:"+f.lianxi);
}
}
else if(a==5)
{
System.out.println("你选择了显示全部信息!");
ArrayList s5=xx.getAllStudents();
for(int i=0;i{
Student f=(Student)s5.get(i);
System.out.println("学号:"+f.xuehao+",姓名:"++",年龄:"+f.age+",性别:"+f.sex+",地址:"+f.classes+",联系电话:"+f.lianxi);
}
}else
{
System.out.println("你选择错误!");
}
break;
case 4:
System.out.println("你选择了修改学生!");
System.out.println("请输入你要修改的学号:");
int nn=read.nextInt();
Student sts = (Student)xx.getStudentsXuehao(nn).get(0);
System.out.println("请选择你要修改的属性(1、姓名)(2、年龄)(3、性别)(4、地址)(5、电话)");
int rr=read.nextInt();
if(rr==1)
{
System.out.println("你选择了修改姓名!");
System.out.println("请输入你将修改的姓名:");
String b1=read.next();
=b1;
xx.editStudentXuehao(sts);
System.out.println(xx.editStudentXuehao(sts)+"学号为"+nn+"的学生已经把姓名该成"+b1);
}else if(rr==2)
{
System.out.println("你选择了修改年龄!");
System.out.println("请输入你将修改的年龄:");
int b2=read.nextInt();
sts.age=b2;
xx.editStudentXuehao(sts);
System.out.println(xx.editStudentXuehao(sts)+"学号为"+nn+"的学生已经把年龄修改成"+b2);
}else if(rr==3)
{
System.out.println("你选择了修改性别!");
System.out.println("请输入你将修改的性别:");
String b3=read.next();
sts.sex=b3;
xx.editStudentXuehao(sts);
System.out.println(xx.editStudentXuehao(sts)+"学号为"+nn+"的学生已经把性别修改成"+b3);
}else if(rr==4)
{
System.out.println("你选择了修改地址!

");
System.out.println("请输入你将修改的地址:");
String b4=read.next();
sts.classes=b4;
xx.editStudentXuehao(sts);
System.out.println(xx.editStudentXuehao(sts)+"学号为"+nn+"的学生已经把地址修改成"+b4);
}else if(rr==5)
{
System.out.println("你选择了修改电话!");
System.out.println("请输入你将修改的电话:");
int b5=read.nextInt();
sts.lianxi=b5;
xx.editStudentXuehao(sts);
System.out.println(xx.editStudentXuehao(sts)+"学号为"+nn+"的学生已经把年龄修改成"+b5);
}else
{
System.out.println("对不起,没有这个选项!");
}
break;
default:
System.out.println("对不起,你选择错误!自爆装置已经启动,5秒后爆炸。。。。。");
}
System.out.println("如果你还要继续请输入?(Yes)或(yes)或(y)");
dnf=read.next();
}while(dnf.equals("Yes") | dnf.equals("yes") | dnf.equals("y"));
ArrayList s7=xx.getAllStudents();
for(int i=0;i{
Student f=(Student)s7.get(i);
System.out.println("学号:"+f.xuehao+",姓名:"++",年龄:"+f.age+",性别:"+f.sex+",地址:"+f.classes+",联系电话:"+f.lianxi);
}
}
}

相关文档
最新文档