学生类
package com;
public class Students {
String name;
String tel;
int id;
String address;
public Students(String name, String tel, int id, String address) {
this.name = name;
this.tel = tel;
this.id = id;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
主类
package com;
import java.util.Scanner;
/**
* 学生系统pro
* 将各个模块打包成函数
* 删除原理进行修改
* 查询功能完善
*/
public class Test01 {
static int number = 0;
public static void main(String[] args) {
Students[] students = new Students[20];
Scanner scanner = new Scanner(System.in);
System.out.println();
int a =0;
while (a!=5)
{
System.out.println("---------欢迎使用学生管理系统-------");
System.out.println("* 1.添加学生 *");
System.out.println("* 2 删除学生 *");
System.out.println("* 3 修改学生 *");
System.out.println("* 4 查询学生 *");
System.out.println("* 5 退出系统 *");
System.out.println("----------------------------------");
System.out.println("请输入你的选择");
a=scanner.nextInt();
if(a==1)
{
//添加学生
Students s= add("输入添加学生的");
students[number]=s;
number++;
}else if (a==2)
{
//删除学生
System.out.println("输入要删除学生id");
int id = scanner.nextInt();
int index = cha(students,id);
if(index!=-1)
{
del(students,index);
}else {
System.out.println("查无此人");
}
}else if(a==3)
{
//修改
System.out.println("输入要修改学生id");
int id = scanner.nextInt();
int index = cha(students,id);
if(index!=-1)
{
charge(students,index);
}else {
System.out.println("查无此人");
}
}else if(a==4)
{
//查询学生
System.out.println("1。查询全部学生 2.查寻个别学生");
int x =scanner.nextInt();
if(x==1)
{
enquiries(students,number);
}else if (x==2)
{
System.out.println("输入要查询学生的id");
int id = scanner.nextInt();
int index = cha(students,id);
if(index!=-1)
{
Students ss = students[index];
System.out.println("name:"+ss.name+" tel:"+ss.tel+" address:"+ss.address+" id:"+ss.id);
}else {
System.out.println("查无此人");
}
}else
{
System.out.println("无此命令");
}
}else
{
if(a != 5)
{
System.out.println("错误指令请重新输入");
}
}
}
System.out.println("结束!!!");
}
//添加学生
public static Students add(String title){
System.out.println(title+"nmae");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println(title+"tel");
String tel = scanner.next();
System.out.println(title+"address");
String address = scanner.next();
System.out.println(title+"id");
int id = scanner.nextInt();
Students s= new Students(name,tel,id,address);
return s;
}
//查看学生
public static int cha(Students[] students,int mid)
{
for (int i =0;i<number;i++)
{
Students s=students[i];
if(s.id==mid)
{
return i;
}
}
return -1;
}
//查询
public static void enquiries(Students[] students,int number)
{
for(int i=0;i<number;i++){
Students s = students[i];
System.out.println("name:"+s.name+" tel:"+s.tel+" address:"+s.address+" id:" + s.id);
}
}
//删除
public static void del(Students[] students,int index)
{
/*
将数组最后一位填补到删除的位置上
*/
students[index]=null;
students[index]=students[number-1];
students[number-1]=null;
number--;
System.out.println("删除成功!!!");
}
//修改
public static void charge(Students[] students,int index)
{
Students s=add("输入修改学生的");
students[index]=s;
System.out.println("修改成功!!!");
}
}