学生管理系统:
学生信息:学号,姓名,家庭住址
能够手动添加学生信息
能够根据学号修改学生的信息
能够删除学生的信息
student类:
public class Student {
String name;
int id;
String address;
public Student() {
}
public Student(String name, int id, String address) {
this.name = name;
this.id = id;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}
StudentMethod类
import java.util.Scanner;
public class StudentMethod {
static Student studentarr[] = new Student[30];//存三十个人
static Scanner scanner = new Scanner(System.in);
static int count = 0;
public static Student[] add(){
Student studentadd = new Student();
System.out.println("请输入要添加的学生学号");
int id = scanner.nextInt();
studentadd.setId(id);
System.out.println("请输入要添加的学生姓名");
String name = scanner.next();
studentadd.setName(name);
System.out.println("请输入要添加的学生家庭住址");
String address = scanner.next();
studentadd.setAddress(address);
studentarr[count]=studentadd;
count++;
return studentarr;
}
public static Student[] update(){
System.out.println("学号"+"\t"+"姓名"+"\t"+"住址");
for (int i = 0; i < count; i++) {
System.out.println(studentarr[i].getId()+"\t"+studentarr[i].getName()+"\t"+studentarr[i].getAddress());
}
return studentarr;
}
public static Student[] revise(int conditionid){
Student studentrevise = new Student();
int index = Array.findbyid(studentarr,conditionid,count);
if (index>=0){
studentrevise.setId(conditionid);
System.out.println("请输入要修改后的学生姓名");
String name = scanner.next();
studentrevise.setName(name);
System.out.println("请输入要修改后的学生家庭住址");
String address = scanner.next();
studentrevise.setAddress(address);
studentarr[index]=studentrevise;
}
return studentarr;
}
public static Student[] delete(int deleteid){
Student[] newstudent = new Student[studentarr.length-1];
int index = Array.findbyid(studentarr,deleteid,count);
if (index>=0){
System.arraycopy(studentarr,0,newstudent,0,index);
//旧数组从零位置开始复制给新数组(新数组也是从0开始接收复制),复制长度index
System.arraycopy(studentarr,index+1,newstudent,index,studentarr.length-1-index);
//旧数组从index+1位置开始复制(新数组则是从index位置开始接收,因为原来index位置的元素已经删除),复制的数组长度为原数组长度-1-index
studentarr = newstudent;
count--;
System.out.println("删除成功");
}else {
System.out.println("查无此人");
}
return newstudent;
}
}
main方法:
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
Student studentall = new Student();
StudentMethod studentMethod = new StudentMethod();
Scanner scanner =new Scanner(System.in);
while(true){
System.out.println("========= 欢迎光临 =========");
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("请操作");
int a = scanner.nextInt();
switch(a){
case 1:
StudentMethod.add();
break;
case 2:
System.out.println("请输入要修改的学生学号");
int conditionid = scanner.nextInt();
StudentMethod.revise(conditionid);
break;
case 3:
System.out.println("请输入要删除的学生学号");
int deleteid = scanner.nextInt();
StudentMethod.delete(deleteid);
break;
case 4:
StudentMethod.update();
break;
case 5:
System.out.println("感谢使用");
System.exit(0);
break;
}
}
}
}
Array类
public class Array {
public static int findbyid(Student[] students,int id,int count){
for (int i = 0; i < count; i++) {
if (id == students[i].getId()){
return i;
}
}
return -1;
}
}