Java实现简单的增删改查操作
需求分析:
通过数组 ,完成 对学生信息的 管理 (增删改查)
创建1个 学生类
创建1个 CRUD的类 – 学生管理类 并测试
在这个程序中我只运用了两个类进行操作
package com.hopu.demo;
/*
学生类
*/
public class Student {
private String id;// 学号
private String name;//姓名
private int age; //年龄
public Student() {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return “Student{” +
“id=\'” + id + \’\\’\’ +
“, name=\'” + name + \’\\’\’ +
“, age=” + age +
\’}\’;
}
}
//方法类
package com.hopu.demo;
import java.util.Scanner;
public class StudentDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“请输入录入学生的数量:”);
int a = sc.nextInt();
Student[] students = new Student[a + 10000];
int n = 0;
for (int i = 0; i < a; i++) {
System.out.print(“请输入第” + (i + 1) + “个学生的学号:”);
String id = sc.next();
System.out.print(“请输入第” + (i + 1) + “个学生的姓名:”);
String name = sc.next();
System.out.print(“请输入第” + (i + 1) + “个学生的年龄:”);
int age = sc.nextInt();
//给student[i]在内存中申请一块空间
students[i] = new Student();
students[i].setId(id);
students[i].setName(name);
students[i].setAge(age);
n++;
}
System.out.println(“录入学生成功!”);
while (true) {
int choose = chooseFuntion();
switch (choose) {
//增加
case 1:
System.out.print(“请输入增加学生数量:”);
int b = sc.nextInt();
for (int j = 0; j < b; j++) {
addStudent(students, a + j);
n++;
}
break;
//删除
case 2:
deleteStudent(students, n);
n–;
break;
//修改
case 3:
updateStudent(students, n);
break;
//查找
case 4:
chazhaoStudent(students, n);
break;
//显示
case 5:
printStudent(students, n);
break;
//退出
case 6:
exit();
break;
default:
System.out.println(“———————————-“);
System.out.println(“功能选择有误,请输入正确的功能序号!”);
break;
}
}
}
public static int chooseFuntion() {
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(“6.退出”);
System.out.print(“请输入要执行的操作序号:”);
Scanner sc = new Scanner(System.in);
int choose = sc.nextInt();
return choose;
}
public static void addStudent(Student[] s, int a) {
s[a] = new Student();
Scanner sc = new Scanner(System.in);
System.out.print(“请输入姓名:”);
String id = sc.next();
s[a].setName(id);
System.out.print(“请输入姓名:”);
String name = sc.next();
s[a].setName(name);
System.out.print(“请输入年龄:”);
int age = sc.nextInt();
s[a].setAge(age);
System.out.println(“———增加成功———-“);
}
public static void deleteStudent(Student[] s, int n) {
Scanner sc = new Scanner(System.in);
System.out.println(“请输入要删除的姓名:”);
String name = sc.next();
for (int i = 0; i < n; i++) {
if (name.equals(s[i].getName())) {
s[i] = s[i + 1];
}
}
System.out.println(“———删除成功———-“);
}
public static void updateStudent(Student[] s, int n) {
Scanner sc = new Scanner(System.in);
System.out.println(“请输入要修改的姓名:”);
String name = sc.next();
for (int i = 0; i < n; i++) {
if (name.equals(s[i].getName())) {
System.out.print(“学号修改为:”);
String a = sc.next();
s[i].setName(a);
System.out.print(“姓名修改为:”);
String b = sc.next();
s[i].setName(b);
System.out.print(“年龄修改为:”);
int c = sc.nextInt();
s[i].setAge(c);
}
}
System.out.println(“———修改成功———-“);
}
public static void chazhaoStudent(Student[] s, int n) {
Scanner sc = new Scanner(System.in);
System.out.println(“请输入要查找的姓名:”);
String name = sc.next();
for (int i = 0; i < n; i++) {
if (name.equals(s[i].getName())) {
System.out.println(“———查找结果———-“);
System.out.println(“学号:” + s[i].getId()+”,姓名:” + s[i].getName() + “,年龄:” + s[i].getAge());
}
}
}
public static void printStudent(Student[] s, int n) {
System.out.println(“———全部学生———-“);
for (int i = 0; i < n; i++) {
String id = s[i].getId();
String name = s[i].getName();
int age = s[i].getAge();
System.out.println(“学号为:” + id+”,姓名为:” + name + “,年龄为:” + age);
}
}
public static void exit() {
System.out.println(“—————-退出—————“);
System.out.println(“您已退出系统”);
}