package com.company.zjx;
public class Student {
private String name;
private int IdCard;
private String Sex;
private boolean isFrozen;
public Student(String name, boolean isFrozen) {
this.name = name;
this.isFrozen = false;
}
public Student(String name) {
this.name = name;
}
public Student(String name, int idCard, String sex, boolean isFrozen) {
this.name = name;
IdCard = idCard;
Sex = sex;
this.isFrozen = isFrozen;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdCard() {
return IdCard;
}
public void setIdCard(int idCard) {
IdCard = idCard;
}
public void freeze() {
this.isFrozen = true;
}
public void unfreeze() {
this.isFrozen = false;
}
public boolean isFrozen() {
return isFrozen;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", IdCard=" + IdCard +
", Sex='" + Sex + '\'' +
", isFrozen=" + isFrozen +
'}';
}
}
package com.company.zjx;
import org.junit.Test;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class StudentManager {
private List<Student> students = new ArrayList<>();
private final String FILENAME = "students.txt";
private final String FILENAME_ = "StudentsInformation.txt";
public StudentManager() {
File file = new File(FILENAME);
File file1 = new File(FILENAME_);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.err.format("Error creating the file: %s%n", e);/*
格式字符串,其中%s是一个占位符,用于表示一个字符串(在这里是异常消息),%n是一个平台独立的换行符。
e: 这是catch块捕获的异常对象。
在这里,它被用作System.err.format方法的参数,
它将替换上述格式字符串中的%s占位符,从而在控制台上显示完整的异常消息。*/
}
}
if (!file1.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
loadFromFile();
}
public void loadFromFile() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILENAME))) {
students = (List<Student>) ois.readObject();
} catch (Exception e) {
}
}
private void saveToFile() {
try (FileWriter writer = new FileWriter(FILENAME, false);
BufferedWriter bw = new BufferedWriter(writer)) {
for (Student student : students) {
bw.write(student.toString());
bw.newLine();
}
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
public void addStudent(String name) {
if (findStudent(name) == null) {
students.add(new Student(name));
saveToFile();
System.out.println("Student " + name + " has been added.");
} else {
System.out.println("Student " + name + " already added.");
}
}
public void deleteStudent(String name) {
Student student = findStudent(name);
if (student != null) {
students.remove(student);
saveToFile();
System.out.println(student + " has been deleted.");
} else {
System.out.println("Student " + name + " not found.");
}
}
/*使用Arraylist集合框架remove时,可能会导致索引问题,考虑使用迭代器
* */
public void deleteInformation(String name) {
//检查是否包含元素
if (findInformation(name) == null) {
System.out.println("Student " + name + " not found.");
}
//暂存文件内容
ArrayList<String> lines = new ArrayList<>();
try (Scanner scanner = new Scanner(new File(FILENAME_))) {
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//使用迭代器删除
Iterator<String> iterator = lines.iterator();
while (iterator.hasNext()) {
String currentLine = iterator.next();
if (currentLine.contains(name)) {
iterator.remove();
System.out.println("Student " + name + " has been deleted.");
}
}
//更新文件内容
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME_, false))) {
for (String line : lines) {
bw.write(line);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public Student findStudent(String name) {
for (Student student : students) {
if (student.getName().equals(name)) {
return student;
}
}
return null;
}
public String findInformation(String name) {
try {
File file = new File(FILENAME_);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.indexOf(name) != -1) {
return line;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
public void freezeAccount(String name) {
Student student = findStudent(name);
if (student != null) {
student.freeze();
saveToFile();
System.out.println("Student " + name + " has been frozen");
} else {
System.out.println("Student " + name + " not found");
}
}
public void unfreezeAccount(String name) {
Student student = findStudent(name);
if (student != null) {
student.unfreeze();
saveToFile();
System.out.println("Student " + name + " has been unfrozen");
} else {
System.out.println("Student " + name + " not found");
}
}
public void displayAllStudents() {
try {
File file = new File(FILENAME_);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("An Error occurred" + e.getMessage());
}
}
public void displayNowStudent() {
try {
File file = new File(FILENAME);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("An Error occurred" + e.getMessage());
}
}
public void SaveInformation() {
try (FileWriter writer = new FileWriter(FILENAME_, true);
BufferedWriter bw = new BufferedWriter(writer)) {
File file = new File(FILENAME);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
bw.write(line);
bw.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.company.zjx;
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("********************************");
System.out.println("1. Add the student. *");
System.out.println("2. Delete the student. *");
System.out.println("3.Find a student. *");
System.out.println("4. Freeze the student. *");
System.out.println("5. UnFreeze the student. *");
System.out.println("6. Display all of students. *");
System.out.println("7. Save and quit the System. *");
System.out.println("********************************");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("Please input the name:");
String name = scanner.nextLine();
manager.addStudent(name);
break;
case 2:
System.out.println("1. Delete now of one student.");
System.out.println("2. Delete all of one student.");
int choose_delete = scanner.nextInt();
switch (choose_delete) {
case 1:
System.out.println("Please input the name:");
name = scanner.nextLine();
manager.deleteStudent(name);
break;
case 2:
System.out.println("Please input the name:");
name = scanner.nextLine();
manager.deleteInformation(name);
break;
default:
System.out.println("Error,not found!");
}
break;
case 3:
System.out.println("Please input the name:");
name = scanner.nextLine();
System.out.println(manager.findInformation(name));
break;
case 4:
System.out.println("Please input the name:");
name = scanner.nextLine();
manager.freezeAccount(name);
break;
case 5:
System.out.println("Please input the name:");
name = scanner.nextLine();
manager.unfreezeAccount(name);
break;
case 6:
System.out.println("1.DisplayNowStudents");
System.out.println("2.DisplayAllStudents");
int choose = scanner.nextInt();
switch (choose) {
case 1:
manager.displayNowStudent();
break;
case 2:
manager.displayAllStudents();
break;
default:
System.out.println("Error,not found!");
}
break;
case 7:
System.out.println("Will save , but will can`t revise forever.");
System.out.println("'y':continue, 'n':return.");
char choose_ = scanner.next().charAt(0);
switch (choose_) {
case 'y':
manager.SaveInformation();
System.out.println("Saved, bye!");
scanner.close();
return;
case 'n':
break;
default:
System.out.println("Error,not found!");
}
default:
System.out.println("Error, not found!");
}
}
}
}
附:JDBC
JAVAmysql_学生信息管理系统:
Java:JDBC_从入门到学生信息管理系统-CSDN博客https://blog.csdn.net/Bob516516/article/details/130608938