學生管理系統(tǒng)之修改學生功能
package com.itheima;
/*
* 這是我的學生類
*/
public class Student {
// 學號
private String id;
// 姓名
private String name;
// 年齡
private String age;
// 居住地
private String address;
public Student() {
}
public Student(String id, String name, String age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = 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 getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.itheima;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 這是我的學生管理系統(tǒng)的主類
*
* 步驟如下:
* A:定義學生類
* B:學生管理系統(tǒng)的主界面的代碼編寫
* C:學生管理系統(tǒng)的查看所有學生的代碼編寫
* D:學生管理系統(tǒng)的添加學生的代碼編寫
* E:學生管理系統(tǒng)的刪除學生的代碼編寫
* F:學生管理系統(tǒng)的修改學生的代碼編寫
*/
public class StudentManagerTest {
public static void main(String[] args) {
//創(chuàng)建集合對象嚎货,用于存儲學生數(shù)據(jù)
ArrayList<Student> array = new ArrayList<Student>();
//為了讓程序能夠回到這里來捉捅,我們使用循環(huán)
while(true) {
//這是學生管理系統(tǒng)的主界面
System.out.println("--------歡迎來到學生管理系統(tǒng)--------");
System.out.println("1 查看所有學生");
System.out.println("2 添加學生");
System.out.println("3 刪除學生");
System.out.println("4 修改學生");
System.out.println("5 退出");
System.out.println("請輸入你的選擇:");
//創(chuàng)建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
String choiceString = sc.nextLine();
//用switch語句實現(xiàn)選擇
switch(choiceString) {
case "1":
//查看所學生
findAllStudent(array);
break;
case "2":
//添加學生
addStudent(array);
break;
case "3":
//刪除學生
deleteStudent(array);
break;
case "4":
//修改學生
updateStudent(array);
break;
case "5"://case穿透
//退出
//System.out.println("謝謝你的使用");
//break;
default:
System.out.println("謝謝你的使用");
System.exit(0);//JVM退出
break;
}
}
}
//修改學生
public static void updateStudent(ArrayList<Student> array) {
//修改學生的思路:鍵盤錄入一個學號,到集合中去查找,看是否有學生使用的是該學號弃榨,如果有就修改該學生
//創(chuàng)建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要修改的學生的學號:");
String id = sc.nextLine();
//定義一個索引
int index = -1;
//遍歷集合
for(int x = 0; x < array.size(); x++) {
//獲取每一個學生對象
Student s = array.get(x);
//拿學生對象的學號和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
index = x;
break;
}
}
if(index == -1) {
System.out.println("不好意思,你要修改的學號對應的學生信息不存在,請回去重新你的選擇");
}else {
System.out.println("請輸入學生新姓名:");
String name = sc.nextLine();
System.out.println("請輸入學生新年齡:");
String age = sc.nextLine();
System.out.println("請輸入學生新居住地:");
String address = sc.nextLine();
//創(chuàng)建學生對象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//修改集合中的學生對象
array.set(index, s);
//給出提示
System.out.println("修改學生成功");
}
}
//刪除學生
public static void deleteStudent(ArrayList<Student> array) {
//刪除學生的思路:鍵盤錄入一個學號,到集合中去查找看是否有學生使用的是該學號滔驾,如果有就刪除該學生
//創(chuàng)建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要刪除的學生的學號");
String id = sc.nextLine();
/*
//遍歷集合
for(int x = 0; x < array.size(); x++) {
//獲取到每一個學生對象
Student s = array.get(x);
//拿這個學生對象的學號進行比較
if(s.getId().equals(id)) {
array.remove(x);//根據(jù)索引刪除
break;
}
}
//給出提示
System.out.println("刪除學生成功");
*/
//我們必須給出學號不存在的時候的提示
//定義一個索引
int index = -1;
//遍歷集合
for(int x = 0; x < array.size(); x++) {
//獲取到每一個學生對象
Student s = array.get(x);
//拿這個學生對象的學號進行比較
if(s.getId().equals(id)) {
index = x;
break;
}
}
if(index == -1) {
System.out.println("不好意思,你要刪除的學號對應的學生信息不存在俄讹,請回去重新你的選擇");
}else {
array.remove(index);
System.out.println("刪除學生成功");
}
}
/*
//添加學生
public static void addStudent(ArrayList<Student> array) {
//創(chuàng)建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入學生學號");
String id = sc.nextLine();
System.out.println("請輸入學生姓名");
String name = sc.nextLine();
System.out.println("請輸入學生年齡");
String age = sc.nextLine();
System.out.println("請輸入學生居住地");
String address = sc.nextLine();
//創(chuàng)建學生對象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//把學生對象作為元素添加到集合
array.add(s);
//給出提示
System.out.println("添加學生成功");
}
*/
//添加學生
public static void addStudent(ArrayList<Student> array) {
//創(chuàng)建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
//為了讓id能夠被訪問到哆致,我們就把id定義在循環(huán)的外面
String id;
//為了讓代碼能夠回到這里,用循環(huán)
while(true) {
System.out.println("請輸入學生學號");
// String id = sc.nextLine();
id = sc.nextLine();
//判斷學號有沒有被人占用
//定義標記
boolean flag = false;
//遍歷集合患膛,得到每一個學生
for(int x = 0; x < array.size(); x++) {
Student s = array.get(x);
//獲取該學生的學號摊阀,和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
flag = true;//說明學號被占用了
break;
}
}
if(flag) {
System.out.println("你輸入的學號已經被占用,請重新輸入");
}else {
break;//結束循環(huán)
}
}
System.out.println("請輸入學生姓名");
String name = sc.nextLine();
System.out.println("請輸入學生年齡");
String age = sc.nextLine();
System.out.println("請輸入學生居住地");
String address = sc.nextLine();
//創(chuàng)建學生對象
Student s = new Student();
s.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//把學生對象作為元素添加到集合
array.add(s);
//給出提示
System.out.println("添加學生成功");
}
//查看所有學生
public static void findAllStudent(ArrayList<Student> array) {
//首先來判斷集合中是否有數(shù)據(jù),如果沒有數(shù)據(jù)胞此,就給出提示臣咖,并讓該方法不繼續(xù)往下執(zhí)行
if(array.size() == 0) {
System.out.println("不好意思,目前沒有學生信息可供查詢豌鹤,請回去重新操作");
return;//如果不想讓代碼繼續(xù)往下執(zhí)行就用return亡哄,相當于回去吧枝缔。
}
//\t其實就是一個tab鍵的位置
System.out.println("學號\t姓名\t年齡\t居住地");
for(int x = 0; x < array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
}