gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:57 27 4
gpt4 key购买 nike

这是父类

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) 例如,您可以定义 Book 对象的 List。

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() ...

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

关于java - 如何将用户输入添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428110/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com