Java新手入門第一個小程式(學生管理系統)
阿新 • • 發佈:2018-12-27
今天來寫一寫學習Java前期自己練習的一個小專案——學生管理系統。
寫這個系統之前我們需要有一個類來規範學生物件,學生類一共有學號、姓名、郵箱、成績、生日五個屬性。其次我們還需要準備一個班級類和一個老師類,老師類有工號、姓名、所授課程三個屬性。班級類則只需要一個班主任和一幫學生即可。
既然是前期的練習自然涉及不到資料庫,所有的資料都是寫在程式碼裡頭供執行測試的,這裡大家不需要太過糾結。
學生類
import java.time.LocalDate; public class Student { //學號 private int id; //姓名 private String name; //郵箱 private String email; //成績 private double sorce; //生日 private LocalDate berthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public double getSorce() { return sorce; } public void setSorce(double sorce) { this.sorce = sorce; } public LocalDate getBerthday() { return berthday; } public void setBerthday(LocalDate berthday) { this.berthday = berthday; } public Student(int id, String name, String email, double sorce, LocalDate berthday) { super(); this.id = id; this.name = name; this.email = email; this.sorce = sorce; this.berthday = berthday; } public Student() { super(); } //這裡是便於列印學生資訊所定義的一個方法,類似toString public String allStu() { return "學號:" + id + ", 姓名:" + name + ", 郵箱:" + email + ", 成績:" + sorce + ", 生日:" + berthday ; } }
老師類
public class Teacher { private int id; private String name; private String subject; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public Teacher(int id, String name, String subject) { super(); this.id = id; this.name = name; this.subject = subject; } public Teacher() { super(); } public String allTea() { return "編號:" + id + ", 姓名:" + name + ", 所教科目:" + subject; } }
班級類
import java.util.*; public class Clazz { private Teacher teacher; private List<Student> stu = new ArrayList<>(); private int id; public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public List<Student> getStu() { return stu; } public void setStu(List<Student> stu) { this.stu = stu; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Clazz(Teacher teacher, List<Student> stu, int id) { super(); this.teacher = teacher; this.stu = stu; this.id = id; } public Clazz() { super(); } }
準備好這三個類以後我們要考慮執行介面,這個介面是體現在操作檯上的簡單介面,所以我們需要靠判斷使用者輸入的數字來執行功能,至於為什麼沒有炫酷的介面,因為在寫這個程式的時候博主還沒有學習炫酷的介面怎麼做(其實我現在也還不知道怎麼做!嘿嘿嘿~)
import java.time.LocalDate;
import java.util.*;
import org.lanqiao.text0413_0416Cla.*;
import org.lanqiao.text0413_0416Services.*;
public class TestMain {
// 班級容器
static List<Clazz> cla = new ArrayList<Clazz>();
// 學生容器
static List<Student> ostu = new ArrayList<Student>();
// 鍵盤輸入物件
static Scanner sc = new Scanner(System.in);
static Scanner sl = new Scanner(System.in);
static Scanner sk = new Scanner(System.in);
static{
// 老師物件
Teacher Bob = new Teacher(1, "Bob,Jasoh", "English");
// 學生物件
Student ost1 = new Student(1, "王強", "[email protected]", 89.2,
LocalDate.of(1996, 2, 21));
Student ost2 = new Student(2, "張三", "[email protected]", 90,
LocalDate.of(1996, 8, 11));
Student ost3 = new Student(3, "王二", "[email protected]", 43,
LocalDate.of(1996, 7, 05));
Student ost4 = new Student(4, "麻子", "[email protected]", 5, LocalDate.of(
1996, 3, 01));
// 將學生物件新增入學生容器
ostu.add(ost1);
ostu.add(ost2);
ostu.add(ost3);
ostu.add(ost4);
// 班級物件
Clazz one = new Clazz(Bob, ostu, 01);
// 將班級物件新增到容器
cla.add(one);
}
public static void main(String[] args) {
// 操作介面
try {
while (true) {
System.out.println("========班級管理系統=======");
System.out.println("1、班級資訊管理");
System.out.println("2、老師資訊管理");
System.out.println("3、學生資訊管理");
System.out.println("4、退出程式");
int oneinput = sc.nextInt();
switch (oneinput) {
case 1:
a: while (true) {
System.out.println("1、檢視所有班級資訊");
System.out.println("2、檢視指定班級資訊");
System.out.println("3、增加班級");
System.out.println("4、刪改班級");
System.out.println("5、返回上一級");
int twoinput = sc.nextInt();
switch (twoinput) {
case 1:
ClazzServices.findClazz(cla);
break;
case 2:
ClazzServices.findOneClazz(cla);
break;
case 3:
ClazzServices.addClazz(cla);
break;
case 4:
ClazzServices.deleteClazz(cla);
break;
case 5:
break a;
default:
System.out.println("沒有此功能!");
break;
}
}
break;
case 2:
b: while (true) {
System.out.println("1、檢視所有老師資訊");
System.out.println("2、修改老師資訊");
System.out.println("3、返回上一級");
int threeinput = sc.nextInt();
switch (threeinput) {
case 1:
TeaServices.findTea(cla);
break;
case 2:
TeaServices.updateTea(cla);
break;
case 3:
break b;
default:
break;
}
}
break;
case 3:
b: while (true) {
System.out.println("1、增加一個學生");
System.out.println("2、刪除一個學生");
System.out.println("3、更新學生個人資訊");
System.out.println("4、學生轉班");
System.out.println("5、返回上一級");
int fourinput = sc.nextInt();
switch (fourinput) {
case 1:
StuService.addStu(cla);
break;
case 2:
StuService.deleteStu(cla);
break;
case 3:
StuService.updateStu(cla);
break;
case 4:
StuService.stuClazz(cla);
break;
case 5:
break b;
default:
break;
}
}
break;
case 4:
System.out.println("謝謝使用");
System.exit(0);
break;
default:
System.out.println("沒有此功能!");
break;
}
}
} catch (Exception e) {
System.out.println("輸入有誤!!");
}
}
}
可以看出來在介面執行之前我們手動往集合裡添加了一些資料,還是那句話沒有資料庫我只能寫一點本地資料。可以看出我這個介面是靠判斷使用者輸入的數字來決定執行哪個類的哪個方法,由於Scanner多次獲取int型別會出現多次輸入的內容出現錯誤,所以博主選擇多建立了幾個Scanner物件(其實我也不知道其他解決辦法QAQ)。
接下來是班級、老師、學生這物件的資訊操作,
班級:
import java.time.LocalDate;
import java.util.*;
import org.lanqiao.text0413_0416.*;
import org.lanqiao.text0413_0416Cla.Clazz;
import org.lanqiao.text0413_0416Cla.Student;
import org.lanqiao.text0413_0416Cla.Teacher;
public class ClazzServices {
static Scanner sc = new Scanner(System.in);
static Scanner sl = new Scanner(System.in);
static Scanner sk = new Scanner(System.in);
//刪除方法
public static void deleteClazz(List<Clazz> cla) {
// 輸入要刪除的班級編號
System.out.println("請輸入要刪除的班級編號!");
int id = sl.nextInt();
// 遍歷班級容器,若id相同直接刪除
for (int i = 0; i < cla.size(); i++) {
if (cla.get(i).getId() == id) {
cla.remove(i);
System.out.println("刪除成功!");
} else if (i == cla.size() - 1 && cla.get(i).getId() != id) {
System.out.println("刪除失敗!");
return;
}
}
}
//新增方法
public static void addClazz(List<Clazz> cla) {
// 請輸入新班級的編號
System.out.println("請輸入新班級的編號");
int id = sc.nextInt();
// 如果班級編號已經存在,則不能新增
for (Clazz a : cla) {
if (a.getId() == id) {
System.out.println("該班級已存在,請核對資訊!");
return;
}
}
System.out.println("請輸入新班級的班主任資訊");
System.out.println("請按以下格式輸入:3 Davi,Jasoh Math");
// 先獲得一個老師資訊,並建立這個物件
String tinfo = sl.nextLine();
String[] tinfos = tinfo.split(" ");
Teacher nt = new Teacher(Integer.parseInt(tinfos[0]), tinfos[1],
tinfos[2]);
// 若果老師編號重複則不能建立
for (Clazz s : cla) {
if (s.getTeacher().getId() == nt.getId()) {
System.out.println("該老師編號已存在,請重新輸入");
return;
}
}
// 拿到一個學生的總數作為迴圈的範圍
System.out.println("請輸入新班級的學生數量");
int num = sl.nextInt();
// 拿到學生資訊,以便建立學生物件
System.out.println("請輸入每個學生的資訊");
System.out.println("格式如:“1 王強 [email protected] 89.2 1996 2 21”");
// 清空學生容器
List<Student> nstu = new ArrayList<>();
// 定義一個變數用於記錄迴圈中建立成功的學生數量
int snum = 0;
a: while (snum < num) {
// 拿到學生的資訊後分割輸入的字串,用於建立學生物件
String info = sk.nextLine();
String[] infos = info.split(" ");
if (infos.length < 7) {
// 若輸入的資訊錯誤則重新輸入
System.out.println("資訊輸入錯誤!請重新輸入!");
continue a;
}
// 用分割後的資訊建立學生物件1
Student stus = new Student(Integer.parseInt(infos[0]), infos[1],
infos[2], Double.parseDouble(infos[3]), LocalDate.of(
Integer.parseInt(infos[4]),
Integer.parseInt(infos[5]),
Integer.parseInt(infos[6])));
// 判斷學生學號是否重複
for (Clazz s : cla) {
for (int i = 0; i < s.getStu().size(); i++) {
if (s.getStu().get(i).getId() == stus.getId()) {
System.out.println("該學生資訊已存在,請重新輸入");
continue a;
}
}
}
// 新增入集合
nstu.add(stus);
snum++;
}
// 建立班級物件,並新增入班級容器
Clazz ncla = new Clazz(nt, nstu, id);
cla.add(ncla);
System.out.println("新增成功!");
}
//檢視班級資訊
public static void findClazz(List<Clazz> cla) {
// 用於記錄公有多少個學生
int total = 0;
System.out.println("本校共有:" + cla.size() + "個班,班級資訊如下:");
for (Clazz a : cla) {
System.out.println(a.getId() + "班共有" + a.getStu().size() + "個學生。");
System.out.println("班主任是:" + a.getTeacher().allTea());
for (int i = 0; i <= a.getStu().size() - 1; i++) {
// allStu是寫在學生類中的列印方法
System.out.println(a.getStu().get(i).allStu());
}
total += a.getStu().size();
}
System.out.println("本校共有:" + total + "個學生!");
}
//檢視某個班級的資訊
public static void findOneClazz(List<Clazz> cla) {
System.out.println("請輸入一個班級id");
int id = sc.nextInt();
for (Clazz a : cla) {
// 如果輸入的班級id相同則列印這個班級的資訊
if (a.getId() == id) {
System.out.println(a.getId() + "班共有" + a.getStu().size()
+ "個學生。");
for (int i = 0; i <= a.getStu().size() - 1; i++) {
System.out.println(a.getStu().get(i).allStu());
}
}
}
}
}
學生:package org.lanqiao.text0413_0416Services;
import java.time.LocalDate;
import java.util.List;
import java.util.Scanner;
import org.lanqiao.text0413_0416Cla.Clazz;
import org.lanqiao.text0413_0416Cla.Student;
public class StuService {
static Scanner sc = new Scanner(System.in);
static Scanner sl = new Scanner(System.in);
static Scanner sk = new Scanner(System.in);
//學生轉班
public static void stuClazz(List<Clazz> cla) {
// 獲取學生所在的班級id
System.out.println("輸入要轉班的學生原班級的編號:");
int id = sc.nextInt();
// 獲取學生學號
System.out.println("輸入要轉班的學生學號:");
int sid = sl.nextInt();
// 獲取學生要轉到的班級編號
System.out.println("輸入學生要轉到的班級的編號:");
int cid = sc.nextInt();
// 建立一個新的學生物件用於接收這個學生資訊
Student nstu = new Student();
// 遍歷班級找出學生所在班級
a: for (int j = 0; j < cla.size(); j++) {
if (cla.get(j).getId() == id) {
for (int i = 0; i <= cla.get(j).getStu().size(); i++) {
// 遍歷學生所在班級,找出學生資訊
if (cla.get(j).getStu().get(i).getId() == sid) {
// 找到學生資訊後將它賦給事先建立好的學生物件
nstu = cla.get(j).getStu().get(i);
// 刪除這個班級學生容器中這個學生的資訊
cla.get(j).getStu().remove(cla.get(j).getStu().get(i));
break a;
// 如果沒有找到就表示學生資訊輸入錯誤
} else if (i == cla.get(j).getStu().size() - 1
&& cla.get(j).getStu().get(i).getId() != sid) {
System.out.println(cla.get(j).getStu().size());
System.out.println("1沒有找到要轉班的學生資訊!");
return;
}
}
// 如果沒有找到學生所在班級,說明班級編號輸入有誤
} else if (j == cla.size() - 1 && cla.get(j).getId() != id) {
System.out.println("2沒有找到學生所在的班級資訊!");
return;
}
}
for (int j = 0; j < cla.size(); j++) {
// 找到學生要轉入的班級
if (cla.get(j).getId() == cid) {
// 找到後將這個學生資訊新增至此班級的學生容器中
cla.get(j).getStu().add(nstu);
System.out.println("轉班成功!");
// 沒有找到表示班級編號輸入有誤
} else if (j == cla.size() - 1 && cla.get(j).getId() != cid) {
System.out.println("3沒有找到學生要轉到的班級資訊!");
return;
}
}
}
//更新學生資訊
public static void updateStu(List<Clazz> cla) {
// 先獲取學生所在班級的編號
System.out.println("輸入學生所在班級的編號:");
int id = sk.nextInt();
// 獲取學生的id
System.out.println("輸入學生的學號:");
int sid = sk.nextInt();
// 找到學生所在的班級,再通過學號找到要修改的學生資訊
for (Clazz a : cla) {
if (a.getId() == id) {
for (Student s : a.getStu()) {
if (s.getId() == sid) {
System.out.println("要修改的學生資訊是:" + s.allStu());
System.out.println("請輸入修改後的學生資訊");
System.out
.println("如王強 [email protected] 89.2 1996 2 21 (學號不可變)");
String info = sl.nextLine();
String[] infos = info.split(" ");
// 修改學生資訊
s.setName(infos[0]);
s.setEmail(infos[1]);
s.setSorce(Double.parseDouble(infos[2]));
s.setBerthday(LocalDate.of(Integer.parseInt(infos[3]),
Integer.parseInt(infos[4]),
Integer.parseInt(infos[5])));
System.out.println("修改成功!");
}
}
}
}
}
//刪除學生
public static void deleteStu(List<Clazz> cla) {
// 先獲取該生所在的班級
System.out.println("輸入學生所在班級的編號:");
int id = sl.nextInt();
// 獲取該生的學號
System.out.println("輸入學生的學號");
int sid = sl.nextInt();
// 查詢學生資訊反饋給使用者
for (Clazz a : cla) {
if (!cla.contains(a.getId() == id)) {
System.out.println("班級資訊有誤!");
return;
}
if (a.getId() == id) {
for (int i = 0; i < a.getStu().size(); i++) {
if (a.getStu().get(i).getId() == sid) {
// 若id相同則詢問使用者是否刪除
System.out.println(a.getStu().get(i).allStu());
a.getStu().remove(i);
System.out.println("刪除成功!");
} else if (i == a.getStu().size() - 1
&& a.getStu().get(i).getId() != sid) {
System.out.println("刪除失敗!查無此人!");
return;
}
}
}
}
}
//增加一個學生
public static void addStu(List<Clazz> cla) {
// 首先輸入一個學生所在班級的id
System.out.println("輸入學生所在班級的id:");
int id = sl.nextInt();
try {
// 輸入新增的學生資訊
System.out.println("輸入要新增的學生資訊");
System.out.println("1 王強 [email protected] 89.2 1996 2 21");
String info = sk.nextLine();
String[] infos = info.split(" ");
// 將輸入的資訊分割後用於建立物件
Student stu = new Student(Integer.parseInt(infos[0]), infos[1],
infos[2], Double.parseDouble(infos[3]), LocalDate.of(
Integer.parseInt(infos[4]), Integer.parseInt(infos[5]),
Integer.parseInt(infos[6])));
// 找到要新增的學生所在的班級
for (int i = 0; i < cla.size(); i++) {
if (cla.get(i).getId() == id) {
// 找到學生所在的班級後遍歷這個班級中的學生,若學生編號重複則不可建立
for (int j = 0; j < cla.get(i).getStu().size(); j++) {
if (cla.get(i).getStu().get(j).getId() == stu.getId()) {
System.out.println("學號重複,該生資訊已存在");
return;
}
}
// 遍歷後若無學號相同的學生則將學生物件新增進班級
cla.get(i).getStu().add(stu);
System.out.println("學生資訊新增成功!");
}
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
System.out.println("輸入資訊有誤!");
}
}
}
老師:
package org.lanqiao.text0413_0416Services;
import java.util.List;
import java.util.Scanner;
import org.lanqiao.text0413_0416Cla.Clazz;
public class TeaServices {
static Scanner sc = new Scanner(System.in);
static Scanner sl = new Scanner(System.in);
static Scanner sk = new Scanner(System.in);
//刪除老師資訊
public static void updateTea(List<Clazz> cla) {
// 輸入老師的id以獲取這個老師的資訊
System.out.println("輸入要修改的老師編號:");
int id = sl.nextInt();
// 輸入更新後的資訊
System.out.println("輸入更新後的資訊:");
System.out.println("如:Macal,Jasoh English (編號不可變!)");
String info = sk.nextLine();
String[] infos = info.split(" ");
for (int i = 0; i < cla.size(); i++) {
if (cla.get(i).getTeacher().getId() == id) {
cla.get(i).getTeacher().setName(infos[0]);
cla.get(i).getTeacher().setSubject(infos[1]);
} else if ((i == cla.size() - 1)
&& (cla.get(i).getTeacher().getId() != id)) {
System.out.println("修改失敗!查無此人!");
return;
}
}
}
//查詢所有老師資訊
public static void findTea(List<Clazz> cla) {
System.out.println("本校一共有:" + cla.size() + "名老師。");
for (Clazz a : cla) {
System.out.println(a.getTeacher().allTea());
}
}
}
總結:這個專案是學習完集合以後的一次練習,一切圍繞著對學生集合、班級集合、老師集合的操作展開,因為對於集合的操作和對於面向物件的運用比較多故將其寫入部落格中,希望其他Java的入門者能寫出比我更漂亮的程式碼,也歡迎大家討論並指出錯誤。