java控制臺實現(xiàn)學生信息管理系統(tǒng)(IO版)
使用java語言用本地文件存儲數(shù)據(jù)實現(xiàn)學生信息管理系統(tǒng),在控制臺上編譯執(zhí)行,也就是學生管理系統(tǒng)IO版
可以實現(xiàn)基本的學生信息增加、刪除、修改、查詢功能(細化了查詢功能)
集合版可以參考我的另外一篇博文。
代碼如下
StudentManager
提供用戶界面
import java.io.IOException;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) throws IOException, ClassNotFoundException {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println("=================================");
System.out.println("=========歡迎使用學生管理系統(tǒng) =========");
System.out.println("=========請選擇您要進行的操作 =========");
System.out.println("= 1 添加學生信息 =");
System.out.println("= 2 刪除學生信息 =");
System.out.println("= 3 修改學生信息 =");
System.out.println("= 4 查詢學生信息 =");
System.out.println("= 5 退出系統(tǒng) =");
System.out.println("==================================");
System.out.println("請輸入您的選擇");
int choose;
if (sc.hasNextInt()) {
choose = sc.nextInt();
if (choose > 0 && choose < 6) {
StudentDAO std = new StudentDAO();
switch (choose) {
case 1:
std.add();
break;
case 2:
std.del();
break;
case 3:
std.update();
break;
case 4:
std.query();
break;
case 5:
System.out.println("成功退出……");
System.out.println("歡迎下次使用");
System.exit(0);
break;
}
} else {
System.out.println("請正確輸入");
}
} else {
System.out.println("請您輸入正確的選項");
}
}
}
}
Student類
public class Student implements Serializable{
private static final long serialVersionUID = 3420928184417313845L;
private String stuNo;
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String stuNo, String name, int age) {
super();
this.stuNo = stuNo;
this.name = name;
this.age = age;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
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 int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((stuNo == null) ? 0 : stuNo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (stuNo == null) {
if (other.stuNo != null)
return false;
} else if (!stuNo.equals(other.stuNo))
return false;
return true;
}
@Override
public String toString() {
return "學生:學號 " + stuNo + ", 姓名 " + name + ", 年齡 " + age ;
}
}
IOUtil類
用于從文件中讀寫信息到集合
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class IOUtil {
//讀取文件信息
public static ArrayList<Student> readStu() {
ArrayList<Student> stuList = null;
File file = new File("test.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try (
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
) {
stuList = (ArrayList<Student>) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return stuList;
}
//寫入文件信息
public static void writeStu(ArrayList<Student> stu) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("test.txt")));
) {
oos.writeObject(stu);
} catch (Exception e) {
e.printStackTrace();
}
}
}
StudentDAO
用于執(zhí)行數(shù)據(jù)的增刪改查操作
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Scanner;
public class StudentDAO {
static Scanner sc = new Scanner(System.in);
// 增加學生信息
public void add() throws IOException, ClassNotFoundException {
ArrayList<Student> list = IOUtil.readStu();
// 學號自增
String stuNo = null;
a: while (true) {
// 如果不存在學生則學號為1000
if (list.size() == 0) {
stuNo = "1000";
} else {
// 創(chuàng)建新的學號,讓新的學號在列表中的最后一個學生學號的基礎(chǔ)上加1
int sNo = Integer.parseInt(list.get(list.size() - 1).getStuNo()) + 1;
// 將學號類型轉(zhuǎn)為String類型
stuNo = String.valueOf(sNo);
}
System.out.println("請輸入你要添加的學號為" + stuNo + "的學生的姓名");
String name = sc.next();
while (true) {
System.out.println("請輸入你要添加的學號為" + stuNo + "的學生的年齡");
Scanner sc1 = new Scanner(System.in);
if (sc1.hasNextInt()) {
int age = sc1.nextInt();
list.add(new Student(stuNo, name, age));
IOUtil.writeStu(list);
break;
} else {
System.out.println("請正確輸入");
continue a;
}
}
System.out.println("學號為" + stuNo + "的學生信息添加成功");
System.out.println("添加信息后,學生的信息為:");
showStudentInformation();
System.out.println("是否繼續(xù)執(zhí)行添加操作(y/n)");
String result = sc.next();
if (result.equalsIgnoreCase("n") || result.equalsIgnoreCase("y")) {
if (result.equalsIgnoreCase("n")) {
break;
}
} else {
}
}
}
// 刪除學生信息
public void del() throws IOException, ClassNotFoundException {
ArrayList<Student> list = IOUtil.readStu();
if (list.size() != 0) {
a: while (true) {
showStudentInformation();
System.out.println("請輸出你要刪除的學生的學號");
String str = sc.next();
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student stu = it.next();
if (stu.getStuNo().equals(str)) {
it.remove();
System.out.println("刪除成功!");
System.out.println("刪除操作后,學生的信息為:");
IOUtil.writeStu(list);
showStudentInformation();
break a;// 跳出到指定循環(huán)外
}
}
System.out.println("查無此人……請查證后重新輸入");
}
} else {
System.out.println("還沒有添加學生信息,快去添加學生信息吧");
}
}
// 修改學生信息
public void update() {
ArrayList<Student> list = IOUtil.readStu();
if (list.size() != 0) {
a: while (true) {
showStudentInformation();
System.out.println("請輸入要修改學生的學號:");
String stuNo = sc.next();
ListIterator<Student> lit = list.listIterator();
while (lit.hasNext()) {
Student stu = lit.next();
if (stu.getStuNo().equals(stuNo)) {
System.out.println("請輸入您要修改的學生的姓名");
String name = sc.next();
System.out.println("請輸入您要修改的學生的年齡");
if (sc.hasNextInt()) {
int age = sc.nextInt();
stu.setName(name);
stu.setAge(age);
System.out.println("修改操作后,學生的信息為:");
IOUtil.writeStu(list);
showStudentInformation();
break a;
} else {
System.out.println("請正確輸入年齡");
}
}
}
System.out.println("查無此人……請查證后重新輸入");
}
} else {
System.out.println("還沒有添加學生信息,快去添加學生信息吧");
}
}
// 顯示學生信息
public void showStudentInformation() {
ArrayList<Student> list = IOUtil.readStu();
if (list.size() != 0) {
System.out.println("=============學生信息==============");
for (Student stu : list) {
System.out.println(stu);
}
System.out.println("=================================");
} else {
System.out.println("還沒有添加學生信息,快去添加學生信息吧");
}
}
// 查詢學生信息
public void query() {
ArrayList<Student> list = IOUtil.readStu();
if (list.size() != 0) {
int choose = 0;
while (true) {
System.out.println("請選擇您的查詢條件:1.查詢?nèi)? 2.學號 3.姓名 4.年齡");
System.out.println("請輸入您的選擇");
try {
choose = sc.nextInt();
break;
} catch (Exception e) {
System.out.println("請重新輸入選擇");
sc = new Scanner(System.in);
}
}
switch (choose) {
case 1:
System.out.println("=============學生信息==============");
for (Student stu : list) {
System.out.println(stu);
}
System.out.println("=================================");
break;
case 2: {
queryStuNo(list);
break;
}
case 3:
queryStuName(list);
break;
case 4:
queryStuAge(list);
break;
default:
System.out.println("查詢條件不正確,正在退出查詢……");
break;
}
} else {
System.out.println("還沒有添加學生信息,快去添加學生信息吧");
}
}
// 按學號查詢
public static void queryStuNo(ArrayList<Student> list) {
a: while (true) {
System.out.println("請輸入您要查詢的學號");
String stuNo = sc.next();
for (Student student : list) {
if ((student.getStuNo()).equals(stuNo)) {
System.out.println(student);
break a;
}
}
System.out.println("查無此人……請查證后重新輸入");
}
}
// 按姓名查詢
public static void queryStuName(ArrayList<Student> list) {
while (true) {
ArrayList<Student> list1 = new ArrayList<>();
System.out.println("請輸入您要查詢的姓名");
String stuName = sc.next();
for (Student student : list) {
if ((student.getName()).equals(stuName)) {
list1.add(student);
}
}
if (list1.size() == 0) {
System.out.println("查無此人……請查證后重新輸入");
} else {
// 遍歷集合
for (Student student : list1) {
System.out.println(student);
}
break;
}
}
}
// 按年齡查詢
public static void queryStuAge(ArrayList<Student> list) {
while (true) {
ArrayList<Student> list1 = new ArrayList<>();
int age;
System.out.println("請輸入您要查詢的年齡");
while (true) {
try {
age = sc.nextInt();
break;
} catch (Exception e) {
System.out.println("請重新輸入年齡");
sc = new Scanner(System.in);
}
}
for (Student student : list) {
if (student.getAge() == age) {
list1.add(student);
}
}
if (list1.size() == 0) {
System.out.println("沒有此年齡的人,請重新查找");
} else {
// 遍歷集合
for (Student student : list1) {
System.out.println(student);
}
break;
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
手把手教學Win10同時安裝兩個版本的JDK并隨時切換(JDK8和JDK11)
最近在學習JDK11的一些新特性,但是日常使用基本上都是基于JDK8,因此,需要在win環(huán)境下安裝多個版本的JDK,下面這篇文章主要給大家介紹了手把手教學Win10同時安裝兩個版本的JDK(JDK8和JDK11)并隨時切換的相關(guān)資料,需要的朋友可以參考下2023-03-03
spring boot實現(xiàn)上傳圖片并在頁面上顯示及遇到的問題小結(jié)
最近在使用spring boot搭建網(wǎng)站的過程之中遇到了有點小問題,最終解決方案是在main目錄下新建了一個webapp文件夾,并且對其路徑進行了配置,本文重點給大家介紹spring boot實現(xiàn)上傳圖片并在頁面上顯示功能,需要的朋友參考下吧2017-12-12
springboot 2.3之后消失的hibernate-validator解決方法
這篇文章主要介紹了springboot 2.3之后消失的hibernate-validator解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
Java利用redis zset實現(xiàn)延時任務詳解
zset作為redis的有序集合數(shù)據(jù)結(jié)構(gòu)存在,排序的依據(jù)就是score。本文就將利用zset score這個排序的這個特性,來實現(xiàn)延時任務,感興趣的可以了解一下2022-08-08
SpringBoot actuator 健康檢查不通過的解決方案
這篇文章主要介紹了SpringBoot actuator 健康檢查不通過的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Springboot?通過FastJson實現(xiàn)bean對象和Json字符串互轉(zhuǎn)問題
這篇文章主要介紹了Springboot?通過FastJson實現(xiàn)bean對象和Json字符串互轉(zhuǎn),本文嘗試驗證兩種場景給大家詳細介紹,對Springboot?FastJson實現(xiàn)bean和Json互轉(zhuǎn)問題,感興趣的朋友一起看看吧2022-08-08

