Wie muss mein Code aussehen (Informatik)?

2 Antworten

Das kannst du im Prinzip einfach so aufschreiben, wie es in der Aufgabe steht:

enum CoinType {
	SILVER,
	BRASS,
	COPPER
}

public interface WithCoinTypes {
	public void setNumberOfCoinsOfType(CoinType coinType, int number);
	public int getNumberOfCoinsOfType(CoinType coinType);
}

Das enum musst du evtl. auch in eine eigene Datei packen und public machen. Ich weiß leider nicht, was in Kapitel 01e der FOP steht.

public class WithCoinTypes {

  private int silverCoins;

  private int brassCoins;

  private int copperCoins;

  public void setNumberOfCoinsOfType(CoinType type, int number) {

    if (type == CoinType.SILVER) {

      this.silverCoins = number;

    } else if (type == CoinType.BRASS) {

      this.brassCoins = number;

    } else if (type == CoinType.COPPER) {

      this.copperCoins = number;

    }

  }

  public int getNumberOfCoinsOfType(CoinType type) {

    if (type == CoinType.SILVER) {

      return this.silverCoins;

    } else if (type == CoinType.BRASS) {

      return this.brassCoins;

    } else if (type == CoinType.COPPER) {

      return this.copperCoins;

    }

  }

}