Java中的钱包传输方法

时间:2016-11-14 01:19:33

标签: java arrays stringbuilder wallet

我正在开始Java课程并且正在为Wallet实验室工作。

我需要将一个钱包(捐赠钱包)的钱包内容转移到另一个钱包(接收钱包)的末尾。

我相信我的构造函数设置正确。

    import.java.util.Arrays;

    public class Wallet
    {
       private static final int MAX = 10;  //Max possible # of banknotes in a wallet

   // instance variables       
       private int contents[]; //array of banknotes
       private int count; //number of banknotes stored in contents[]

   /**
    * Default constructor for objects of class Wallet
    */
   public Wallet()
   {
        // initialize instance variables
        contents = new int[MAX];
        count = 0;       
   }

   /**
    * Overloaded constructor for objects of class Wallet
    */
   public Wallet(int a[])
   {
       contents = new int[MAX]; 

       for (int i = 0; i<a.length; i++)
       {
            contents=a;
            if (contents[i] > 0) 
                count++;
       }
   }

但需要帮助验证我写的下面的add()方法是否正确:

public void add(int banknote)//Not sure if this is right
   {  
       StringBuilder sb = new StringBuilder();

       for (int i = 0; i<contents.length; i++)
                sb.append(contents[i] + ", ");

       sb.append(banknote);

       count++;

   }

或者应该是:

 contents[count] = banknote;
  count++;

我还需要将捐赠者钱包的内容转移到接收器钱包,清空捐赠者钱包并添加到接收器,我写下面的代码,但它似乎关闭并且无法正常工作:

 public void transfer(Wallet donor)
   {

      for(int i = 0; i < count; i++)
        {
            add(donor.contents[i]);
            count++;
        }
      donor.count=0;
   }

关于我可能会出现这种错误的任何指导,现在已经持续了几个小时......谢谢

2 个答案:

答案 0 :(得分:0)

嗯,这应解决一些问题:

public Wallet(int a[]) {
  contents = new int[MAX];
  count = 0;
  for (int i = 0; i < a.length; i++) {
    if (a[i] > 0)
      contents[count++] = a[i];  
      // only increase your content position for actual notes
  }

public void add(int banknote) {
  contents[count++] = banknote;  
  /* this version makes a lot more sense
   * as you actually mutate your instance */
}

public void transfer(Wallet donor) {
  for(int i = 0; i < donor.count; i++) // gotta use donor.count here
    add(donor.contents[i]);
  donor.count=0;
}

答案 1 :(得分:-1)

更新:我建议您使用整数列表,因为这会简化您的代码。我在这个答案中的代码已经适应了这一点。

首先,您不需要跟踪有多少银行票据&#39;在数组中有一个单独的变量:只需调用contents.length,它将返回多少&#39;银行票据&#39;钱包里有。

然后,要在钱包中添加一张纸币,我会让您添加&#39; function返回布尔值:如果已添加帐单则为true;如果钱包没有足够的空间,则为false。为此,您可以执行以下操作:

static int MAX = 10;
static List<Integer> contents = new ArrayList<Integer>();

public static void main(String... args){
    add(5);
    add(10);

    transfer(Arrays.asList(1, 5, 2, 4));

    for(int note: contents){
        System.out.println(note);
    }
}

public static boolean add(int note){
    if(contents.size() >= MAX)
        return false; // The wallet is full, therefore we cannot add anything.

    contents.add(note);
    return true;
}

为了转移资金,您只需执行以下操作:

public static void transfer(List<Integer> donor){ // You would change 'int[]' to Wallet, and the code below according to this change
    for(int i = 0; i < donor.size(); i++) // Going through all the values of the donor's wallet
        if(!add(donor.get(i))) // If there is no space left, we must stop the transfer
            return;
}

启动此课程时,您的控制台应打印:

5
10
1
5
2
4

我希望这有帮助!