Java改進(jìn):構(gòu)造方法實(shí)例和圖析,學(xué)生管理系統(tǒng)(含查詢所有和單個(gè)學(xué)生信息,增刪改查)

定義學(xué)生類,包含學(xué)生id,名稱,年齡,成績(jī),在構(gòu)造方法里對(duì)屬性進(jìn)行賦值,定義學(xué)生介紹自己的方法,介紹的時(shí)候打印自己的信息
package fuXi;
public class jieSao {
public static void main(String[] args) {
student s1 = new student(1, "詩(shī)書(shū)", 19, 99.8);
student s2 = new student(2, "畫(huà)唱", 20, 99.9);
student s3 = new student(2, "詩(shī)書(shū)畫(huà)唱", 21, 100);
}
}
class student {
private int age;
private double chengJi;
private int ID;
private String name;
public student(int ID, String name, int age, double chengJi) {
System.out.println("大家好!我是" + name + ",今年" + age + "歲,學(xué)生編號(hào)是" + ID + ","
+ "成績(jī)是" + chengJi + "分,希望與你們共同努力,互相幫助,一起進(jìn)步,謝謝!");
}
public int getAge() {
return age;
}
public double getChengJi() {
return chengJi;
}
public int getID() {
return ID;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setChengJi(double chengJi) {
this.chengJi = chengJi;
}
public void setID(int iD) {
ID = iD;
}
public void setName(String name) {
this.name = name;
}
}


登錄
package fuXi;
import java.util.Scanner;
public class dengLu {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
yonghu y = new yonghu();
while (true) {
System.out.println("歡迎來(lái)到詩(shī)書(shū)畫(huà)唱學(xué)生管理系統(tǒng)");
System.out.println("請(qǐng)登錄!先點(diǎn)贊!先投幣!先收藏!先三連!先關(guān)注!");
System.out.println("請(qǐng)輸入用戶名:");
String uname = s.next();
System.out.println("請(qǐng)輸入密碼:");
String pwd = s.next();
if (y.getUname().equals(uname) && y.getPwd().equals(pwd)) {
break;
} else {
System.out.println("用戶名或密碼輸入錯(cuò)誤,請(qǐng)重新輸入!點(diǎn)個(gè)贊!點(diǎn)個(gè)關(guān)注!");
}
}
System.out.println("登錄成功!");
}
}
class yonghu {
private String pwd = "1";
private String uname = "1";
public String getPwd() {
return pwd;
}
public String getUname() {
return uname;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void setUname(String uname) {
this.uname = uname;
}
}


學(xué)生管理系統(tǒng)(登錄成功后有以下選項(xiàng),1.查詢所有學(xué)生信息??2,根據(jù)用戶編號(hào)查詢學(xué)生信息 3.根據(jù)用戶編號(hào)修改學(xué)生信息????4.刪除學(xué)生? 5.退出系統(tǒng))

package fuXi;
import java.util.ArrayList;
import java.util.Scanner;
public class lizi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
yonghu y = new yonghu();
while (true) {
System.out.println("歡迎來(lái)到詩(shī)書(shū)畫(huà)唱學(xué)生管理系統(tǒng)");
System.out.println("請(qǐng)登錄!先點(diǎn)贊!先投幣!先收藏!先三連!先關(guān)注!");
System.out.println("請(qǐng)輸入用戶名:");
String uname = s.next();
System.out.println("請(qǐng)輸入密碼:");
String pwd = s.next();
if (y.getUname().equals(uname) && y.getPwd().equals(pwd)) {
break;
} else {
System.out.println("用戶名或密碼輸入錯(cuò)誤,請(qǐng)重新輸入!點(diǎn)個(gè)贊!點(diǎn)個(gè)關(guān)注!");
}
}
System.out.println("登錄成功!");
ArrayList<student> arr = new ArrayList<student>();
arr.add(new student(1, "詩(shī)名", '男', 95));
arr.add(new student(2, "書(shū)名", '男', 92));
arr.add(new student(3, "畫(huà)名", '男', 90));
arr.add(new student(4, "唱名", '男', 111));
arr.add(new student(5, "帥名", '男', 134));
while (true) {
System.out.println("請(qǐng)選擇你要執(zhí)行的功能1.查詢所有學(xué)生信息 "
+ " 2,根據(jù)用戶編號(hào)查詢學(xué)生信息3根據(jù)用戶編號(hào)修改學(xué)生信息" + "4.刪除學(xué)生 5.退出系統(tǒng)");
Scanner s1 = new Scanner(System.in);
int num = s1.nextInt();
if (num == 1) {
System.out.println("執(zhí)行查詢功能");
for (student i : arr) {
System.out.println(i);
}
} else if (num == 2) {
System.out.println("執(zhí)行查詢單個(gè)學(xué)生信息的功能");
System.out.println("請(qǐng)輸入你要查詢的學(xué)生的編號(hào)");
int bianhao = s1.nextInt();
for (int j = 0; j < arr.size(); j++) {
if (bianhao == arr.get(j).bianhao) {
System.out.println(arr.get(j));
}
}
} else if (num == 3) {
System.out.println("執(zhí)行修改功能");
System.out.println("請(qǐng)輸入你要修改的學(xué)生的編號(hào)");
int bianhao = s1.nextInt();
for (int i = 0; i < arr.size(); i++) {
if (bianhao == arr.get(i).bianhao) {
System.out.println("你要修改的成績(jī)?yōu)槎嗌?");
double d = s1.nextDouble();
arr.get(i).ChengJi = d;
}
}
} else if (num == 4) {
System.out.println("執(zhí)行刪除功能");
System.out.println("請(qǐng)輸入你要?jiǎng)h除的學(xué)生的編號(hào)");
int bianhao = s1.nextInt();
for (int i = 0; i < arr.size(); i++) {
if (bianhao == arr.get(i).bianhao) {
arr.remove(arr.get(i));
}
}
System.out.println("刪除完畢");
}
else if (num == 5) {
System.exit(0);
}
}
}
}
class student {
int bianhao;
double ChengJi;
String name;
char sex;
public student(int bianhao, String name, char sex, double ChengJi) {
this.bianhao = bianhao;
this.name = name;
this.sex = sex;
this.ChengJi = ChengJi;
}
@Override
public String toString() {
return "編號(hào):" + bianhao + ",\t名字:" + name + ",\t性別:" + sex + ",\t成績(jī):"
+ ChengJi;
}
}
class yonghu {
private String pwd = "1";
private String uname = "1";
public String getPwd() {
return pwd;
}
public String getUname() {
return uname;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void setUname(String uname) {
this.uname = uname;
}
}

