Balance checker

Report a typo

Create an algorithm that prevents an e-wallet account from going into negative balance. The account should be debited for each purchase sequentially while the balance allows it.

The input should include:

  1. On the first line, a single integer number representing the available balance.
  2. On the second line, a sequence of integers representing purchase payments, which should be processed one after another.

The output should contain:

  1. If there is not enough money for a purchase, output Error, insufficient funds for the purchase. Your balance is N, but you need M., where N is the remaining balance on the account and M is the cost of the items you cannot afford.
  2. If there is enough money for all the purchases, output Thank you for choosing us to manage your account! Your balance is N., where N is the amount of money left after the purchases.

Sample Input 1:

10000
5000 3000

Sample Output 1:

Thank you for choosing us to manage your account! Your balance is 2000.

Sample Input 2:

1000
500 500

Sample Output 2:

Thank you for choosing us to manage your account! Your balance is 0.

Sample Input 3:

5000
500 2000 3000

Sample Output 3:

Error, insufficient funds for the purchase. Your balance is 2500, but you need 3000.
Write a program in Kotlin
import java.util.Scanner

fun main() {
val scanner = Scanner(System.`in`)
var balance = readln().toInt()
while (scanner.hasNextInt()) {
// TODO
}
// TODO
}
___

Create a free account to access the full topic