简体   繁体   English

计算Arraylist中的书数

[英]Count number of books in Arraylist

I am new to Java programming and studying it at school. 我是Java编程的新手,正在学校学习它。

We have been given an assignment to create a small library, with the following classes Library, Book, Author and Copy. 我们已经分配了一个创建小型图书馆的任务,该图书馆具有以下类别的图书馆,书籍,作者和复制。 With a given class Biblio which has predefined code and adds the books to the class book in an arraylist in Class Copy. 对于给定的Biblio类,它具有预定义的代码,并将这些书添加到Class Copy中数组列表中的书中。

The UML Domain is attached so you know the flow of the classes Everything is working fine and the generated output is correct. 附加了UML域,因此您知道类的流程一切正常,并且生成的输出正确。

There is just one method in class Library that is not working, the int method has to count the number of Copy's based on the Class Book (String): I have to go through the Arraylist in Class Copy and look for a specific book and return the number of copy's. 类库中只有一种方法不起作用,int方法必须根据类簿(字符串)计算Copy的数量:我必须遍历类拷贝中的Arraylist并查找特定的书并返回副本的数量。 Sorry for the Dutch language in the code. 很抱歉代码中的荷兰语。 I have tried multiple steps using a for loop Now I have found a similar post the uses hashset, I have tried below code but the return comes back with 0. (There are 3 copy's) 我已经尝试过使用for循环的多个步骤现在我发现了一个类似的用法hashset,我尝试了以下代码,但是返回结果为0。(有3个副本)

package domein;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import domein.Boek;
import domein.Exemplaar;

/**
 *
 * Klasse die verantwoordelijk is voor het beheer van boeken
 *
 */
public class Bibliotheek {
  private ArrayList < Exemplaar > boekenlijst = new ArrayList < Exemplaar > ();


  /**
   *
   * Print de naam van de Bibliiotheek.
   *
   */
  public Bibliotheek(String string) {
    System.out.println(string);

  }

  /**
   * Methode om een alle exmeplaren van de opgegeven boek toe te voegen aan de
   * lijst.
   *
   * @param b1
   * @param exemplaar
   */

  public void voegToe(Boek b1, int exemplaar) {
    for (int i = 0; i < exemplaar; i++) {
      Exemplaar e = new Exemplaar(b1, exemplaar);
      boekenlijst.add(e);

    }

  }

  /**
   * Methode om een boek toe te voegen aan de lijst.
   *
   * @param b2
   */
  public void voegToe(Boek b2) {

    Exemplaar f = new Exemplaar(b2, 1);

    boekenlijst.add(f);
  }

  /**
   * Hiermee worden alle boeken van de bibliotheek opgevraagd en getoond op het
   * scherm.
   *
   * @return
   */
  public ArrayList < String > toonCollectie() {
    ArrayList < String > titels = new ArrayList < String > ();
    for (Exemplaar boek: boekenlijst) {

      System.out.println("Exemplaar --> " + boek.getTitel() + " " + boek.getAuteur() + " in taal " + boek.getTaal());
    }
    return titels;
  }

  /**
   * Hiermee worden alle Engelse boeken van de bibliotheek opgevraagd en worden
   * de exemeplaren in het Engels getoond op het scherm.
   *
   * @param string
   * @return
   */
  public ArrayList < Exemplaar > toonCollectie(String string) {
    for (Exemplaar boek: boekenlijst) {
      if (boek.getTaal() == string)

        System.out.println("Exemplaar --> " + boek.getTitel() + " " + boek.getAuteur() + " in taal " + boek.getTaal());
    }
    return boekenlijst;

  }

  /**
   * Hiermee worden alle auteurs welke een prijs hebben opgevraagd in de
   * bibliotheek en degene met een prijs worden op het scherm getoond.
   *
   * @return
   */
  public boolean printAuteurs() {
    for (Exemplaar auteur: boekenlijst)
      if (auteur.getPrijs(true))
        System.out.println(auteur.getAuteur());
    return false;

  }

  /**
   * Een methode om door de boekenlijst te gaan en de totaal aantal exemplaren
   * op te vragen.
   *
   * @param b1
   * @return
   */

  public int telExemplaren(Boek b1) {
    Set < String > set = new HashSet < String > ();
    int count = 0;
    for (Exemplaar element: boekenlijst) {
      String names = element.getTitel();
      set.add(names);
    }

    for (String a: set) {
      for (Exemplaar element: boekenlijst) {
        String names = element.getTitel();
        if (a.equals(names)) {
          count++;
        }
      }

      count = 0;
    }

    return count;

  }
}

Any help is much appreciated. 任何帮助深表感谢。 I have added the complete code 我已经添加了完整的代码

Removing the line 拆除线

count = 0;

from near the end of the function should help it do something more useful than currently. 从函数的末尾开始应该可以帮助它做一些比当前有用的事情。

Your telExemplaren() method completely ignores the Boek b1 parameter. 您的telExemplaren()方法完全忽略了Boek b1参数。

Perhaps this is what you wanted to do? 也许这就是您想要做的?

public int telExemplaren(Boek b1) {
  int count = 0;
  for (Exemplaar element: boekenlijst) {
    if (b1.getTitel().equals(element.getTitel())) {
      count++;
    }
  }
  return count;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM