作者:AlexTan
E-mail: alextanbz@gmai.com
上一篇博客我们介绍了用数组的方式来实现图书管理系统,但是用数组实现的方式有两个主要的缺点就是:1. 查找和删除比较麻烦;2. SIZE得固定,SIZE小了的话装不下那么多书,SIZE大了的话就比较浪费空间。所以接下来我们使用JAVA里的ArrayList的方式来解决这些问题。
ArrayList简介(百度的)
ArrayList 是一个数组队列,相当于动态数组。与Java中的数组相比,它的容量能动态增长。它继承于AbstractList,实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。
ArrayList 继承了AbstractList,实现了List。它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。
ArrayList 实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。
具体的使用详解可以自己百度。
Java入门之图书管理系统二(ArrayList实现):
Book类和上一篇文档的一样,传送门:http://blog.csdn.net/alextan_/article/details/65447446
主要变化的是MainClass文件,这里把上次的不需要的代码都打注释了,可以对比一下:
package ui;
import java.util.ArrayList;
import java.util.Scanner;
import model.Book;
public class MainClass {
/*
public static final int SIZE = 10;
Book[] booklist = new Book[SIZE];
*/
ArrayList booklist = new ArrayList();
int count = 0;
public MainClass()
{
Scanner scan = new Scanner(System.in);
printMenu();
while(true)
{
//读取用户输入
int choice = scan.nextInt();
if(choice == 5)
{
System.out.println("成功退出系统,欢迎再次光临!");
break;
}
switch(choice)
{
case 1: addBook(); break;
case 2: deleteBoo(); break;
case 3: changeBoo(); break;
case 4: findBoo(); break;
default: System.out.println("输入非法"); printMenu(); continue;
}
}
/*
while(true)
{
//根据用户输入调用不同方法
if(choice == 1)
{
addBook();
}
else if(choice == 2)
{
deleteBoo();
}
else if(choice == 3)
{
changeBoo();
}
else if(choice == 4)
{
findBoo();
}
else if(choice == 5)
{
System.out.println("成功退出系统,欢迎再次光临!");
break;
}
}
*/
}
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("当前共有:"+booklist.size()+"本书!");
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的增加的方法,是不是简便许多?
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++)
booklist[i]=booklist[i+1];
*/
booklist.remove(id);//ArrayList删除第几个(从0开始计数)元素的方法,简单多了,有木有?
count--;
System.out.println("删除成功!");
printAllBook();
}
else
{
System.out.println("输入错误!");
}
}
else if(choose == 2)
{
System.out.println("请输入您要删除的书名:");
String name = scan.next();
int id = nameFind(name);
if(id > -1)
{
/*
for(int j = id; j<count-1; j++)
{
booklist[j]=booklist[j+1];
}
*/
booklist.remove(id);
count --;
System.out.println("删除成功!");
printAllBook();
}
else
{
System.out.println("未查找到您想要的书名");
}
}
else if(choose == 3)
{
printMenu();
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);
if(id > -1)
{
Book book = (Book)booklist.get(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);
System.out.println("修改成功!");
printAllBook();
}
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("原书名为:"+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);
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);//ArrayList获取指定元素的方法
//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)
{
//System.out.println(number);
if(number <= count)
{
int id = number - 1;
return id;
}
else
return -1;
}
int nameFind(String name)
{
int id = -1;
for(int i = 0; i < count; i++)
{
Book book = (Book)booklist.get(i);
//if(booklist[i].getBookname().equals(name))
if(book.getBookname().equals(name))
{
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();
}
}
运行效果和上次的效果一样,所以就不贴截图了。
总结:
用ArrayList是比数组方便了许多,不过现在遇到的问题就是,每次添加的数据都没有存在磁盘上,所以当终止程序后,重新运行,以前添加的数据就不见了,这显然是不符合需求的。所以,下一篇教程将会加入IO处理,把数据都写入到本地的txt文件保存,这样就不用担心终止运行后数据消失了。
请阅读下一篇:Java入门教程之图书管理系统(由简入繁)(三)
转载请注明出处:http://blog.csdn.net/alextan_/article/details/65449333