Java图书管理系统(非正式系统任务导向型,内含完整项目代码),编辑Library类并完成TestDriver,南澳大学计算机大作业。

Java图书管理系统

(非正式管理系统,任务导向型。)
#下面是该系统的说明及相关文档:
Introduction
This will be done in the form of a library system that allows users to borrow various items that the library holds. You will be required to create an inheritance structure to contain all the different types of items the library might hold. In this case there are five different types of items Books, Magazines, Blurays, DVDs and MusicCDs.

  • You are required to create a library class that manages this system allowing an item to added or removed in addition to allowing a person to borrow an item and return it.
  • This program is supplied with an incomplete testing program, you must complete the test driver to ensure the completeness of your library system.
  • You are also required to generate the rest of the classes to describe the items held in your library using a suitable inheritance model in your program.

Description of the System
There are a number of different types of items in the library. There are Books, Magazines, MusicCDs and Movies.
Movies in turn can be DVDs or Blurays. You will need to implement these items as an inheritance structure with
appropriate setter and getter methods in addition to appropriate access permissions. As a starting point:

  • All items contain an id (String), a name (String), loaned (boolean), borrower (Person), and a cost (double).
  • All Books will have an author (String)
  • All Magazines will have distributionCity (String) and a
    magazineFrequency (Frequency) based on an enumerated
    type which has day, week monthly, quarterly and yearly.
  • All Movies will have an genre (Genre) based on an enumerated type Genre which has scifi, action, drama and romance
  • All DVDs will have a region (int)
  • All Blurays will have region (char) All MusicCD’s will have composer (String) and an artist (String)

In addition to the Library class described, there are also a few other classes needed for this assignment:

  • A Library contains the list of items (ArrayList of Items) currently held by the library
  • A Person has a name (String) and an address (String). Library items can be borrowed by a person.
  • A TestDriver (supplied with incomplete tests) that will test your program.
  • DuplicateItemException (Supplied). This exception object is Thrown when a duplicate item is inserted into the Library.
  • ItemNotFoundException (Supplied). This exception object Thrown when an item is not found in the list
  • In summary you should have 12 classes including the TestDriver.

Error Handling
To complete the assignment effectively there are some rules regarding exceptions:

  • In the event that an input is incorrect an Exception should be thrown. Eg invalid region for a bluray or dvd.
  • If you try to find an item that doesn’t exist you should throw an ItemNotFoundException
  • If you try to insert an item that already exists you should get a DuplicateItemException
  • In the event of any other error situations you should throw an exception.

Testing
You are to write a series of tests to make sure your program is uncrashable. You should do this in a test document (a text document included in your project called test.txt). That document should list each method in library with the tests that you consider need to be performed. You should then code your test conditions in the testDriver class. When you run your program you should have the following form of output.
Testing addItem***
-Test add an item and see if it is in the list-
Expected Found
Got Found
Success
-Test adding a new item then retrieving an item that doesn’t exist-
Expected Not Found
Got Not Found
Success
**
You should have one test for every scenario you can think of. In the first example above you would call the addItem with a parameter of a new item then would try finding the item. If an item is returned print out found and declare the test a success.
I will give you a basic test driver that you can expand for the assignment.

Restrictions
There are a number of rules you must follow when addressing the requirements for this assignment.

  • You must use inheritance.
  • You must have all the named classes (Library, Person, TestDriver and the two Exception objects).
  • Your Library class must have the methods EXACTLY as specified. I‘ll say this one again Your Library class must have the methods EXACTLY as specified.
  • If you don’t it wont compile when I dump my testing class in the folder and you will fail the assignment.

附上原说明文档:该项目的说明文件及基础代码
文件中含以下内容,其中HTML为说明文档,其他为基础代码文件中含如下内容
大致意思就是完成Library类及各种Item项目的类并通过测试!
贴代码太麻烦,我就直接上完成好的项目文档吧!
需要的自行下载:该项目完成后的项目压缩包
里面的内容如下图:
内含如图内容
这里附上Library类的相关代码:(其他请下载项目压缩包)

import java.util.ArrayList;
 /**
  * Library类
 * @author
 * @version 1.0
 */
