需求分析:
通過陣列 ,完成 對學生資訊的 管理 (增刪改查)
建立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("您已退出系統");
}