Java---图书管理系统(二)
参考:http://blog.****.net/alextan_/article/details/65447446
本系列是通过学习AlexTan这位博主的文章搭建一个图书管理系统,顺便再学习一下java这门语言。大二水了一学期java课,完全是菜鸡,所以注释很多自己的理解。
上一篇博客介绍了用数组的方式实现图书管理系统,但是用数组的方式有两个主要的缺点就是:
1、查找和删除比较麻烦
2、SIZE得固定,SIZE小了的话装不下来那么多书,SIZE大了就浪费空间
解决办法:
使用ArrayList类
一、ArrayList
ArrayList是一个数组队列,相当于动态数组,与Java中的数组相比,它的容量能动态增长。它继承于AbstractList,实现了List,RandomAccess,Cloneable,java.io.Serializable这些接口。
1)ArrayList 继承了AbstractList,实现了List。它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
2)ArrayList 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在ArrayList中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。稍后,我们会比较List的“快速随机访问”和“通过Iterator迭代器访问”的效率。
3)ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。
4)ArrayList 实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。
详细参考:https://www.cnblogs.com/skywang12345/p/3308556.html
二、修改MainClass.java文件,代码如下:
package ui;
import java.util.ArrayList;
import java.util.Scanner;
import model.Book;
public class MainClass {
/*
public static final int SIZE = 10; //最大可存储图书的数量
//public指定该变量为公共,static指定变量被所有对象共享,即所有实例都可以使用该变量
//final为最终修饰符,指定此变量的值不能变
Book[] booklist = new Book[SIZE]; //初始化Book类型的实例
int count = 0; //当前图书数量
*/
ArrayList booklist = new ArrayList();
int count=0;
public MainClass()
{
Scanner scan = new Scanner(System.in);
//初始化一个Scanner类实例,来获取用户的输入
printMenu(); //调用打印菜单的方法
while(true)
{
//读取用户输入
int choice = scan.nextInt(); //获取一个int型输入
if(choice == 5)
{
System.out.println("成功退出系统,欢迎再次光临!");
break;
}
switch(choice)//switch形式
{
case 1: addBook(); break;
case 2: deleteBoo(); break;
case 3: changeBoo(); break;
case 4: findBoo(); break;
default: System.out.println("输入非法"); printMenu(); continue;//这个continue 是continue的while,
}
}
}
void printMenu()
{
//打印菜单
System.out.println("欢迎...");
System.out.println("增加图书...1");
System.out.println("删除图书...2");
System.out.println("修改图书...3");
System.out.println("查询图书...4");
System.out.println("退出系统...5");
}
void addBook()//增加图书
{
if (count > booklist.size()-1) //注意这里的改动
{
System.out.println("当前共有:"+count+"本书!");
Scanner scan = new Scanner(System.in);
System.out.println("请输入图书名:");
String bookname = scan.next();
System.out.println("请输入作者:");
String author = scan.next();
System.out.println("请输入单价:");
float price = scan.nextFloat();
Book book = new Book(bookname,author,price); //创建一本书
//booklist[count] = book;
booklist.add(book);//调用ArrayList的add方法
count++;
System.out.println("增加成功!");
printAllBook();
}
else
{
System.out.println("图书库已满!");
}
}
void deleteBoo()//删除图书
{
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println("请输入按哪种方法删除图书:1、序号/2、书名/3、返回主菜单");
int choose = scan.nextInt();
if(choose == 1)
{
System.out.println("请输入要删除第几本书:");
int id = scan.nextInt();
id = orderFind(id);
//System.out.println(id);
if(id > -1)
{
/*
for(int i = id; i < count - 1 ; i++)//用for循环的形式实现对数组的删除
booklist[i]=booklist[i+1]; //通过数组的后面元素覆盖前面的元素来实现删除
*/
booklist.remove(id); //调用ArrayList的删除方法
count--;
System.out.println("删除成功!");
printAllBook();
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要删除的书名:");
String name = scan.next();
int id = nameFind(name); //通过方法将作者名转化为对应的id,但是这样有一个bug,要是作者名相同呢?
if(id > -1)
{
/*
for(int j = id; j<count-1; j++)//用for循环的形式实现对数组的删除
{
booklist[j]=booklist[j+1];
}
*/
booklist.remove(id);
count --;
System.out.println("删除成功!");
printAllBook();
}
else
{
System.out.println("未查找到您想要的书名");
}
}
else if(choose == 3)
{
printMenu();
break; //这个break会跳出当前循环,从而实现跳出当前函数,返回上一级循环,即主菜单。
}
else
{
System.out.println("输入非法!");
}
}
}
void changeBoo()
{
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println("请输入按哪种方法修改图书:1、序号/2、书名/3、返回主菜单");
int choose = scan.nextInt();
if(choose == 1)
{
System.out.println("请输入要修改第几本书:");
int number = scan.nextInt();
int id = orderFind(number); //将输入的数字转换为id,因为数组下标从0开始,所以id和number差了1
if(id > -1)
{
Book book = (Book)booklist.get(id); //根据id获取需要修改的元素
//System.out.println("原书名为:"+booklist[id].getBookname()+" 请输入你要修改为什么书名:");
System.out.println("原书名为:"+book.getBookname()+" 请输入你要修改为什么书名:");
String str = scan.next();
System.out.println("请输入作者:");
String author = scan.next();
System.out.println("请输入单价:");
float price = scan.nextFloat();
//booklist[id].setBook(str,author,price);
book.setBook(str, author, price); //调用Book类的重置属性方法
System.out.println("修改成功!");
printAllBook();
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要修改的书名:");
String name = scan.next();
int id = nameFind(name); //将书名转化为对应的id
if(id > -1)
{
Book book = (Book)booklist.get(id);
//System.out.println("原书名为:"+booklist[id].getBookname()+" 请输入你要修改为什么书名:");
String str = scan.next();
System.out.println("请输入作者:");
String author = scan.next();
System.out.println("请输入单价:");
float price = scan.nextFloat();
//booklist[id].setBook(str,author,price);
book.setBook(str, author, price);
System.out.println("修改成功!");
printAllBook();
}
}
else if(choose == 3)
{
printMenu();
break;
}
else
{
System.out.println("输入非法!");
}
}
}
void findBoo()
{
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println("请输入按哪种方法查找图书:1、序号/2、书名/3、返回主菜单");
int choose = scan.nextInt();
if(choose == 1)
{
System.out.println("请输入要查找第几本书:");
int number = scan.nextInt();
int id = orderFind(number);
if(id > -1)
{
Book book = (Book)booklist.get(id);
//System.out.println("你要查找的书名为:"+booklist[id].getBookname()+" 作者:"+booklist[id].getAuthor()+" 单价:"+booklist[id].getPrice()+"元/本");
System.out.println("你要查找的书名为:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要查找的书名:");
String name = scan.next();
int id = nameFind(name);
if(id > -1)
{
Book book = (Book)booklist.get(id);
//System.out.println("查找成功,您查找到的书为第"+(id+1)+"本书!"+"书名为:"+booklist[id].getBookname()+" 作者:"+booklist[id].getAuthor()+" 单价:"+booklist[id].getPrice()+"元/本");
System.out.println("查找成功,您查找到的书为第"+(id+1)+"本书!"+"书名为:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
}
else if(choose == 3)
{
printMenu();
break;
}
else
{
System.out.println("输入非法!");
}
}
}
void printAllBook() //循环打印所有的图书
{
for (int i = 0; i < count; i++)
{
Book book = (Book)booklist.get(i);
//System.out.println("第"+(i+1)+"本书名:"+booklist[i].getBookname()+" 作者:"+booklist[i].getAuthor()+" 单价:"+booklist[i].getPrice()+"元/本");
System.out.println("第"+(i+1)+"本书名:"+book.getBookname()+" 作者:"+book.getAuthor()+" 单价:"+book.getPrice()+"元/本");
}
}
int orderFind(int number) //按序号查找,返回id
{
//System.out.println(number);
if(number <= count)
{
int id = number - 1;
return id;
}
else
return -1;
}
int nameFind(String name)//按书名查找,返回id
{
int id = -1;
for(int i = 0; i < count; i++)
{
Book book = (Book)booklist.get(i);
if(book.getBookname().equals(name)) //调用Book类中的获取书名方法
{
id = i;
break;
}
else if(i<count)
continue;
else
{
System.out.println("未查找到您想要的书名");
break;
}
}
return id;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainClass();
}
}
运行结果和修改前无异:
Book类不需要修改。