首页 > 代码库 > 用基本数据结构修改后的学生信息管理系统(增删改查)
用基本数据结构修改后的学生信息管理系统(增删改查)
package com.xt.student.system;
//创建学生类存放信息
public class Student {
//声明变量
private String stuNo;
private String stuName;
private String gender;
private int age;
private int score;
//对变量进行封装
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
//修改toString()方法来输出学生信息,否则输出的为学生信息存储地址
public String toString() {
return "| " + stuNo + " | " + stuName + " | " + gender + " | " + age + " | " + score + " | ";
}
}
//创建一个类实现增删改查
package com.xt.student.system;
public class StuStata {
/**
* 存放学生信息的数组
*/
private static List<Student> stuData = http://www.mamicode.com/new LinkedList
/**
* 学生数组信息存放标志位
*/
private static int dataIndex = 0;
/**
* 增加学生信息
*
*/
public void addStu(Student stu){
stuData.add(stu);
dataIndex++;
}
/**
* 根据学生编号删除学生信息
* 当查找到学生信息之后,将查找到的学生以后位置的学生信息向前移一位。
*/
public void delStu(String stuNo){
boolean boo = false;
for(int i = 0;i < dataIndex;i++){
if(stuData.get(i).getStuNo().equals(stuNo)){
stuData.remove(stuData.get(i));
boo = true;
break;
}
}
if(boo){
dataIndex--;
}
}
/**
* 修改学生信息
*/
public void update(Student stu){
for(int i = 0;i< dataIndex; i++){
if(stu.getStuNo().equals(stuData.get(i).getStuNo())){
stuData.set(i, stu);
}
}
}
/**
* 根据学生编号查询学生信息,当有此学生信息时,
* 返回学生信息,当学生信息不存在时,返回 Null
*
public Student getStuByStuNo(String stuNo){
for(int i = 0;i < dataIndex;i++){
if(stuNo.equals(stuData.get(i).getStuNo())){
return stuData.get(i);
}
}
return null;
}
public int getDataIndex(){
return dataIndex;
}
/**
* 获取所有的学生信息
*/
public Student getAllStuInfo(int i){
return stuData.get(i);
}
}
//根据用户输入的信息修改学生信息
package com.xt.student.system;
import java.util.List;
import java.util.Scanner;
public class stuSeverce {
StuStata sd = new StuStata();
public void addStuInfo(Scanner scanner){
Student stu = new Student();
System.out.println("请输入学生编号:");
String stuNo = scanner.next();
if(sd.getStuByStuNo(stuNo) != null){
System.out.println("此学生编号已存在,请选择操作:");
System.out.println("1 继续添加学生信息");
System.out.println("0 退出");
int optional = scanner.nextInt();
switch(optional){
case 0:
return;
case 1:
addStuInfo(scanner);
}
}
stu.setStuNo(stuNo);
System.out.println("请输入学生姓名:");
stu.setStuName(scanner.next());
System.out.println("请输入学生性别(男/女):");
stu.setGender(scanner.next());
System.out.println("请输入学生年龄:");
stu.setAge(scanner.nextInt());
System.out.println("请输入学生成绩:");
stu.setScore(scanner.nextInt());
sd.addStu(stu);
System.out.println("输入信息成功 \n");
}
public void printAllStuInfo(){
int dataIndex = sd.getDataIndex();
if(dataIndex == 0){
System.out.println("当前无学生信息! \n\n");
}
for(int i = 0; i < dataIndex ;i++){
Student stu = sd.getAllStuInfo(i);
printTitle();
System.out.println(""+stu.toString());
}
}
public void delStuInfo(Scanner scanner){
System.out.println("请输入要删除的学生编号:");
String stuNo = scanner.next();
if(sd.getStuByStuNo(stuNo) == null){
System.out.println("此学生编号不存在,请选择操作:");
System.out.println("1 继续删除学生信息");
System.out.println("0 退出");
int optional = scanner.nextInt();
switch(optional){
case 0:
return;
case 1:
delStuInfo(scanner);
}
}
sd.delStu(stuNo);
System.out.println("删除信息成功!\n");
}
public void updateStuInfo(Scanner scanner){
System.out.println("请输入要修改的学生编号:");
Student stu = new Student();
String stuNo = scanner.next();
if(sd.getStuByStuNo(stuNo) == null){
System.out.println("此学生编号不存在,请选择操作:");
System.out.println("1 继续修改学生信息");
System.out.println("0 退出");
int optional = scanner.nextInt();
switch(optional){
case 0:
return;
case 1:
updateStuInfo(scanner);
}
}
stu.setStuNo(stuNo);
System.out.println("请输入学生姓名:");
stu.setStuName(scanner.next());
System.out.println("请输入学生性别(男/女):");
stu.setGender(scanner.next());
System.out.println("请输入学生年龄:");
stu.setAge(scanner.nextInt());
System.out.println("请输入学生成绩:");
stu.setScore(scanner.nextInt());
sd.update(stu);
System.out.println("输入信息成功! \n");
}
public void checkStuInfo(Scanner scanner){
System.out.println("请输入所要查询的学生的编号");
String stuNo = scanner.next();
if(sd.getStuByStuNo(stuNo) == null){
System.out.println("此学生编号不存在,请选择操作:");
System.out.println("1 继续查询学生信息");
System.out.println("0 退出");
int optional = scanner.nextInt();
switch(optional){
case 0:
return;
case 1:
updateStuInfo(scanner);
}
}
printTitle();
System.out.println("" + sd.getStuByStuNo(stuNo).toString());
}
public void printTitle(){
System.out.print("| 学生编号 | 学生姓名 | 性别 | 年龄 | 成绩 | \n\n");
}
}
//菜单
package com.xt.student.system;
public class Menu {
public void showmenu(){
System.out.println("1:添加学生信息");
System.out.println("2:修改学生信息");
System.out.println("3:删除学生信息");
System.out.println("4:查询学生信息");
System.out.println("5:查询所有学生信息");
System.out.println("0:退出系统");
}
}
//主方法
package com.xt.student.system;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println(" 欢迎来到学生信息管理系统 ");
Menu menu = new Menu();
stuSeverce stu = new stuSeverce();
while(true){
System.out.println("请按指示进行操作");
menu.showmenu();
int optional = scanner.nextInt();
switch(optional){
case 0:
System.out.println("退出系统");
System.exit(0);
break;
case 1:
stu.addStuInfo(scanner);
break;
case 2:
stu.updateStuInfo(scanner);
break;
case 3:
stu.delStuInfo(scanner);
break;
case 4:
stu.checkStuInfo(scanner);
break;
case 5:
stu.printAllStuInfo();
break;
}
}
}
}
用基本数据结构修改后的学生信息管理系统(增删改查)