一个简陋的java图书管理系统
作者:A_book 时间:2021-08-10 23:49:40
本文代码为原创一个简陋的管理系统,只做功能的测试。并没有去完善所有应有的功能,只做了输入输出查找,仅供参考!
菜单部分:
import java.util.Scanner;
public class Menu {
int Min = 1;
int Max = 3;
public void getMenu(){
System.out.println("1、显示/2、输入/3、查找");
}
public void getFindMenu(){
System.out.println("1、编号/2、书名/3、作者");
}
public int setMenu(){
System.out.println("输入序号:");
Scanner reader = new Scanner(System.in);
int num = reader.nextInt();
if(num >= Min || num <= Max)
return num;
else
return -1;
}
}
重点的管理部分:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Scanner;
import java.io.IOException;
public class Book {
public void find(){
Menu menu = new Menu();
menu.getFindMenu();
Scanner reader = new Scanner(System.in);
int num = menu.setMenu();
switch(num){
case 1:
System.out.println("请输入编号");
Find(reader.next(), 0);
break;
case 2:
System.out.println("请输入书名");
Find(reader.next(), 1);
break;
case 3:
System.out.println("请输入作者");
Find(reader.next(), 2);
break;
}
}
public void Find(String s,int n){
try {
Scanner in = new Scanner(new File("res/Book.txt"));
while (in.hasNextLine()) {
String str = in.nextLine();
String[] book = str.trim().split("#");
if(book[n].compareTo(s) == 0)
System.out.println(book[0] +" "+ book[1] +" "+ book[2]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String findNum(String s,int n){
try {
Scanner in = new Scanner(new File("res/Book.txt"));
while (in.hasNextLine()) {
String str = in.nextLine();
String[] book = str.trim().split("#");
if(book[n].compareTo(s) == 0)
return book[n];
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "没有找到";
}
public String message(){
Scanner reader = new Scanner(System.in);
String str = "";
String s = "";
System.out.println("请输入编号");
str = reader.next();
if(findNum(str,0).compareTo("没有找到") != 0){
System.out.println("此编号存在输入错误");
return "@@!!";
}
s += str + "#";
System.out.println("请输入书名");
str = reader.next();
s += str + "#";
System.out.println("请输入作者");
str = reader.next();
s += str + "#\n";
return s;
}
public void setBook() {
FileOutputStream fop = null;
File file;
String content = message();
if(content.compareTo("@@!!") == 0)
return ;
try {
file = new File("res/Book.txt");
fop = new FileOutputStream(file,true);
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void getBook() {
try {
Scanner in = new Scanner(new File("res/Book.txt"));
while (in.hasNextLine()) {
String str = in.nextLine();
splitt(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static String[] splitt(String str) {
String[] book = str.trim().split("#");
for (int i = 0; i < book.length; i++) {
System.out.println(book[i]);
}
System.out.println("\n*********************");
return book;
}
}
主函数部分:/strong>
public class ManageBook {
public static void main(String[] agse){
Menu menu = new Menu();
Book book = new Book();
while(true){
menu.getMenu();
int num = menu.setMenu();
switch(num){
case 1:
book.getBook();
break;
case 2:
book.setBook();
break;
case 3:
book.find();
break;
case -1:
System.out.println("输入有误");
break;
}
}
}
}
关于管理系统的更多内容请点击《管理系统专题》进行学习
标签:java,图书管理系统
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于socket和javaFX简单文件传输工具
2022-11-04 12:37:42
使用Spring boot + jQuery上传文件(kotlin)功能实例详解
2022-09-03 14:12:21
![](https://img.aspxhome.com/file/2023/3/77963_0s.png)
Java开发之spring security实现基于MongoDB的认证功能
2022-06-29 15:52:32
Java中的几种读取properties配置文件的方式
2022-09-06 13:36:19
java中使用数组进行模拟加密的方法
2023-11-18 15:37:39
Lombok中@EqualsAndHashCode注解的使用及说明
2023-11-30 04:47:05
![](https://img.aspxhome.com/file/2023/4/64204_0s.png)
详解C#中使用对象或集合的初始值设定项初始化的操作
2021-12-25 20:04:52
如何在Springboot实现拦截器功能
2023-01-10 01:11:38
![](https://img.aspxhome.com/file/2023/1/86831_0s.png)
Springboot动态切换数据源的具体实现与原理分析
2022-11-24 13:45:26
![](https://img.aspxhome.com/file/2023/8/70918_0s.png)
java实现扫雷游戏入门程序
2023-04-24 16:20:40
![](https://img.aspxhome.com/file/2023/6/86786_0s.jpg)
Idea Jrebel 报错:Cannot reactivate,offline seat in use
2022-01-11 05:08:22
![](https://img.aspxhome.com/file/2023/6/60576_0s.png)
java使用Socket实现SMTP协议发送邮件
2022-06-08 19:25:50
详谈signed 关键字
2022-09-19 13:45:36
Java集合删除元素ArrayList实例详解
2022-11-09 19:12:39
![](https://img.aspxhome.com/file/2023/0/69310_0s.png)
SpringBoot接口如何对参数进行校验
2021-08-30 17:25:00
![](https://img.aspxhome.com/file/2023/5/74895_0s.png)
spring拓展之如何定义自己的namespace
2022-01-12 05:13:33
![](https://img.aspxhome.com/file/2023/3/79873_0s.png)
C#集合之有序列表的用法
2023-10-02 15:47:02
Spring Boot和Thymeleaf整合结合JPA实现分页效果(实例代码)
2023-11-25 07:05:15
![](https://img.aspxhome.com/file/2023/1/60051_0s.png)
C#汉字转换拼音技术详解(高性能)
2022-01-19 02:03:16
Java实现十进制与二进制互转的示例详解
2022-04-02 07:40:56