代码之家  ›  专栏  ›  技术社区  ›  Alex Cristea

如何从另一个类的数组中打印特定行

  •  0
  • Alex Cristea  · 技术社区  · 6 年前

    因此,我试图只打印添加到阵列中的小说,我不知道如何。。 我还需要归还特定种类的书籍数量。 我是编程新手,所以请让我放松:)

    这是应用程序,我必须从控制台添加所有书籍 从控制台添加的每个单词都会触发一个特定的操作,以便打印我需要写的书打印、添加我需要写的书添加、书的类型、书的标题、作者等等。

    import java.util.*;
    public class App{
        public static void main(String [] args){
            Scanner s = new Scanner(System.in);
            Library bib = Library.getInstance();
            String line = "";
            line = s.nextLine();
            String[] v = line.split("\\s+");
            while(true){
            switch(v[0]){
                case "add": 
                    String title = v[2];
                    String autor = v[3];
                    Book c = null;
                    if("story".equals(v[1])){
                        c  = new Story(title, autor);
                    } else if("novel".equals(v[1])){
                        int pages = Integer.parseInt(v[4]);
                        c = new Novel(title, autor, pages);
                    } else if("poetry".equals(v[1])){
                        String publish = v[4];
                        c = new Poetry(title, autor, publish);
                    }
                    bib.adauga(c);
                    break;
                case "print" : 
                case "return" : 
                case "exit" : System.exit(0); break;
                default : System.out.println("Command " + v[0] + " does not exist");
            }
        }
    }
    }
    

    import java.util.*;
    public class Library{
        private static Library instance;
        List<Book> books;
    
        private Library(){
            books = new ArrayList<>();
        }
    
        public static Library getInstance(){
            if(instance == null){
                instance = new Library();
            }
            return instance;
        }
    
        public void adauga(Book c){
            books.add(c);
    
        }
    
        public void afisare(){
            for(Book c : books){
                System.out.println(c);
            }
        }
    
    
    }
    

    public abstract class Book{
        String title;
        String autor;
    
        public Book(String title, String autor){
            this.title=title;
            this.autor=autor;
        }
    
         @Override
        public String toString(){
            return  title + " " + autor;
        }
    }
    

    public class Novel extends Book {
        int pages;
    
        public Novel(String title, String autor, int pages){
            super(title, autor);
            this.pages=pages;
        }
    
         @Override
        public String toString(){
            return super.toString() + " " + pages;
        }
    }
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   Locke    6 年前

    可以使用instanceof检查对象的类型。

    //Go through every book
    for(Book book : bib.books){
        if(book instanceof Novel){
             System.out.println(book);
        }
    }
    
        2
  •  0
  •   azro    6 年前

    您已经创建了一个打印所有 books Library ,您需要对其进行修改以打印好的图书类型:

    使用 print novel 例如:

    // in App
    case "print" : bib.afisare(v[1]);
                   break;
    

    // in Library
    public void afisare(String type){
        for(Book c : books){
            if("novel".equals(type) && c instance of Novel)
                System.out.println(c);
            else if("story".equals(type) && c instance of Story)
                System.out.println(c);
            else if("poetry".equals(type) && c instance of Poetry)
                System.out.println(c);
        }
    }
    

    还有,因为你 while(true) 您将在无限循环中运行,切换后需要再次询问 在使用之前不要询问 do{}while();

    do{
        line = s.nextLine();
        String[] v = line.split("\\s+");
        switch(v[0]){ 
            ... 
        }
    }while(true);
    
        3
  •  0
  •   Levent Dag    6 年前

    您可以使用“.”从其他对象访问变量只要变量处于可见性(例如public或在同一个包中),或者您使用getter方法。

    所以在你的情况下,你可以这样做。

    final List<Book> books = Library.getInstance().getBooks();
    for(Book book : books) {
        if(book instanceof Novel) {
            System.out.println(((Novel)book).toString());
        }
    }
    

    需要添加到库

    public List<Book> getBooks() {
        return this.books;
    }
    

    这只是一个简单的例子,你可以在你的例子中使用你的书籍和页面,你可以根据需要随意编辑和使用它。