簡體   English   中英

字符串和另一個數組列表的 ArrayList

[英]ArrayList of string and another arraylist

所以我正在制作一個程序來管理一家書店,這里有一個例子,說明應該如何處理以下主要內容:

        bookStore bookStore = new bookStore("Lelo");
        book l1 = new TecnicalBook("H. Schildt", "\"Java: The Complete Reference\"", 80, "Computer Science- Java");
        bookStore.addBook(l1);
        book l2= new AdventureBook("A. Christie", "\"The Man in the Brown Suit\"", 75, 14);
        bookStore.addBook(l2);
                
        
        Client c1 = new Premium("B. Antunes", "antunes@books.com");
        bookStore.addClient(c1);
        Client c2 = new Frequent("A. Oliveira", "oliveira@books.com");
        bookStore.addClient(c2);
        System.out.println("Initial stock:");
        bookStore.showBooks();
        bookStore.sale(l1, c1);
        System.out.println("Stock after selling:");
        bookStore.showBooks();
        System.out.println("Sales Volume:"+bookStore.SalesVolume);        
        bookStore.showClientsBooks(c1);

應該顯示這個結果:

Initial stock:
Author:H. Schildt   Title:"Java: The Complete Reference"   Base Price:80.0
Author:A. Christie   Title:"The Man in the Brown Suit"   Base Price:75.0
B. Antunes bought "Java: The Complete Reference", Tecnical Book of Computer Science- Java, for 72.0
Stock after selling:
Author:A. Christie   Title:"The Man in the Brown Suit"   Base Price:75.0
Sales Volume:72.0
B. Antunes bought: "UML Distilled". "The Man in the Brown Suit".

我唯一不能做的部分是最后一行,顯示每個客戶購買的產品,我正在考慮創建一個包含客戶名稱字符串和書籍類數組列表的類,然后有一個數組列表每個客戶的那個班級,但我不知道如何做到這一點,可能有更好的方法,謝謝

編輯:我有這門課:課本課技術書擴展書課冒險書擴展書

類客戶端類常規擴展客戶端類頻繁擴展客戶端類高級擴展客戶端

我有書店類,在那里我有一個書籍和客戶端對象的數組列表。

您可以將 Books 列表添加到您的 Client 類以跟蹤銷售情況:

class Client {

    ...

    private List<Book> books;

    ClientBooks(Client client) {
        ...

        this.books = new ArrayList<>();
    }

    public void addBook(Book book) {
        this.books.add(book);
    }

    public void printBooks() {
        for (Book book : this.books) {
            System.out.println(book);
        }
    }
}

銷售一本書可以這樣實現:

List<Client> clients = ...

String buyer = ...
Book book = ...

for(Client client : clients) {
    if (client.getName().equals(buyer)) {
        client.addBook(book);
    }
}

在每個類中覆蓋toString很重要。 然后當您打印該類的實例時,該字符串將打印而不是一些奇怪的值。

如果您願意,此方法還允許您一次添加所有書籍或單個書籍。

這門課沒有經過測試,但它應該讓你知道如何繼續。

class Client {
  String name;
  private List<Book> clientBooks = new ArrayList<>();
  public Client(String name) {
     this.name = name;
  }

  public addBooks(List<Book> books) {
      clientBooks.addAll(books);
  }

  public void addBook(Book bk) {
      clientBooks.add(bk);
  }

  public String toString() {
     StringBuilder sb = new StringBuilder(name).append(":");
     // add either the entire toString for book here or just selected
     // items like the title.  It's up to you.
     for (Book b :clientBooks) {
        sb.append(b.toString()).append(", ");
     }
     
     return sb.toString().substring(0,sb.length()-2);
  }
}

因此,我正在編寫一個管理書店的程序,以下是有關以下主要內容的示例:

        bookStore bookStore = new bookStore("Lelo");
        book l1 = new TecnicalBook("H. Schildt", "\"Java: The Complete Reference\"", 80, "Computer Science- Java");
        bookStore.addBook(l1);
        book l2= new AdventureBook("A. Christie", "\"The Man in the Brown Suit\"", 75, 14);
        bookStore.addBook(l2);
                
        
        Client c1 = new Premium("B. Antunes", "antunes@books.com");
        bookStore.addClient(c1);
        Client c2 = new Frequent("A. Oliveira", "oliveira@books.com");
        bookStore.addClient(c2);
        System.out.println("Initial stock:");
        bookStore.showBooks();
        bookStore.sale(l1, c1);
        System.out.println("Stock after selling:");
        bookStore.showBooks();
        System.out.println("Sales Volume:"+bookStore.SalesVolume);        
        bookStore.showClientsBooks(c1);

應顯示此結果:

Initial stock:
Author:H. Schildt   Title:"Java: The Complete Reference"   Base Price:80.0
Author:A. Christie   Title:"The Man in the Brown Suit"   Base Price:75.0
B. Antunes bought "Java: The Complete Reference", Tecnical Book of Computer Science- Java, for 72.0
Stock after selling:
Author:A. Christie   Title:"The Man in the Brown Suit"   Base Price:75.0
Sales Volume:72.0
B. Antunes bought: "UML Distilled". "The Man in the Brown Suit".

我唯一不能做的部分是最后一行,顯示每個客戶購買的產品,我正在考慮制作一個類,其中包含一個帶有客戶名稱的字符串和一本書類的數組列表,然后有一個數組列表每個客戶的課程,但我不確定如何制作,可能還有更好的方法,謝謝

編輯:我有這個課:課本課tecnicalbook擴展書

類客戶類常規擴展客戶類頻繁擴展客戶類高級擴展客戶

我有書店類,其中有書和客戶對象的數組列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM