图书馆系统:借阅

时间:2016-05-28 09:59:45

标签: java arrays

我不知道如何在我必须创建的库菜单中执行borrowHolding()。 因此,borrowHolding()的目的是让会员能够借阅书籍或视频。

这只是数组的示例数据:

member[0] = new StandardMember("ID", "Name");
member[1] = new PremiumMember("ID", "Name");
holding[0] = new Book("ID", "Title");
holding[1] = new Video("ID", "Title", loanFee);

这是TestLibrary类中的borrowHolding()方法:(该数组也在TestLibrary类中)

public static void borrowHolding(){
    String option;
    option = input.next();

do{

    Scanner scan = new Scanner(System.in);
    int tempId = 0;
    System.out.println("Enter your ID: ");
    String searchID = scan.next();

    for(int i = 0; i < member.length; i++){

        if(member[i].getID().equals(searchID)){

                tempId = i;

            }
        }

因此,对于该方法,我尝试编写一个代码来搜索数组以找到想要借用的memberID。它还没有完成,因为我相信我没有正确地做到这一点

有一个包含

的成员类
public class Member{

    public Holding[] getCurrentHoldings(){
    }

}

从方法的名称,它用于存储借来的成员的馆藏。因此,如果成员1借书,那本书将被存储在数组中,我想。我在考虑为这个方法使用ArrayList,但不确定它是否有意义。

借用书籍或视频,有一些条件可以借用,但我不知道如何将其实现到borrowHolding()中。其中一个条件是在Hold类中。

public class Holding{
    public boolean borrowHolding(){
        if(status == true && isOnLoan() == false)
            borrowDate = newDateTime(); //this is to get the time when the book or video is borrowed

            return true;
        }else
            return false;

    }
}

会员班的另一个条件是会员必须有足够的信贷借款。图书借阅费用为10美元,视频价格为4美元或6美元。

我想我写了一些不需要的信息,但我想它比信息更少更好。

我的问题是我该如何处理LibraryMenu中的borrowHolding()方法?如果一个成员想要借用一个持股,我该怎么做呢?持股将在成员类的成员数组下​​

public class Member{

    public Holding[] getCurrentHoldings(){
    }

}

使用持有类中的条件(如果满足),并且在执行borrowHolding方法时,成员类的方法将能够通过书籍或视频中的贷款费用减去成员信用。有可能吗?

public class Member{
    private int credit = 30;
    public int calculateRemainingCredit(){

        credit = credit - //(the loan fee from the book or video class)

    }
}

1 个答案:

答案 0 :(得分:0)

如果你的意图是为会员类增加一个保留,那么这是可能的。我建议添加一个Hold的ArrayList而不是一个常规数组,因为它似乎会不断变化。

public class Member{
    private ArrayList<Holding> currentholdings; // you may need to import the arraylist
    private int credit;

    public void init(){ // goes in constructor 
        currentholdings = new ArrayList<Holding>();
        credit=0;
    }

    public void addHolding(Holding newholding){ // adds a new holding to the members array
        currentholdings.add(newholding);
        credit-=newholding.getFee(); // will need to add a fee variable to the holding class;
    }
}

至于检查成员是否有足够的“信用”,可以在识别数组索引后立即在borrowHolding()方法中完成。我建议将该成员的参数添加到borrowHolding()方法中,以便您可以轻松地从该成员访问变量。

if(member[i].getID().equals(searchID)){
    tempId = i;
    int tempHolding; // index of whatever holding you wanted (could get this from the scanner)

    if (holding[tempHolding].borrowHolding(member[tempId])){ // check status
        member[tempId].addHolding(holding[tempHolding]); // they have passed the req. so you can add the holding
    }

    break;
}

希望这能回答你的问题。