这是父类
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) 例如,您可以定义一个 List of 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() ...
我不完全确定你想要什么,但在关注你的粗体文本之后,这就是我可以理解你的情况。如果不符合您的要求,请随时纠正我。希望对:)有所帮助