gpt4 book ai didi

java - 将数组与子类链接

转载 作者:行者123 更新时间:2023-12-01 13:41:46 24 4
gpt4 key购买 nike

我当前的程序是一个学生图书馆系统,我有我的数组列表、菜单和方法,它们都可以工作。我的问题是我需要从父类(super class) LoanBook 中读取数组,该父类(super class) LoanBook 接受子类(小说和非小说)的覆盖。

正如您从 AddBook 方法中看到的,它获取书籍的详细信息并将其存储到数组列表中。

我的问题:我需要添加选项小说或非小说,但我需要数组列表从父类(super class)和子类中获取属性。我可以得到一些帮助吗?

我很乐意回答您的任何问题或提供更多信息。

主类

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.Scanner;

public class Main{
static Scanner keyboard = new Scanner(System.in);
static boolean run = true;
static Formatter x;


public static void main(String[]args){

LoanBook myBook = new LoanBook();

while (run){ // this while statement allows the menu to come up again
int answer = 0;
boolean isNumber;
do{ // start of validation
System.out.println("1. Add book");
System.out.println("2. Display the books available for loan");
System.out.println("3. Display the books currently on loan");
System.out.println("4. Make a book loan");
System.out.println("5. Return book ");
System.out.println("6 Write book details to file");
if (keyboard.hasNextInt()){ // I need to consider putting in a =>1 <=6
answer = keyboard.nextInt();
isNumber = true;
} else {
System.out.print(" You must enter a number from the menu to continue. \n");
isNumber = false;
keyboard.next(); // clears keyboard

}
}
while (!(isNumber));
switch (answer){

case 1:
addBook();
break;
case 2:
viewAll();
break;
case 3:
booksOnLoan();
break;
case 4:
loanBook();
break;
case 5:
returnBook();
break;
case 6:
writeToFile();
break;
case 7:
break;
}
}
}
static List<String>pupilName = new ArrayList<String>();
static List<String>issueDate = new ArrayList<String>();
static List<String>bookTitle = new ArrayList<String>();
static List<String>bookAuthor = new ArrayList<String>();
static List bookOnloan = new ArrayList<Boolean>();
public static void viewAll(){
System.out.println("\n");
for (int x = 0; x < bookTitle.size();x++){
int counter = x+1;
System.out.println("BookID:" +counter + "\n " + bookTitle.get(x) + " - " + bookAuthor.get(x)+" " + bookOnloan.get(x));
}
}
public static void booksOnLoan(){
System.out.println("\n");
for (int x = 0; x < pupilName.size();x++){
if (bookOnloan.contains(true)){
int counter = x+1;
System.out.println("BookID:" +counter + "\n "+"Pupil name: " + pupilName.get(x)
+"\n Book Title: "+ bookTitle.get(x) + " by " + bookAuthor.get(x)+" " + bookOnloan.get(x)+ "\n Issued: "+ issueDate.get(x)) ;
}
}
}



public static void addBook(){

System.out.println("Please enter the book title: ");
String newTitle = keyboard.next();
bookTitle.add(newTitle);
System.out.println("Please enter the book author");
String newAuthor = keyboard.next();
bookAuthor.add(newAuthor);
bookOnloan.add(false);
System.out.println("\n Your book: "+ bookTitle.get(bookTitle.size()-1)+ " has been added to the library" + "\n");
}

public static void loanBook(){
viewAll();
System.out.println("Please choose the BookID you would like to issue: ");
int issue = keyboard.nextInt()-1;
if (issue > 10){
System.out.println("Invalid book selection");
}
else {
bookOnloan.set(issue,true);
System.out.println("Please enter pupil name: ");
String newPupil = keyboard.next();
pupilName.add(newPupil);
System.out.println("Please enter date of issue: ");
String newIssue = keyboard.next();
issueDate.add(newIssue);
}
}
public static void returnBook(){
// booksOnLoan();
System.out.println("Please choose the BookID you would like to return: ");
int issue = keyboard.nextInt()-1;
if (issue > 10){
System.out.println("Invalid book selection");
}
else {
bookOnloan.set(issue,false);

接下来是我的父类(super class)

public class LoanBook {


private int bookID;
private String title,author,name,date;
boolean onLoan;
private static int count = 0;
static List<String> bookTitle = new ArrayList<String>();
static List<String>bookAuthor = new ArrayList<String>();
static List<String> pupilName = new ArrayList<String>();
static List<String>issueDate = new ArrayList<String>();
static List bookOnloan = new ArrayList<Boolean>();

public LoanBook(String title,String author){ //constructor
this.bookID = count;
this.author = author;
this.title = title;
bookOnloan.add(false);
count++;
}



public void setTitle(String title){
bookTitle.set(1,title);

}
public String getTitle(){
return bookTitle.toString();
}

public void setAuthor(String author){
bookTitle.set(1,author);

}
public String getAuthor(){
return bookAuthor.toString();
}
public String getName(){
return pupilName.toString();
}
public void setName(String name){
pupilName.set(1,name);
}
public String getDate(){
return issueDate.toString();
}
public void setDate(String date){
issueDate.set(1,date);
}

public Boolean getOnloan(){
return bookOnloan.add(false);
}
public void setOnLoan(Boolean onLoan){
bookOnloan.add(false);

}

}

接下来是我的子类

public class Fiction extends LoanBook {

private String type;

public Fiction(){

}


public Fiction(String title,String author, String type){
super(title,author); //calls constructor of the superclass
this.type = type;
}

public void setType(String type){
type = "Fiction";
}
public String getType(){
return type;
}

public String toString(){
return super.toString() + " The book type is: " + getType()+"\n";
}
}

以及其他子类

public class NonFiction extends LoanBook {

private String type;

public NonFiction(){

}


public NonFiction(String title,String author, String type){
super(title,author); //calls constructor of the superclass
this.type = type;
}

public void setType(String type){
type = "Fiction";
}
public String getType(){
return type;
}

public String toString(){
return super.toString() + " The book type is: " + getType()+"\n";
}
}

最佳答案

从过度使用静态字段到错误使用继承,再到将 Book 和 Book 集合的概念全部组合在一个类中,您的整个程序结构都被破坏了。

建议:

  • 不要将您的图书类别与您的图书 Collection 混在一起。这看起来是您的代码的主要问题。
  • 从 Book 类开始。它根本不应该包含任何列表。
  • 如果需要,您可以让 FictionBook 和 NonFictionBook 扩展 Book。
  • 或者您可以简单地为 Book 提供一个 boolean 字段“fiction”,然后根据需要将其设置为 true 或 false。
  • 创建一个包含图书列表的 LoanBook 类。
  • 除非存在真正的“is-a”关系,否则不要使用继承。您的代码不满足这一点主要是由于您的第一个问题,您将 Book 类与图书库代码混合在一起,这迫使您的小说书和非小说书继承库代码,这不仅不需要,而且非常有害.
  • 避免使用静态任何东西,除非它们是为了特定的静态目的而存在的。
  • 丢弃当前代码并重新开始可能会为您带来最好的服务。

关于java - 将数组与子类链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692500/

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