如何将用户输入添加到数组中
How to add user input into an array
这是parentclass
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());
}
}
这是child class:
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 方法将在书中 class,要完成 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
谢谢。
edit: 用户添加图书或视频后,当用户要打印所有馆藏时,会显示书名、id、借阅费用和最长借阅期限。我该怎么做?
edit2:清理一些我的问题不需要的部分
我建议您使用 "List of Object" 概念来处理它。
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() ...
我不完全确定你想要什么,但在关注你的 粗体 文本后,这就是我对你的情况的理解。如果不符合您的要求,请随时纠正我。
希望对您有所帮助:)
这是parentclass
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());
}
}
这是child class:
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 方法将在书中 class,要完成 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
谢谢。
edit: 用户添加图书或视频后,当用户要打印所有馆藏时,会显示书名、id、借阅费用和最长借阅期限。我该怎么做?
edit2:清理一些我的问题不需要的部分
我建议您使用 "List of Object" 概念来处理它。
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() ...
我不完全确定你想要什么,但在关注你的 粗体 文本后,这就是我对你的情况的理解。如果不符合您的要求,请随时纠正我。 希望对您有所帮助:)