Write a method that will withdraw money from a particular customer's account and return the balance on the account. If there is not enough money on the account, the method will return -1
The signature of the method - withdraw (String [] clients, int [] balances, String client, int amount)
package Lesson5;
/**
* Created by Ruslan on 12.06.2017.
*/
public class takeOffBalance {
public static void main(String[] args) {
String clients[] = {"John", "Pedro", "Valera", "Muchachos", "Vovan"};
int [] balances = {1, 100, 3500, 222, 1234};
System.out.println(withdraw(clients, balances, "Pedro", (int) 10000));
}
static int withdraw(String[] clients, int[] balances, String client, int amount) {
int res = balances[findClient(clients, client)] - amount;
return res >= 0 ? res : -1;
}
static int findClient (String [] clients, String client) {
int clientIndex = 0;
for (String name : clients) {
if (name == client) {
break;
}
clientIndex++;
}
return clientIndex;
}
}