一个简陋的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,图书管理系统
0
投稿
猜你喜欢
C# 利用PdfSharp生成Pdf文件的示例
2022-01-18 17:31:30
java数据结构之java实现栈
2023-11-25 05:32:36
Java哈希表和有序表实例代码讲解
2023-05-28 11:29:29
详解Java数据结构和算法(有序数组和二分查找)
2023-04-08 13:38:07
SpringBoot集成Redis的实现示例
2022-10-22 14:50:33
java文件的重命名与移动操作实例代码
2022-06-16 04:18:12
C# 开发(创蓝253)手机短信验证码接口的实例
2023-05-22 11:15:54
90分钟实现一门编程语言(极简解释器教程)
2022-01-15 05:11:56
Java中FilterInputStream和FilterOutputStream的用法详解
2023-10-02 06:47:03
Mapreduce分布式并行编程
2023-04-30 02:43:16
C++中顺序表操作的示例代码
2022-05-12 17:39:52
java项目构建Gradle的使用教程
2023-06-07 19:17:41
Java中控制流程语句的深入讲解
2022-07-21 15:29:25
关于Springboot+gateway整合依赖并处理依赖冲突问题
2023-10-23 21:13:56
Android实现淘宝客户端倒计时界面
2023-09-18 21:25:09
C#中缓存System.Web.Caching用法总结
2021-09-05 04:41:11
java数据结构实现机器人行走
2023-12-04 00:44:24
用Spring Native将SpringBoot程序转换为GraalVM
2023-09-17 16:23:00
C#使用DropDownList绑定添加新数据的方法汇总
2023-05-08 03:20:56
C#结束进程及子进程
2021-06-22 15:18:10