写在前面,我也是一个编程小白,在校软件工程专业学生。写博客的目的主要是让自己有一个复习的过程,因为在编码的过程中或多或少都会有百度相关的知识盲区的时候,因此写博客来巩固知识,同时也方便以后出现同样的问题好解决。另一个目的呢就是给遇到困难的同学提供一些思路或者emmmm解决ddl的快捷经。以下附有详细代码并且简陋的注释。同时也欢迎大家多多提出改进意见。
0.程序基本思路
由于没有好好学UML,这里就不发了。
①主要就是一个BOOK简单类,表示书的基本信息。
②程序中的所有操作都是对对象进行操作,也符合面向对象嘛。但是对于大多数从C语言过来的伙伴都会犯一个错误就是觉得对象实例化没有用,因为我完全可以用ArrayList来存储图书信息,每次操作都对数组进行操作,我的BOOK对象就没有任何意义。我的代码可以比较好的帮助大家转变这种思想,体会到BOOK类的作用。BOOK对象会使得代码更简洁并且思路更清晰。
③ArrayList数组存储的是BOOK对象,每次在使用的时候需要调用BOOK类中的setter和getter函数,因此在写的时候必须写上这两个函数。我想大家既然都开始写这么样的小程序了,应该有了解JAVA简单类。
④在把数据存储回文件的时候,把BOOK的属性转化成字符串存储,这样文件的存储格式就不会改变。
1.BOOK类的定义
package BMS;
public class BOOK {
private String Bname;
private String Bauthor;
private String publicationDate;
private String publishingHouse;
private String BISBN;
private static int Bid = 0;
public BOOK(){}//无参数传递构造函数
public BOOK(String bname, String bauthor, String publicationDate, String publishingHouse, String BISBN){
this.Bname = bname;
this.Bauthor = bauthor;
this.publicationDate = publicationDate;
this.publishingHouse = publishingHouse;
this.BISBN = BISBN;
Bid++;
}
public BOOK(String [] args){
this.Bname = args[0];
this.Bauthor = args[1];
this.publicationDate = args[2];
this.publishingHouse = args[3];
this.BISBN = args[4];
Bid++;
}
//以下是setter函数对每个属性进行赋值
public void setBname(String Bname){
this.Bname = Bname;
}
public void setBauthor(String Bauthor){
this.Bauthor = Bauthor;
}
public void setPublicationDate(String publicationDate){
this.publicationDate = publicationDate;
}
public void setPublishingHouse(String publishingHouse){
this.publishingHouse = publishingHouse;
}
public void setBISBM(String BISBM){
this.BISBN = BISBM;
}
//以下是getter函数,获取每个属性值
public String getBname(){
return Bname;
}
public String getBauthor(){
return Bauthor;
}
public String getPublicationDate(){
return publicationDate;
}
public String getPublishingHouse(){
return publishingHouse;
}
public String getBISBM(){
return BISBN;
}
//获取所有成员属性值
public String getInfo(){
return Bid + "\t\t" + Bname+ "\t\t" + Bauthor+ "\t\t" + publicationDate+ "\t\t" + publishingHouse+ "\t\t" + BISBN;
}
}
以上BOOK类中繁琐的setter、getter函数。
其中
①
BOOK(String bname, String bauthor, String publicationDate, String publishingHouse, String BISBN)
构造函数,主要是在添加图书的方法中需要传入字符串实例化对象。
②
BOOK(String [] args)
数组参数的构造函数,在读取文件的时候,我是先把文件按行读出,
while (txt.hasNextLine()) {
b = txt.nextLine().split("\\s");
BOOK book = new BOOK(b);
strList.add(book);
count ++;
}
每一行生成一个数组,然后通过传递数组来实例化BOOK对象。
2.Operation类定义
这一部分代码比较长,可以快速阅读比较感兴趣的模块。
大概300行,因为考虑到程序的健壮性,因此用了较多的try以及if判断和while循环。
package BMS;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Operation {//打开文件夹导入数组
Scanner in = new Scanner(System.in);
int count = 0;
public ArrayList<BOOK> getBook() {//读取文件,把文件的值放入ArrayList数组中
ArrayList<BOOK> strList = new ArrayList<>();
String[] b = new String[5];
System.out.println("~~~~~~~~~~~~~~~开始读取文件~~~~~~~~~~~~~~~");
try {
Scanner txt = new Scanner(new FileReader("E:\\Java_IDEA_work\\Java江华实验\\src\\BMS\\book.txt"));
while (txt.hasNextLine()) {
b = txt.nextLine().split("\\s");
BOOK book = new BOOK(b);
strList.add(book);
count ++;
}
txt.close();
} catch (FileNotFoundException e) {
System.out.println("File not found!");
}
System.out.println("文件读取成功!");
return strList;
}
public int orderFind(int number){
if (number <= count){
number = number - 1;
return number;
}
else {
return -1;
}
}
public int nameFind(String NAME,ArrayList list){
int id = -1;
for (int i =0; i < count; i++){
BOOK book = (BOOK)list.get(i);
if (book.getBname().equals(NAME)){
id = i;
break;
}
else if (i < count){
continue;
}
else {
System.out.println("未查找到图书!");
break;
}
}
return id;
}
public void modifyBook(ArrayList<BOOK> list) {//修改图书
System.out.println("~~~~~~~~~~~~~~~开始修改图书~~~~~~~~~~~~~~~");
while (true) {
System.out.println("1.按照图书序列号修改图书\t\t\t2.按照图书书名修改图书\n3.返回菜单");
int choose = in.nextInt();
if (choose == 1) {
System.out.println("请输入图书序列号:");
int id = orderFind(in.nextInt());
if (id > -1) {
BOOK book = (BOOK)list.get(id);//生成实例化对象,对对象进行操作
System.out.println("你要修改的图书的是:" + book.getBname());
while (true) {
System.out.println("你要修改内容:\n1.图书名称\t\t\t2.图书作者\n3.出版社\t\t\t4.出版日期\n5.ISBN\t\t\t6.返回上一级菜单");
System.out.print("请输入选项:");
int a = in.nextInt();
try {
switch (a){
case 1:
System.out.print("将图书书名修改为:");
String NAME = in.next();
book.setBname(NAME);
break;
case 2:
System.out.println("图书作者修改为:");
String AUTHOR = in.next();
book.setBauthor(AUTHOR);
break;
case 3:
System.out.println("图书出版社修改为:");
String House = in.next();
book.setPublishingHouse(House);
break;
case 4:
System.out.println("图书出版日期修改为:");
String DATE = in.next();
book.setPublicationDate(DATE);
break;
case 5:
System.out.println("图书ISBN修改为:");
String I = in.next();
book.setBISBM(I);
break;
}
if (a == 6){
break;
}
System.out.println("修改成功!");
} catch (Exception e) {
System.out.println("输入错误!");
}
}
}
else{
System.out.println("输入有误!");
}
}
else if (choose == 2){
System.out.println("请输入图书书名:");
int id = nameFind(in.next(),list);
if (id > -1) {
BOOK book = (BOOK)list.get(id);//生成实例化对象,对对象进行操作
System.out.println("你要修改的图书的是:" + book.getBname());
while (true) {
System.out.println("你要修改内容:\n1.图书名称\t\t\t2.图书作者\n3.出版社\t\t\t4.出版日期\n5.ISBN\t\t\t6.返回上一级菜单");
System.out.print("请输入选项:");
int a = in.nextInt();
try {
switch (a){
case 1:
System.out.print("将图书书名修改为:");
String NAME = in.next();
book.setBname(NAME);
break;
case 2:
System.out.println("图书作者修改为:");
String AUTHOR = in.next();
book.setBauthor(AUTHOR);
break;
case 3:
System.out.println("图书出版社修改为:");
String House = in.next();
book.setPublishingHouse(House);
break;
case 4:
System.out.println("图书出版日期修改为:");
String DATE = in.next();
book.setPublicationDate(DATE);
break;
case 5:
System.out.println("图书ISBN修改为:");
String I = in.next();
book.setBISBM(I);
break;
}
if (a == 6){
break;
}
System.out.println("修改成功!");
} catch (Exception e) {
System.out.println("输入错误!");
}
}
}
else{
System.out.println("输入有误!");
}
}
else if (choose == 3){
break;
}
}
}
public void addBook(ArrayList list) {//添加图书
System.out.println("~~~~~~~~~~~~~~~开始添加图书~~~~~~~~~~~~~~~");
System.out.println("输入图书名称:");
String NAME = in.next();
System.out.println("输入图书作者:");
String AUTHOR = in.next();
System.out.println("输入出版社:");
String House = in.next();
System.out.println("输入出版日期:");
String DATE = in.next();
System.out.println("输入ISBM:");
String I = in.next();
BOOK book = new BOOK(NAME, AUTHOR, DATE, House, I);
list.add(book);
count++;
System.out.println("添加成功!");
}
public void Traverse_the_book(ArrayList list) {//遍历书目
System.out.println("~~~~~~~~~~~~~~~开始遍历图书~~~~~~~~~~~~~~~");
for (int i = 0; i < count; i++){
BOOK book = (BOOK)list.get(i);
System.out.println("第"+(i+1)+"本书:\n"+"书名:"+book.getBname()+"\n作者:"+book.getBauthor()+"\n出版社:"+book.getPublishingHouse()+"\n出版日期:"+book.getPublicationDate()+"\nISBN号:"+book.getBISBM());
}
}
public void Query_bibliography(ArrayList list) {//查询图书
System.out.println("~~~~~~~~~~~~~~~开始查找图书~~~~~~~~~~~~~~~");
System.out.println("1.按照序号查找图书\t\t\t2.按照书名查找图书");
System.out.print("请输入选择:");
int choose = in.nextInt();
if (choose == 1){
while (true) {
System.out.print("请输入图书序号:");
int numble = in.nextInt();
int id = orderFind(numble);
if (id > -1) {
BOOK book = (BOOK) list.get(id);
System.out.println("书名:" + book.getBname() + "\n作者:" + book.getBauthor() + "\n出版社:" + book.getPublishingHouse() + "\n出版日期:" + book.getPublicationDate() + "\nISBN号:" + book.getBISBM());
break;
} else {
System.out.println("序列号溢出!");
}
}
}
else{
while (true){
System.out.print("请输入书名:");
String NAME = in.next();
int id = nameFind(NAME, list);
if (id > -1) {
BOOK book = (BOOK) list.get(id);
System.out.println("书名:" + book.getBname() + "\n作者:" + book.getBauthor() + "\n出版社:" + book.getPublishingHouse() + "\n出版日期:" + book.getPublicationDate() + "\nISBN号:" + book.getBISBM());
break;
} else {
System.out.println("未找到图书!");
}
}
}
}
public void deleteBook(ArrayList list) {//删除书目
System.out.println("~~~~~~~~~~~~~~~开始删除图书~~~~~~~~~~~~~~~");
while (true) {
System.out.println("1.按照序号删除图书\t\t\t2.按照书名删除图书\t\t3.返回菜单");
int choose = in.nextInt();
if (choose == 1){
System.out.print("请输入图书编号:");
int number = in.nextInt();
int id = orderFind(number);
if (id > -1){
list.remove(id);
count--;
System.out.println("删除成功!");
}
else {
System.out.println("序号溢出!");
}
}
else if (choose == 2){
System.out.print("请输入图书书名:");
String NAME = in.next();
int id = nameFind(NAME,list);
if (id > -1){
list.remove(id);
count--;
System.out.println("删除成功!");
}
else {
System.out.println("未找到图书!");
}
}
else if (choose == 3){
break;
}
}
}
public void Write_File(ArrayList<BOOK> list) {//写入文件
System.out.println("~~~~~~~~~~~~~~~开始保存数据~~~~~~~~~~~~~~~");
try {
PrintWriter out = new PrintWriter(new FileWriter("C:\\Users\\junqi\\Desktop\\book.txt"));
for (BOOK value : list) {
String txt = value.getBname() + " " + value.getBauthor() + " " + value.getPublicationDate() + " " + value.getPublishingHouse() + " " + value.getBISBM();
out.println(txt);
}
System.out.println("保存数据成功!");
out.flush();
out.close();
} catch (IOException e) {
System.out.println("写入失败!");
}
}
public int menu() {//菜单
System.out.println("1.添加书目\t\t2.删除书目\n3.查询书目\t\t4.所有书目\n5.保存数据\t\t6.退出程序");
System.out.print("请输入你要执行的命令:");
return in.nextInt();
}
}
3.main函数
package BMS;
import java.util.ArrayList;
public class main {
public static void main(String[] args){
System.out.println("图书管理系统1.0");
System.out.println("欢迎使用小型的图书管理程序:BMS");
Operation operation = new Operation();//实例化操作类
ArrayList bookList = new ArrayList();//实例化ArrayList数组
bookList = operation.getBook();
while (true) {
int choose;
choose = operation.menu();
switch (choose){
case 1:
operation.addBook(bookList);
break;
case 2:
operation.deleteBook(bookList);
break;
case 3:
operation.Query_bibliography(bookList);
break;
case 4:
operation.Traverse_the_book(bookList);
break;
case 5:
operation.Write_File(bookList);
break;
}
if (choose == 6){
operation.Write_File(bookList);
break;
}
}
}
}