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