public class Library {  
    /**
     * itemList contains the List of all items in the library.
     */
    private ArrayList itemList = new ArrayList();
    /**
     * count of all the items in the library.
     */
    private int count=0;
    /**
     * Changes the status of the named item to borrowed 
     * and adds the Person to the borrowed 
     * @param id The ID of the item to be borrowed
     * @param newPerson The borrower of the item
     * @throws ItemNotFoundException thrown if the id is not found.
     */
    public void loanItem(String id, Person newPerson) throws ItemNotFoundException {
      //TODO:  Finish this method
        Item find_one=findItem(id);
        find_one.setLoaned(true);
        Person copy=new Person(newPerson);
        find_one.setBorrower(copy);
    }
    /**
     * Changes the status of the named item to not borrowed and removes the user from the item.
     * The borrower (person) is returned to the caller
     * @param id The id of the item
     * @return The person who borrowed the item
     * @throws ItemNotFoundException thrown if the id is not found.
     */
    public Person returnItem(String id) throws ItemNotFoundException {
      //TODO:  Finish this method
        Item find_one=findItem(id);
        Person borrower=find_one.getBorrower();
        find_one.setLoaned(false);
        find_one.setBorrower(null);
      //return null so the code still compiles.  You will need to change it
      return borrower;
    }
    /**
     * Find the item by the given ID and return that Item
     * @param id The item to be returned
     * @return The item searched for.
     * @throws ItemNotFoundException thrown if the id is not found.
     */
    public Item findItem(String id) throws ItemNotFoundException {
      //TODO:  Finish this method
        for(int i=0;i<itemList.size();i++){
            if(((Item)itemList.get(i)).getId().equals(id)){
                    return (Item)itemList.get(i);
            }
        }
        throw new ItemNotFoundException("Cann't find this item!");
      //return null so the code still compiles.  You will need to change it
    }
    /**
     * Gets the borrower of an item.  If the item is not found throw ItemNotFoundException.
     * 
     * @param id the id of the item
     * @return the borrower of the item.  returns null if the item exists but is not borrowed.
     * @throws ItemNotFoundException thrown if the id is not found
     */
    public Person getBorrower(String id) throws ItemNotFoundException {
      //TODO:  Finish this method
     //return null so the code still compiles.  You will need to change it
      return findItem(id).getBorrower();
    }
    /**
     * Look up the name of the library item based on the ID given.
     * @param id The id of item searched for. 
     * @return The name of the item blank if not found.
     */
    public String nameForID(String id) {
      //TODO:  Finish this method
        String find_name="";
        try{
           find_name=findItem(id).getName();
        }catch(ItemNotFoundException e){
            System.out.print("Item not find!");
        }
        return find_name;
      //return null so the code still compiles.  You will need to change it
    }
        /**
     * Returns the id of the library item based on the name given.
     * 
     * @param name the name of the item
     * @return the id of the item blank if not found.
     */
    public String IDForName(String name) {
      //TODO:  Finish this method
        String find_id="";
        try{
            find_id=findItem(name).getId();
        }catch(ItemNotFoundException e){
            System.out.print("Item not find!");
        }
        return find_id;
      //return null so the code still compiles.  You will need to change it
    }
    /**
     * Add a new item to the list of library items.
     * 
     * @param newItem The new item to be added to the list.
     * @throws DuplicateItemException thrown if the item is already in the list.
     */
    public void addItem(Item newItem) throws DuplicateItemException{
      //TODO:  Finish this method
        Item item=null;
        //遍历集合
        for (int i = 0; i < itemList.size(); i++)
        {
            item =(Item)itemList.get(i);
            if(newItem.id.equals(item.id))
            {
                throw new DuplicateItemException("You are adding Duplicate item");
            }
        }
        Item copy_one=new Item();
        copy_one=newItem;
        itemList.add(copy_one);
        count++;
    }
    /**
     * Return the number of Items that are either Blurays or DVDs
     * @return the number of blurays and DVDs
     */
    public int countMovies() {
      //TODO:  Finish this method
        int movieCount=0;
        for(int i=0;i<itemList.size();i++){
            if(((Item)itemList.get(i) )instanceof Movies){
                movieCount++;
            }
        }
      //return -99999 so the code still compiles.  You will need to change it
      return movieCount;
    }
    /**
     * Convert the entire library to a string.  This should call the appropriate toString methods of each item in the ArrayList.
     * Should be in the format
     <pre>
Magazine [magazineFrequency=day, distributionCity=San Andreas]Item [id=ID000, name=Vanity Not So Faire, loaned=false, borrower=null, cost=5.95]
Magazine [magazineFrequency=day, distributionCity=San Andreas]Item [id=ID001, name=Click, loaned=false, borrower=null, cost=5.95]
Magazine [magazineFrequency=day, distributionCity=San Andreas]Item [id=ID002, name=Renovate, loaned=false, borrower=null, cost=5.95]
太多,就删了,都是各项目的toString,格式跟上面一样,各Item数据都是在TestDriver中添加的!
     </pre>
     */
    public String toString() {
      //TODO:  Finish this method
        String a="";
        for(int i=0;i<itemList.size();i++){
                a=a+((Item)itemList.get(i)).toString()+"\n";
            }
             return a;
    }
    /**
     * Checks if a specific library item is borrowed.
     * @param id The id of the item that is to be checked.
     * @return the status of the item whether it is borrowed or not.
     * @throws ItemNotFoundException thrown if the id is not found.
     */
    public boolean isBorrowed(String id) throws ItemNotFoundException {
      return findItem(id).isLoaned();
    } 
    /**
     * Return the total number of items out on loan.
     * @return The number representing the number of items currently on loan
     */
    public int countBorrowed() {
      //TODO:  Finish this method
        int borrowedCount=0;
        for(int i=0;i<itemList.size();i++){
            if(((Item)itemList.get(i)).isLoaned()){
                borrowedCount++;
            }
        }
      //return -99999 so the code still compiles.  You will need to change it
      return borrowedCount;
    }
    /**
     * The percentage of the number of items that are out on loan.  Expressed as a percentage of borrowed to total number of items.
     * @return the percentage borrowed.
     */
    public double percentageBorrowed() {
      //TODO:  Finish this method
        double a;
        a=(( (double)countBorrowed()/(double) itemList.size())*100);
      return a;
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

輕塵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值