目的:实现对学生信息的增删改查
一、student 类
- 四个基本信息:学号、姓名、手机号、地址
package venus;
public class Student{
private String id;
private String name;
private String mobile;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", mobile=" + mobile + ", address=" + address + "]";
}
}
二、Main 类
package venus;
import java.util.Scanner;
public class Main {
static int index =0;
static Student [] students=new Student[45];
public static void main(String[] args) {
System.out.println("***********************************");
System.out.println("* 欢迎使用学生管理系统 *");
System.out.println("***********************************");
for(;;) {
menu();
}
}
private static void menu() {
System.out.println("1、添加学生信息");
System.out.println("2、删除学生信息");
System.out.println("3、修改学生信息");
System.out.println("4、查询学生信息");
System.out.println("请输入操作项,点击Enter键");
Scanner scanner=new Scanner(System.in);
int option = scanner.nextInt();
switch (option) {
case 1:{
if(index>students.length-1) {
System.out.println("已经没有座位了");
break;
}
Student student=new Student();
System.out.println("请输入学号,点击Enter键");
String id=scanner.next();
student.setId(id);
System.out.println("请输入姓名,点击Enter键");
String name=scanner.next();
student.setName(name);
System.out.println("请输入手机号,点击Enter键");
String mobile=scanner.next();
student.setMobile(mobile);
System.out.println("请输入地址,点击Enter键");
String address=scanner.next();
student.setAddress(address);
if(index!=0) {
for(Student stu:students) {
if(stu==null) {
break;
}
if(stu.getId().equals(id)){
System.out.println("学号不能重复");
return;
}
}
}
students[index]=student;
index=index+1;
System.out.println("添加成功");
break;
}
case 2:{
System.out.println("请输入学号,并按Enter键");
boolean hasData=false;
String Id=scanner.next();
int i=0;
for(;i<students.length;i++) {
if(students[i]==null) {
break;
}
if(students[i].getId().equals(Id)){
for(;i<students.length;i++) {
students[i]=students[i+1];
students[i+1]=null;
break;
}
System.out.println("删除成功");
index=index-1;
hasData=true;
return;
}
}
if(!hasData) {
System.out.println("没有找到学号为"+Id+"的同学,请重新输入");
}
return;
}
case 3:{
System.out.println("请输入学号,并按Enter");
boolean hasData=false;
String Id=scanner.next();
for(Student student:students) {
if(student==null) {
break;
}
if(student.getId().equals(Id)){
System.out.println("请输入姓名,点击Enter键");
String name=scanner.next();
student.setName(name);
System.out.println("请输入手机号,点击Enter键");
String mobile=scanner.next();
student.setMobile(mobile);
System.out.println("请输入地址,点击Enter键");
String address=scanner.next();
student.setAddress(address);
System.out.println("修改成功");
hasData=true;
return;
}
}
if(!hasData) {
System.out.println("没有找到学号为"+Id+"的同学,请重新输入");
}
return;
}
case 4:{
boolean hasData=false;
System.out.println("请输入姓名,并按Enter");
String name=scanner.next();
for(Student stu:students) {
if(stu==null) {
break;
}
if(stu.getName().equals(name)) {
System.out.println(stu);
hasData = true;
}
}
if(!hasData) {
System.out.println("没有找到姓名为"+name+"的学生");
}
return;
}
default:
System.out.println("系统没有找到"+option+"操作项");
break;
}
}
}
结束语:博主是第一次写,有问题的话请多多指教!
转载:https://blog.csdn.net/xuebanub1/article/details/105107568
查看评论