作者:AlexTan
E-mail: [email protected]
上一篇博客我們介紹了用數組的方式來實現圖書管理系統,但是用數組實現的方式有兩個主要的缺點就是: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