如何将用户输入添加到数组中

时间:2016-05-25 04:55:38

标签: java arrays inheritance user-input

这是父类

public class Holding {

private String holdingId, title;
private int defaultLoanFee, maxLoanPeriod, calculateLateFee;

public Holding(String holdingId, String title){

    this.holdingId = holdingId;
    this.title = title;
}

public String getId(){

    return this.holdingId;  

}

public String getTitle(){

    return this.title;  

}


public int getDefaultLoanFee(){

    return this.defaultLoanFee;

}
public int getMaxLoanPeriod(){

    return this.maxLoanPeriod;

}

public void print(){

    System.out.println("Title: " + getTitle());
    System.out.println("ID: " + getId());
    System.out.println("Loan Fee: " + getDefaultLoanFee());
    System.out.println("Max Loan Period: " + getMaxLoanPeriod());
}



}

这是儿童班:

public class Book extends Holding{

private String holdingId, title;
private final int defaultLoanFee = 10;
private final int maxLoanPeriod = 28;

public Book(String holdingId, String title){
    super(holdingId, title);    
}

public String getHoldingId() {
    return holdingId;
}

public String getTitle(){
    return title;
}

public int getDefautLoanFee(){
    return defaultLoanFee;
}

public int getMaxLoanPeriod(){
    return maxLoanPeriod;
}

public void print(){
    System.out.println("Title: " + getTitle());
    System.out.println("ID: " + getHoldingId());
    System.out.println("Loan Fee: " + getDefaultLoanFee());
    System.out.println("Max Loan Period: " + getMaxLoanPeriod());
}

}

这是菜单:

public class LibraryMenu {

static Holding[]holding = new Holding[15];
static int hold = 0;
static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    int options = keyboard.nextInt();
    do{
    }
    System.out.println();
    System.out.println("Library Management System");


    System.out.println("1. Add Holding");
    System.out.println("2. Remove Holding");


    switch(options){

    case 1:
        addHolding();
        break;

    default:
        System.out.println("Please enter the following options");
    }
}while(options == 13);
    System.out.println("Thank you");

}

public static void addHolding(){

    System.out.println("1. Book");
    System.out.println("2. Video");
    int input1 = input.nextInt();

    switch(input1){

    case 1:
        addBook();
        break;

    case 2:
        addVideo();
        break;

    default:
        System.out.println("Please select the following options");
        break;
    }
}

public static void addBook(){


    }   
}

我想要的只是当用户想要添加一本书时,用户将为其输入标题和ID,它将保存到数组中,一旦完成,它将返回到菜单。我现在似乎无法用我的知识做到这一点。

我试图在addBook()方法中执行此操作

holding[hold] = new Book();
holding[hold].setBook();

setBook方法将在Book类中,为了完成setBook,我必须为所有get方法设置set方法。但是当我这样做时,构造函数是未定义的。

public void setBook(){

System.out.println("Title: " + setTitle());
System.out.println("ID: " + setHoldingId());
System.out.println("Loan fee: " + setDefaultLoanFee());
System.out.println("Max Loan Period: " + setMaxLoanPeriod());

}

如果我的代码有任何问题,请随时告诉我:D

感谢。

编辑:用户添加图书或视频后,当用户想要打印所有馆藏时,它将显示标题,ID,贷款费用和最长贷款期限。我该怎么做?

edit2:用我的问题清理一些不必要的部分

1 个答案:

答案 0 :(得分:0)

我建议您在处理它时使用“对象列表”概念。

1)例如,您可以定义Book对象列表。

List<Book> bookList = new ArrayList<Book>();

2)每当你点击AddBook()函数时,你就可以做这样的事情。

//Create a temporary object of Book to get the user input data.
Book tempBook = new Book(holdingIDParam, titleParam);

//Add them to the list
bookList.Add(tempBook);

3)然后,您可以将该列表用于您的喜好,是将它们存储到基于.txt的数据库中,还是在整个程序中使用它们。用法示例:

bookList.get(theDesiredIndex).getBook() ...

我不完全确定你想要什么,但在关注你的粗体文本之后,这就是我能理解你的情况。如果不符合要求,请随时纠正我的要求。 希望它有所帮助:)

相关问题