要给我注释让我看得懂或者给我点思路
已帮你完成自习室座位预约系统代码,望采纳
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
public class SeatReservationSystem {
private static final int NUM_OF_SEATS = 26;
private static final String USERNAME_PASSWORD_FILE = "username_password.txt";
private static final String SEATS_FILE = "seats.txt";
private static String username;
private static String password;
private static boolean[] seats = new boolean[NUM_OF_SEATS];
public static void main(String[] args) throws IOException {
// 从用户名和密码文件中读取用户名和密码
readUsernameAndPassword();
// 循环读取用户输入,直到用户输入 exit 退出系统
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Enter username:");
String inputUsername = scanner.nextLine();
System.out.println("Enter password:");
String inputPassword = scanner.nextLine();
if (inputUsername.equals(username) &&
inputPassword.equals(password)) {
// 用户名和密码正确,打印座位信息
printSeats();
System.out.println("Enter seat number to reserve (1-" +
NUM_OF_SEATS + "), or enter exit to exit:");
String input = scanner.nextLine();
// 如果用户输入 exit,则退出系统
if (input.equals("exit")) {
break;
}
// 如果用户输入的是合法的座位号,则预定该座位
if (Pattern.matches("\\d+", input) &&
(Integer.parseInt(input) >= 1) &&
(Integer.parseInt(input) <= NUM_OF_SEATS)) {
int seatNumber = Integer.parseInt(input) - 1;
if (seats[seatNumber]) {
System.out.println("Seat " + (seatNumber + 1) +
" is already reserved.");
} else {
seats[seatNumber] = true;
System.out.println("Seat " + (seatNumber + 1) +
" reserved successfully.");
// 保存座位信息到文件
saveSeats();
}
} else {
System.out.println("Invalid input.");
}
} else {
System.out.println("Incorrect username or password.");
}
}
}
private static void readUsernameAndPassword() throws IOException {
File file = new File(USERNAME_PASSWORD_FILE);
if (!file.exists()) {
file.createNewFile();
}
BufferedReader reader = new BufferedReader(new FileReader(file));
username = reader.readLine();
password = reader.readLine();
reader.close();
}
private static void printSeats() {
System.out.println("Seats:");
for (int i = 0; i < NUM_OF_SEATS; i++) {
System.out.print((seats[i] ? "X" : "O") + " ");
}
System.out.println();
}
private static void saveSeats() throws IOException {
File file = new File(SEATS_FILE);
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < NUM_OF_SEATS; i++) {
writer.write(seats[i] ? "1" : "0");
writer.newLine();
}
writer.close();
}
}
在上面的代码中,我们定义了一个 SeatReservationSystem
类,该类中包含了用于实现自习座位预约系统的代码。
首先,我们使用 readUsernameAndPassword
方法从文件中读取用户名和密码。然后我们使用 printSeats
方法打印当前座位的使用情况。用户输入合法的座位号后,我们使用 saveSeats
方法将座位的使用情况保存到文件中。
下面是一个使用 SeatReservationSystem 类的示例:
public class Example {
public static void main(String[] args) throws IOException {
// 创建 SeatReservationSystem 对象
SeatReservationSystem reservationSystem = new SeatReservationSystem();
// 调用 SeatReservationSystem 的 main 方法
reservationSystem.main(null);
}
}
在上面的代码中,我们首先创建了一个 SeatReservationSystem 对象,然后调用该对象的 main 方法,以实现自习座位预约系统的功能。
使用这个示例代码,可以实现以下功能:
提供参考实例【用Java实现简易的图书管理系统(超详细)】,链接:https://blog.csdn.net/yss233333/article/details/124956914
JAVA图书管理系统,适合新手:
http://t.csdn.cn/aLmcw
如有帮助
1.简单代码案例供参考。
2.java用文件代替数据库的方案一般有
1、使用io写入文本文件,不过这种缺点是读取不方便拆分有效信息。
2、写入EXCEL文档,可参考:[](https://download.csdn.net/download/gongjin28_csdn/85324396)
3、使用JAXP 框架将数据写入xml文件,这样可以根据key找到value,可以简单实现关系型数据库。
4、也可以使用dom4j解析。
/**
* @author 小小野猪
* @date 2022/12/8
* @desc default
**/
public class BookManagement {
public static void main(String[] args) {
ArrayList<Book> list = new ArrayList<Book>();
while (true) {
System.out.println("--------欢迎来到图书管理系统:----------");
System.out.println("1、查看所有图书");
System.out.println("2、添加所有图书");
System.out.println("3、删除所有图书");
System.out.println("4、修改所有图书");
System.out.println("5、退出");
System.out.println("请输入你的选择");
Scanner sc = new Scanner(System.in);
String choiceString = sc.nextLine();
switch (choiceString) {
case "1":
// 查看所有图书
findAllBook(list);
break;
case "2":
// 添加所有图书
addBook(list);
break;
case "3":
// 删除所有图书
deleteBook(list);
break;
case "4":
// 修改所有图书
break;
case "5":
// 退出,利用的是case穿透的特效
default:
System.out.println("谢谢你的使用");
System.exit(0);
break;
}
}
}
private static void updateBook(ArrayList<Book> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改图书的图书编号:");
String bookId = sc.nextLine();
int index = -1;
for (int x = 0; x < list.size(); x++) {
Book book = list.get(x);
if (book.getBookId().equals(bookId)) {
index = x;
break;
}
}
if (index != -1) {
System.out.println("请输入图书名称:");
String bookName = sc.nextLine();
System.out.println("请输入价格:");
String price = sc.nextLine();
System.out.println("请输入借书人:");
String bookUserId = sc.nextLine();
Book book = new Book();
book.setBookId(bookId);
book.setBookUserId(bookUserId);
book.setPrice(price);
list.set(index, book);
System.out.println("修改图书成功");
} else {
System.out.println("不好意思,你要修改的图书编号对应的图书信息不存在,请回去重新选择你的操作");
return;
}
}
public static void deleteBook(ArrayList<Book> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除图书的图书编号:");
String id = sc.nextLine();
int index = -1;
for (int x = 0; x < list.size(); x++) {
Book book = list.get(x);
if (book.getBookId().equals(id)) {
index = x;
break;
}
}
if (index != -1) {
list.remove(index);
System.out.println("删除图书成功");
} else {
System.out.println("不好意思,你要删除的图书编号对应的图书信息不存在,请回去重新选择你的操作");
return;
}
}
public static void addBook(ArrayList<Book> list) {
Scanner sc = new Scanner(System.in);
String id;
while (true) {
System.out.println("请输入图书编号:");
id = sc.nextLine();
boolean flag = false;
for (int x = 0; x < list.size(); x++) {
Book book = list.get(x);
if (book.getBookId().equals(id)) {
flag = true;
}
}
if (flag == true) {
System.out.println("你输入的图书编号已经被存在,请重新输入");
} else {
break;
}
}
System.out.println("请输入图书名称:");
String bookName = sc.nextLine();
System.out.println("请输入图书价格:");
String price = sc.nextLine();
System.out.println("请输入作者:");
String author = sc.nextLine();
Book book = new Book();
book.setBookId(id);
book.setBookName(bookName);
book.setPrice(price);
book.setAuthor(author);
book.setPrice("38元");
list.add(book);
System.out.println("添加图书成功");
}
public static void findAllBook(ArrayList<Book> list) {
if (list.size() == 0) {
System.out.println("不好意思,目前没有图书信息可供查看,请回去重新选择你的操作");
return;
}
for (int i = 0; i < list.size(); i++) {
Book book = list.get(i);
System.out.println(book.toString());
}
}
}
Java 图书管理系统 非常详细
如有帮助,望采纳
https://blog.csdn.net/Naion/article/details/124170954