创建一个像ArrayList .add()一样工作的方法

时间:2015-01-21 04:01:39

标签: java arraylist

我正在编写一个程序,作为一个口袋'用户能够输入一种硬币,例如四分之一和它所具有的季度。我被分配到3个不同的类,硬币及其值可以在其中实现的硬币类,口袋类,我必须编写一个可以添加用户硬币的方法(基本上该方法会像ArrayList .add())和PocketClass测试器。我已经编写了大部分代码,但我仍然不知道如何编写以下方法:

 public void addCoin(String s, int i)
     {
          // s is type of coin, you are using s to instantiate a Coin and get value
         // i is number of coins, you are using i to keep adding value to the    totalValue
     }

我的问题是我应该怎样处理这个问题?我不太清楚如何创建方法。我会使用for循环来跟踪硬币的数量吗?我知道addCoin方法与ArrayList中的.add()非常相似。

以下是我其他课程的代码:

public class Coin
{
    private final String DOLLAR = "DOLLAR";
    private final String QUARTER = "QUARTER";
    private final String DIME = "DIME";
    private final String NICKEL = "NICKEL";
    private final String PENNY = "PENNY";
    private int value;
    private String coinName;
    public Coin(String s,int count)//name of the coin and also the number of the coins you have 
    {
        //Use if or switch statement to identify incoming string and provide value
       double value=0;
        if(DOLLAR.equalsIgnoreCase(s))
        {
            value=100.0;
        }
        else if(QUARTER.equalsIgnoreCase(s))
        {
            value=25.0;
        }
        else if(DIME.equalsIgnoreCase(s))
        {
            value=10.0;

        }
        else if(NICKEL.equalsIgnoreCase(s))
        {
            value=5.0;
        }
        else if(PENNY.equalsIgnoreCase(s))
        {
            value=1.0;
        }

    }

    public int getValue()
    {
        return value;
    }
}

以及Pocket类的结构:

public class Pocket
{
   private int currentValue; 
   private int totalValue; 
   private Coin quarter;
   private Coin dime;
   private Coin nickle;
   private Coin penny;


   public Pocket()
   {  //Set initial value to zero
      totalValue = 0;
      currentValue = 0;
   }
   public void addCoin(String s, int i)
   {
       // s is type of coin, you are using s to instantiate a Coin and get value
      // i is number of coins, you are using i to keep adding value to the totalValue




   }
   public int getValue()
   {
       return totalValue;
    }
   public void printTotal()
   {
    //print out two different output

   }
}

2 个答案:

答案 0 :(得分:0)

我假设你在addCoin课程中添加Pocket方法。

如果您打算跟踪Pocket内每种类型的硬币数量,最简单的方法是声明一个由硬币类型键入的Hashmap(例如,&# 34;季度"或"美元")并按该类型的硬币数量计算。调用addCoin(type, count)方法,比如addCoin("dollar", 5)可以检查哈希映射是否已经包含一个名为" $"如果存在,请将其值增加count

答案 1 :(得分:0)

我建议将硬币存储在列表中,以便您可以添加不限数量的硬币。

示例:

class Coin{
  //Same as your code....

  public Coin(String coinType){
  //..Same as your code, but removed number of coins
  }
}

公共类口袋    {      private int currentValue;      private int totalValue;      //创建一个硬币列表来存储无限数量的硬币      //一个口袋可以半个5角钱      列出硬币;

 public Pocket(){  
    //Set initial value to zero
    totalValue = 0;
    currentValue = 0;
    coins = new ArrayList<Coin>();
 }
 /**
  * This method will take only one coin at a time
  **/
 public void addCoin(String s){
   Coin c = new Coin(s);
   coins.add(c);
   totalValue+=c.getValue();
 }
 /**
  * This method will take any number of coins of same type
  **/
 public void addCoin(String s, int c){
   //Add each one to array
   for(int i=0;i<c;i++)[
      addCoin(s);
   }
 }
}

我不赞成在一个Coin对象中保留多个硬币值,因为它不是对象的真实表示。这意味着明天,如果你想存储其他硬币属性,如“印刷年份”,“硬币上的总统画面”等,你将会遇到困难。在我看来,最好在程序中使用一个对象实例来表示一个真实世界对象(这里是一个硬币),