前言
本系统仅仅只是为了简单的复习一下java一些的基本知识,我是使用eclipse开发的,在控制台上面运行,系统的功能比较简单,只是对增删改查的简单使用,还有很大的改进空间。
由于仅仅只是为了复习java知识,而且储存的数据比较单一,仅仅使用了文件储存图书信息,这里并不需要使用数据库。
系统流程
运行截图
系统第一次启动时
添加一条记录
程序代码
Book.java
package wqzcode.booksystem;
import java.io.Serializable;
/**
* 储存图书的信息的类
* @author wqz
*
*/
public class Book implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id; // 图书编号
private String bookName; // 图书名字
private String author; // 作者
private String press; // 出版社
private String bookType; // 图书类型
private double price; // 价格
private int num; // 数量
public Book() {
super();
}
public Book(String id, String bookName, String author, String press, String bookType, double price, int num) {
super();
setId(id);
setBookName(bookName);
setAuthor(author);
setPress(press);
setBookType(bookType);
setPrice(price);
setNum(num);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public String getBookType() {
return bookType;
}
public void setBookType(String bookType) {
this.bookType = bookType;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return id + "\t\t" + bookName + "\t\t" + author + "\t\t" + press + "\t\t"
+ bookType + "\t\t" + price + "\t\t" + num;
}
}
BookMethod.java
package wqzcode.booksystem;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Scanner;
/**
* 对书操作的方法定义
* @author wqz
*
*/
public class BookMethod {
/**
* 自定义添加图书的方法
* @param books
*/
public void addBook(List<Book> books) {
Book book = new Book();
Scanner sc = new Scanner(System.in);
System.out.println("-----------------------------------------");
System.out.println(" 添加图书");
// 输入图书信息
System.out.println("请输入图书编号:");
String id = sc.next();
System.out.println("请输入图书名称:");
String bookName = sc.next();
System.out.println("请输入图书作者:");
String author = sc.next();
System.out.println("请输入图书出版社:");
String press = sc.next();
System.out.println("请输入图书类型:");
String bookType = sc.next();
System.out.println("请输入图书价格:");
double price = sc.nextDouble();
System.out.println("请输入图书数量:");
int num = sc.nextInt();
// 将图书信息储存到book对象中
book.setId(id);
book.setBookName(bookName);
book.setAuthor(author);
book.setPress(press);
book.setBookType(bookType);
book.setPrice(price);
book.setNum(num);
//将对象添加到数组中
books.add(book);
System.out.println("-----------------------------------------");
System.out.println("输入任意值返回主菜单...");
String str = sc.next();
// 将更改后的数据重新写入文件books.txt
writeBooks(books);
}
/**
* 自定义删除图书的方法
* @param books
* @param book
*/
public void deleteBook(List<Book> books, String id) {
for(int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if(book.getId().equals(id)) {
books.remove(i);
}
}
Scanner sc = new Scanner(System.in);
System.out.println("输入任意值返回主菜单...");
String str = sc.next();
// 将更改后的数据重新写入文件books.txt
writeBooks(books);
}
/**
* 自定义查找图书的方法
* @param books
* @param message
*/
public void findBook(List<Book> books, String message) {
System.out.println("--------------------------------------------------");
System.out.println("序号\t\t编号\t\t名称\t\t作者\t\t出版社\t\t\t类型\t\t价格\t\t数量");
int j = 1; // 查找的图书编号
// 以编号查找
System.out.print("--------------------------------------------------");
System.out.println("根据编号查找:");
//System.out.println(books);
for(int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if(book.getId().indexOf(message) != -1) {
System.out.print(j + "\t\t");
System.out.println(book);
j++;
}
}
// 以名称查找
System.out.print("--------------------------------------------------");
System.out.println("根据名称查找:");
for(int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if(book.getBookName().indexOf(message) != -1) {
System.out.print(j + "\t\t");
System.out.println(book);
j++;
}
}
// 以作者查找
System.out.print("--------------------------------------------------");
System.out.println("根据作者查找:");
for(int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if(book.getAuthor().indexOf(message) != -1) {
System.out.print(j + "\t\t");
System.out.println(book);
j++;
}
}
// 以出版社查找
System.out.print("--------------------------------------------------");
System.out.println("根据出版社查找:");
for(int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if(book.getPress().indexOf(message) != -1) {
System.out.print(j + "\t\t");
System.out.println(book);
j++;
}
}
// 以类型查找
System.out.print("--------------------------------------------------");
System.out.println("根据类型查找:");
for(int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if(book.getBookType().indexOf(message) != -1) {
System.out.print(j + "\t\t");
System.out.println(book);
j++;
}
}
System.out.println("--------------------------------------------------");
Scanner sc = new Scanner(System.in);
System.out.println("输入任意值返回主菜单...");
String str = sc.next();
}
/**
* 自定义修改图书的方法
* @param books
* @param ID
*/
public void modifyBook(List<Book> books, String ID) {
Scanner sc = new Scanner(System.in);
for(int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if(book.getId().equals(ID)) {
System.out.println("-----------------------------------------");
System.out.println(" 修改图书");
// 重新输入修改后的图书信息
System.out.println("请重新输入图书编号( "+book.getId()+" ):");
String id = sc.next();
System.out.println("请重新输入图书名称( "+book.getBookName()+" ):");
String bookName = sc.next();
System.out.println("请重新输入图书作者( "+book.getAuthor()+" ):");
String author = sc.next();
System.out.println("请重新输入图书出版社( "+book.getPress()+" ):");
String press = sc.next();
System.out.println("请重新输入图书类型( "+book.getBookType()+" ):");
String bookType = sc.next();
System.out.println("请重新输入图书价格( "+book.getPrice()+" ):");
double price = sc.nextDouble();
System.out.println("请重新输入图书数量( "+book.getNum()+" ):");
int num = sc.nextInt();
// 将对象中的信息重新覆盖
book.setId(id);
book.setBookName(bookName);
book.setAuthor(author);
book.setPress(press);
book.setBookType(bookType);
book.setPrice(price);
book.setNum(num);
System.out.println("-----------------------------------------");
}
}
System.out.println("输入任意值返回主菜单...");
String str = sc.next();
// 将更改后的数据重新写入文件books.txt
writeBooks(books);
}
/**
* 自定义将图书的集合写入books.txt文件
* @param books
*/
public void writeBooks(List<Book> books) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("books.txt"));
oos.writeObject(books);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
BookLogic.java
package wqzcode.booksystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* 图书管理功能的逻辑管理
* @author wqz
*
*/
public class BookLogic {
private List<Book> books = new LinkedList<>();
public void startBookSystem() {
BookMethod bm = new BookMethod();
while(true) {
books = initBooks(books);
//System.out.println(books);
System.out.println("-------------------------------------------------------------------------------");
if(books.size() == 0) {
System.out.println("您还没存放图书...");
} else {
System.out.println(" 图书列表");
System.out.println("-------------------------------------------------------------------------------");
System.out.println("序号\t\t编号\t\t名称\t\t作者\t\t出版社\t\t\t类型\t\t价格\t\t数量");
for(int i = 0; i < books.size(); i++) {
System.out.print((i+1) + "\t\t");
System.out.println(books.get(i));
}
}
System.out.println("-------------------------------------------------------------------------------");
System.out.print("1.添加图书\t");
System.out.print("2.删除图书\t");
System.out.print("3.修改图书\t");
System.out.print("4.查找图书\t");
System.out.println("0.退出系统");
System.out.println("\n请输入选择的功能:");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch(choose) {
case "1":
bm.addBook(books);
break;
case "2":
System.out.println("请输入需要删除的图书序号:");
int index1 = sc.nextInt();
bm.deleteBook(books, books.get(index1-1).getId());
break;
case "3":
System.out.println("请输入需要修改的图书序号:");
int index2 = sc.nextInt();
bm.modifyBook(books, books.get(index2-1).getId());
break;
case "4":
System.out.println("请输入该图书的关键字(编号,名称,作者,出版社,类型)");
String message = sc.next();
bm.findBook(books, message);
break;
case "0": return;
default :System.out.println("输入错误,请重新输入...");
}
}
}
public List<Book> initBooks(List<Book> books) {
try {
// 判断books.txt文件存在
File f1 = new File("books.txt");
if(!f1.exists()) {
System.out.println("文件不存在,自动初始化...");
f1.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("books.txt"));
oos.writeObject(books);
oos.close();
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("books.txt"));
books = (List<Book>) ois.readObject();
//System.out.println("--------" + books);
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return books;
}
}
BookMain.java
package wqzcode.booksystem;
public class BookMain {
public static void main(String[] args) {
BookLogic bl = new BookLogic();
bl.startBookSystem();
System.out.println("谢谢使用本系统,欢迎下次使用!!!");
}
}