There is a field for entering numbers, as well as the mWithdrawButton button, which minus the values ​​from 0 and the mDepositButton button, which adds values. All this is displayed in mAmountDisplay .
I would like to add the ability to display a finite number in the number of bills, for example, we made 475, in mAmountDisplay shows that our balance is 475, and then: 4 x 100; 1 X 50; 1 X 20; 1 x 5. Tell me how best to register.

  public class MainActivity extends Activity { private static final String TAG = "MainActivity"; EditText mAmountInput; Button mWithdrawButton; Button mDepositButton; TextView mAmountDisplay; BankAccount mCurrentAccount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mCurrentAccount = new SavingsAccount(); mAmountInput = (EditText)findViewById(R.id.amount_input); mWithdrawButton = (Button)findViewById(R.id.withdraw_button); mDepositButton = (Button)findViewById(R.id.deposit_button); mAmountDisplay = (TextView)findViewById(R.id.balance_display); mWithdrawButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String amount = mAmountInput.getText().toString(); mCurrentAccount.withdraw(Double.parseDouble(amount)); mAmountDisplay.setText("Balance is " + mCurrentAccount.getBalance()); } }); mDepositButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String amount = mAmountInput.getText().toString(); mCurrentAccount.deposit(Double.parseDouble(amount)); mAmountDisplay.setText("Balance is " + mCurrentAccount.getBalance()); } }); } public abstract class BankAccount { private ArrayList<Double> mTransactions; public static final double OVERDRAFT_FEE = 30; BankAccount(){ mTransactions = new ArrayList<Double>(); } public void withdraw(double amount){ mTransactions.add(-amount); if (getBalance() < 0) { mTransactions.add(-OVERDRAFT_FEE); } } protected int numberOfWithdrawals(){ int count = 0; for (int i = 0; i < mTransactions.size(); i++) { if(mTransactions.get(i) < 0) { count++; } } return count; } public void deposit(double amount){ mTransactions.add(amount); } public double getBalance(){ double total = 0; for(int i = 0; i < mTransactions.size(); i++){ total += mTransactions.get(i); } return total; } } public class SavingsAccount extends BankAccount { private static final String TAG = "SavingsAccount"; @Override public void withdraw(double amount) { if(numberOfWithdrawals() >= 3){ return; } super.withdraw(amount); } } 
  • one
    Do you have bills initially or just splitting the numbers into face values? Why 4x100, not 2x200, for example? - VAndrJ
  • Do you have a Double type - are you also using kopecks? - pavlofff
  • No, just splitting the number into face values. Double type added as a test (I will remove it later) the main task is to display the balance, which will be presented as a number of bills with n nominal values. - Morozov

2 answers 2

In general, I sketched a code for you in a hurry, it will give you the minimum number of bills from large to smaller (if I understood your problem correctly). I think you will figure out how to apply in your task.

  int [] note = {500, 200, 100, 50, 20, 10, 5, 2, 1}; int [] amountNote = new int [note.length]; int totalMoney = 12345; int temp; for(int i = 0; i < note.length; ++i){ temp = totalMoney % note[i]; if(totalMoney == 1){ amountNote[note.length - 1] = 1; break; } if(temp != 0){ amountNote[i] = totalMoney / note[i]; totalMoney = totalMoney - (amountNote[i] * note[i]); } } for(int i = 0; i < note.length; ++i){ if(amountNote[i] != 0) { Log.d("RESULT", "У вас " + amountNote[i] + " купюр по " + note[i]); } } 
  • Thank you, just about everything, just I don’t really understand how to implement such code in class 1, maybe in a fragment, how can it be transferred at all? - Morozov
  • you can move it anywhere. Add these variables to your class, substitute your bag in totalAmount. In your case, you need to parse the string in the int. To make a method from a cycle with logic of calculation of notes. And then output the result at your convenience (I brought the logs to the second cycle) - Eugene Troyanskii

something like this:

 public class MainActivity extends AppCompatActivity { int mBalance = 407; int mNominals[][] = {{100,50,10,5,1},{0,0,0,0,0}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView mTextBalance = (TextView)findViewById(R.id.textBalance); TextView mTextNominals = (TextView)findViewById(R.id.textNominals); mNominals = getNominals(mBalance, mNominals); mTextBalance.setText("Balance:" + mBalance); for (int i = 0; i < mNominals[0].length; i++) { if (mNominals [1][i] > 0) mTextNominals.append("Nominals " + mNominals[0][i] + " x " + mNominals[1][i] + " \n"); } } int[][] getNominals (int balance, int[][] nominals) { for (int i = 0 ; i < nominals[0].length; i++ ) { nominals[1][i] = balance / nominals[0][i]; balance = balance % nominals[0][i]; } return nominals; } } 

The getNominals() method takes as input the total amount and an array of denominations "bills". The output forms a two-dimensional array - the first row of denominations, the second row the number of "bills" of this denomination. When displaying a string with a zero number of "bills" ignored.

If the values ​​are fixed, then the array can be simplified to one-dimensional and the values ​​in the method can not be transferred, but right there can be initialized.

  • Yes, it divides everything as it should, but this is only if you initially set the value int mBalance = 407; And in my code when adding a deposit / issuance of a deposit, this value of mBalance will always change. I'm just trying to insert your code into my own, there is a conflict with displaying the values ​​in the onClick methods. I don’t understand how to tweak a bit ( - Morozov
  • In your case, after the line mAmountDisplay.setText("Balance is " + mCurrentAccount.getBalance()); write: mNominals = getNominals(mCurrentAccount.getBalance(), mNominals); get the resulting array divided into "banknotes" balance, as you see fit. Naturally, the getNominals() method must be present in the class, and the values ​​of mNominals[][] initialized before this. - pavlofff
  • Yes, now there is no error. But my output goes to mAmountDisplay.setText ("Balance is" + mCurrentAccount.getBalance ()); And there is just the current value of the balance without the resulting array. Please tell me how you can correctly output. - Morozov
  • @VadimMorozov Well, you see the answer and do the same. You write the program, from where I know how you need to correctly withdraw it. - pavlofff