Java学生管理系统之查询与修改—Day06
1.说明
1.用户根据提示输入相应的数字选择功能:
- 1).查看学生信息
- 2).输入学生Id删除对应学生
- 3).选择修改要修改的项目:
- (01.Id
- (02.姓名
- (03.性别
- (04.年龄
2.实现思路
- 首先创建一个JavaBean类用来创建学生学对象
- 学生属性:Id、姓名、性别、年龄
- 创建一个数组用来存储学生对象
- 定义一个方法用来遍历数组输出学生对象信息
- 需要进行对象非空判断,只有对象不为空时才进行输出,否则程序报错
- 定义一个方法用来删除指定了Id的学生信息
- 非空判断
- 根据Id返回的索引将该学生对象在数组上的信息赋值为null
- 定义方法修改学生信息
- 输入1修改学生Id
- 输入2修改学生姓名
- 输入3修改学生性别
- 输入4修改学生年龄
代码实现
1.JavaBean类
package com.java.StudentMmDk;
public class StudentBean {
private int Id;
private String name;
private char gender;
private int age;
public StudentBean() {
}
public StudentBean(int Id, String name, char gender, int age) {
this.Id = Id;
this.name = name;
this.gender = gender;
this.age = age;
}
/**
* 获取
* @return Id
*/
public int getId() {
return Id;
}
/**
* 设置
* @param Id
*/
public void setId(int Id) {
this.Id = Id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return gender
*/
public char getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(char gender) {
this.gender = gender;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
// public String toString() {
// return "StudentBean{Id = " + Id + ", name = " + name + ", gender = " + gender + ", age = " + age + "}";
// }
}
2.遍历数组输出学生信息
//打印学生信息
public static void getStudentInfo(StudentBean[] array) {
for (int i = 0; i < array.length; i++) {
StudentBean student;
student = array[i];
if (student != null) {
System.out.println(student.getId() + "," + student.getName() + "," + student.getGender() + "," + student.getAge());
}
}
}
3.获取Id在数组中的索引
//获取Id在数组中的索引
public static int getIndex(StudentBean[] array, int id) {
//遍历数组获取每一个对象
for (int i = 0; i < array.length; i++) {
StudentBean student = array[i];
//判断对象是否为空
if (student != null) {
if (student.getId() == id) {
return i;
}
}
}
return -1;
}
4.修改学生信息
//修改学生信息
public static StudentBean[] getChangeArray(StudentBean[] array, int id) {
//获取要修改的学生的Id
Scanner sc = new Scanner(System.in);
int index = getIndex(array, id);
System.out.println("输入1修改学生Id,输入2修改学生名字,输入3修改学生性别,输入4修改学生年龄,输入0停止修改:");
int choose = sc.nextInt();
StudentBean st = new StudentBean();
// 01.Id
// 02.姓名
// 03.性别
// 04.年龄
while (true){
if (choose == 1) {
System.out.println("要修改的Id:");
st = array[index];
int id2 = sc.nextInt();
st.setId(id2);
break;
} else if (choose == 2) {
System.out.println("要修改的名字:");
st = array[index];
String name = sc.next();
st.setName(name);
break;
} else if (choose == 3) {
System.out.println("要修改的性别:");
st = array[index];
char gender = sc.nextLine().charAt(0);
st.setGender(gender);
break;
}else if (choose == 4){
System.out.println("要修改的年龄:");
st = array[index];
int age = sc.nextInt();
st.setAge(age);
break;
} else if (choose == 0){
System.out.println("exit!");
break;
}else {
System.out.println("请选择正确的功能:");
}
完整测试类
package com.java.StudentMmDk;
//Author:Yh
import java.util.Scanner;
/**
* 定义一个数组存储学生的信息:Id、姓名、性别、年龄、班级
* 1.用户根据提示输入相应的数字选择功能:
* 1).查看学生信息
* 2).输入学生Id删除对应学生
* 3).选择修改要修改的项目:
* (01.Id
* (02.姓名
* (03.性别
* (04.年龄
*/
public class TestStudentMmDk {
public static void main(String[] args) {
//创建学生对象数组
StudentBean[] studentArray = getArray();
//功能选择
Scanner scan = new Scanner(System.in);
System.out.println("输入1,输出学生信息,输入2删除学生,输入3修改学生信息,输入0退出系统:");
while (true) {
int choose = scan.nextInt();
if (choose == 1) {
getStudentInfo(studentArray);
System.out.println("================");
System.out.println();
} else if (choose == 2) {
System.out.println("请输入您想要删除的学生Id(输入0停止删除):");
boolean flog = true;
while (flog) {
int id = scan.nextInt();
int index = getIndex(studentArray, id);
if (index >= 0) {
//说明存在该对象,删除该对象并提示删除成功
studentArray[index] = null;
System.out.println("该学生已成功删除!");
flog = false;
} else if (id == 0) {
flog = false;
} else {
//说明不存在该对象,提示删除失败
System.out.println("不存在该对象,删除失败!请重新输入想要删除的学生Id:");
}
}
System.out.println("================");
System.out.println();
} else if (choose == 3) {
System.out.println("请输入您想要修改信息的学生Id(输入0退出):");
int id = scan.nextInt();
if (id == 0){
System.out.println("exit!");
return;
}else {
getChangeArray(studentArray, id);
}
System.out.println("================");
System.out.println();
} else if (choose == 0) {
break;
} else {
System.out.println("请选择正确的功能!");
}
}
}
public static StudentBean[] getArray() {
StudentBean[] studentArray = new StudentBean[4];
StudentBean stu1 = new StudentBean(01, "丽丽", '女', 19);
StudentBean stu2 = new StudentBean(02, "红红", '女', 18);
StudentBean stu3 = new StudentBean(03, "小帅", '男', 20);
StudentBean stu4 = new StudentBean(04, "小强", '男', 19);
studentArray[0] = stu1;
studentArray[1] = stu2;
studentArray[2] = stu3;
studentArray[3] = stu4;
return studentArray;
}
//获取Id在数组中的索引
public static int getIndex(StudentBean[] array, int id) {
//遍历数组获取每一个对象
for (int i = 0; i < array.length; i++) {
StudentBean student = array[i];
//判断对象是否为空
if (student != null) {
if (student.getId() == id) {
return i;
}
}
}
return -1;
}
//打印学生信息
public static void getStudentInfo(StudentBean[] array) {
for (int i = 0; i < array.length; i++) {
StudentBean student;
student = array[i];
if (student != null) {
System.out.println(student.getId() + "," + student.getName() + "," + student.getGender() + "," + student.getAge());
}
}
}
//修改学生信息
public static StudentBean[] getChangeArray(StudentBean[] array, int id) {
//获取要修改的学生的Id
Scanner sc = new Scanner(System.in);
int index = getIndex(array, id);
System.out.println("输入1修改学生Id,输入2修改学生名字,输入3修改学生性别,输入4修改学生年龄,输入0停止修改:");
int choose = sc.nextInt();
StudentBean st = new StudentBean();
// 01.Id
// 02.姓名
// 03.性别
// 04.年龄
while (true){
if (choose == 1) {
System.out.println("要修改的Id:");
st = array[index];
int id2 = sc.nextInt();
st.setId(id2);
break;
} else if (choose == 2) {
System.out.println("要修改的名字:");
st = array[index];
String name = sc.next();
st.setName(name);
break;
} else if (choose == 3) {
System.out.println("要修改的性别:");
st = array[index];
char gender = sc.nextLine().charAt(0);
st.setGender(gender);
break;
}else if (choose == 4){
System.out.println("要修改的年龄:");
st = array[index];
int age = sc.nextInt();
st.setAge(age);
break;
} else if (choose == 0){
System.out.println("exit!");
break;
}else {
System.out.println("请选择正确的功能:");
}
}
return array;
}
}
END!