简体   繁体   中英

Remove object from array with multiple classes in Java

I'm fairly new in Java, and I having some difficulties on this exercise. I have to create a "Piggy Bank" where the user can deposit coins with different values and currency (Euro, Dolar, Real etc), as well as remove them afterwards and convert the total value to the desired currency. It's proposed that we create a abstract superclass for Coin, and three subclasses for Euro, Dolar and Real. I could get pretty far without a hussle, but I can't figure out how to remove, for example, any 10 dollar coin in the bank, and leave others 10 dollars or 10 euros coins untouched, like a Iterator that scans the array and when he meet the criteria (Dollar, value 10), he removes that object and stop running. I'm creating an array list and putting coins on it through this blocks of code:

ArrayList<Coin> list = new ArrayList<Coin>();

double euroCoinValue = scan.nextDouble();
list.add(new Euro(euroCoinValue)); 

double dollarCoinValue = scan.nextDouble();
list.add(new Dollar(dollarCoinValue)); 

Here is my understanding of what you wanted to do. Let me know if I missed your actual question.

import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    Main main = new Main();
    main.testRemoveOneOfTheDollars();

    main.testRemoveOneReal();

    main.testRemoveTwoReal();

  }

  private PiggyBank setup() {
    PiggyBank piggyBank = new PiggyBank();
    piggyBank.add(new Euro(BigDecimal.valueOf(10L)));
    piggyBank.add(new Dollar(BigDecimal.valueOf(10L)));
    piggyBank.add(new Euro(BigDecimal.valueOf(10L)));
    piggyBank.add(new Dollar(BigDecimal.valueOf(10L)));
    piggyBank.add(new Real(BigDecimal.valueOf(10L)));
    return piggyBank;

  }

  public void testRemoveOneOfTheDollars() {
    PiggyBank piggyBank = setup();
    piggyBank.printCurrentState();
    piggyBank.remove("Main$Dollar");

  }

  public void testRemoveOneReal() {
    PiggyBank piggyBank = setup();
    piggyBank.printCurrentState();
    piggyBank.remove("Main$Real");
    piggyBank.printCurrentState();
  }

  public void testRemoveTwoReal() {
    PiggyBank piggyBank = setup();
    piggyBank.printCurrentState();
    piggyBank.remove("Main$Real");
    piggyBank.printCurrentState();
    try {
      piggyBank.remove("Main$Real");
    } catch (IllegalStateException e) {
      System.out.println(e.getMessage());
    }
    piggyBank.printCurrentState();
  }

  class PiggyBank {
    private List<Coin> coins;

    public PiggyBank() {
      coins = new ArrayList<Coin>();
    }

    public void add(Coin newCoin) {
      System.out.println("Adding coin [" + newCoin + "]");
      coins.add(newCoin);
    }

    public Coin remove(String coinType) {
      System.out.println("Removing coin of type [" + coinType + "]");
      int numOfCoins = coins.size();
      for (int i = 0; i < numOfCoins; i++) {
        Coin toReturn = coins.get(i);
        if (toReturn.getClass().getName().equalsIgnoreCase(coinType)) {
          coins.remove(i);
          System.out.println("Removed coin [" + toReturn + "]");
          return toReturn;
        }
      }
      throw new IllegalStateException("No Coins of type [" + coinType + "] exist in PiggyBank");
    }

    public void printCurrentState() {
      List<String> coinStrs = coins.stream().map(Coin::toString).collect(Collectors.toList());
      System.out.println("PiggyBank Current State [" + coinStrs + "]");

    }

  }

  abstract class Coin {
    protected BigDecimal value;

    public Coin(BigDecimal value) {
      this.value = value;
    }

    public BigDecimal getValue() {
      return value;
    }

    public String toString() {
      return "value: [" + getValue() + "], type [" + this.getClass().getName() + "]\n";
    }

  }

  class Euro extends Coin {
    public Euro(BigDecimal value) {
      super(value);
    }
  }

  class Dollar extends Coin {
    public Dollar(BigDecimal value) {
      super(value);
    }
  }

  class Real extends Coin {
    public Real(BigDecimal value) {
      super(value);
    }
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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