Banks lend money to each other. In tough economic times, if a bank goes bankrupt
ID: 3640359 • Letter: B
Question
Banks lend money to each other. In tough economic times, if a bank goes bankrupt, it may not be able to pay back the loan (You might remember 2008 banking crisis). A bank’s total assets are its current balance plus its loans to other banks. Figure 1 is a diagram that shows five banks. The banks’ current balances are 25, 125, 175, 75, and 181 million dollars, respectively. The directed edge from node 1 to node 2 indicates that bank 1 lends 40 million dollars to bank 2.diagram at http://www.cise.ufl.edu/class/cop3502sp12/Files/PA4.pdf
Figure 1 Banks lend money to each other.
If a bank’s total assets are under a certain limit, the bank is unsafe. The money it borrowed cannot be returned to the lender, and the lender cannot count the
loan in its total assets. Consequently, the lender may also be unsafe, if its total assets are under the limit.
Write a program to find all unsafe banks. Your program reads the input as follows. It first reads two integers n and limit, where n indicates the number of banks and limit the minimum total assets for keeping a bank safe. It then reads n lines that describe the information for n banks with id from 0 to n-1. The first number in the line is the bank’s balance, the second number indicates the number of banks that borrowed money from the bank, and the rest are pairs of two numbers. Each pair describes a borrower. The first number in the pair is the borrower’s id and the second is the amount borrowed. For example, the input for the five banks in Figure 1 is as follows (note that the limit is 201):
>5 201
>25 2 1 100.5 4 320.5
>125 2 2 40 3 85
>175 2 0 125 3 75
>75 1 0 125
>181 1 2 125
The total assets of bank 3 are (75 + 125 = balance + loan), which is under 201. So bank 3 is unsafe. After bank 3 becomes unsafe, the total assets of bank 1 will fall below (125 + 40). So, bank 1 is also unsafe. The output of the program should be
>Unsafe banks are 3 1 (Hint: Use a two-dimensional array borrowers to represent loans. borrowers[i][j] indicates the loan that bank i loans to bank j. Once bank j becomes unsafe, borrowers[i][j] should be set to 0.)
Explanation / Answer
If you want to enable user input just disable the comments in input method and comment rest below. All it needs is to detect Which bank has failed.. I will let you do that..If not message me.public class BankTest {
int totalBanks;
double bankLimit;
Bank[] bankList;
BankTest() {
input(new java.util.Scanner(System.in));
}
class Bank {
String name;
double balance;
int borrowedBanks;
double totalAssets;
double[][] borrowers;
//Bank(String name, double balance, int borrowedBanks, String borrowers) {
Bank(String name, String bankInfo) {
java.util.StringTokenizer st = new java.util.StringTokenizer(bankInfo, " ");
int tokenCount = st.countTokens();
this.name = name;
this.balance = Double.parseDouble(st.nextToken());
this.borrowedBanks = Integer.parseInt(st.nextToken());
borrowers = new double[borrowedBanks][2];
int index = 0;
while (st.hasMoreTokens()) {
borrowers[index][0] = Double.parseDouble(st.nextToken());
totalAssets = totalAssets +(borrowers[index][1] = Double.parseDouble(st.nextToken()));
index++;
}
totalAssets = totalAssets + balance;
display();
}
void display() {
System.out.println(" Bank Name : "+this.name + " Current Balance : " + this.balance+"M");
System.out.println("Bank Loaned " + " Loan Amount");
System.out.println("--------------------------------");
for(int i=0;i<borrowers.length; i++) {
for(int j=0;j<2; j++) {
if(j==1)
System.out.print(borrowers[i][j]+"M ");
else
System.out.print(borrowers[i][j]+" ");
}
System.out.println();
}
System.out.println(" Assets (balance+loan) : "+this.totalAssets+"M");
}
}
void input(java.util.Scanner sc) {
//System.out.print("Enter Number Of Banks : ");
totalBanks = 5; //Integer.parseInt(sc.nextLine());
bankList = new Bank[totalBanks];
//System.out.print("Enter Banks Limit : ");
bankLimit = 201; //Integer.parseInt(sc.nextLine());
// System.out.println("Enter Bank-Info format : BAL BORROWED [BANK LOAN,....]");
/*for (int i = 0; i < totalBanks; i++) {
//bankList[i] = new Bank("" + i, sc.nextLine());
bankList[i] = new Bank("" + i, "25 2 1 100.5 4 320.5");
}*/
bankList[0] = new Bank("" + 0, "25 2 1 100.5 4 320.5");
bankList[1] = new Bank("" + 1, "125 2 2 40 3 85");
bankList[2] = new Bank("" + 2, "175 2 0 125 3 75");
bankList[3] = new Bank("" + 3, "75 1 0 125");
bankList[4] = new Bank("" + 4, "181 1 2 125");
}
public static void main(String[] args) {
BankTest test = new BankTest();
}
}
public class BankTest {
int totalBanks;
double bankLimit;
Bank[] bankList;
BankTest() {
input(new java.util.Scanner(System.in));
}
class Bank {
String name;
double balance;
int borrowedBanks;
double totalAssets;
double[][] borrowers;
//Bank(String name, double balance, int borrowedBanks, String borrowers) {
Bank(String name, String bankInfo) {
java.util.StringTokenizer st = new java.util.StringTokenizer(bankInfo, " ");
int tokenCount = st.countTokens();
this.name = name;
this.balance = Double.parseDouble(st.nextToken());
this.borrowedBanks = Integer.parseInt(st.nextToken());
borrowers = new double[borrowedBanks][2];
int index = 0;
while (st.hasMoreTokens()) {
borrowers[index][0] = Double.parseDouble(st.nextToken());
totalAssets = totalAssets +(borrowers[index][1] = Double.parseDouble(st.nextToken()));
index++;
}
totalAssets = totalAssets + balance;
display();
}
void display() {
System.out.println(" Bank Name : "+this.name + " Current Balance : " + this.balance+"M");
System.out.println("Bank Loaned " + " Loan Amount");
System.out.println("--------------------------------");
for(int i=0;i<borrowers.length; i++) {
for(int j=0;j<2; j++) {
if(j==1)
System.out.print(borrowers[i][j]+"M ");
else
System.out.print(borrowers[i][j]+" ");
}
System.out.println();
}
System.out.println(" Assets (balance+loan) : "+this.totalAssets+"M");
}
}
void input(java.util.Scanner sc) {
//System.out.print("Enter Number Of Banks : ");
totalBanks = 5; //Integer.parseInt(sc.nextLine());
bankList = new Bank[totalBanks];
//System.out.print("Enter Banks Limit : ");
bankLimit = 201; //Integer.parseInt(sc.nextLine());
// System.out.println("Enter Bank-Info format : BAL BORROWED [BANK LOAN,....]");
/*for (int i = 0; i < totalBanks; i++) {
//bankList[i] = new Bank("" + i, sc.nextLine());
bankList[i] = new Bank("" + i, "25 2 1 100.5 4 320.5");
}*/
bankList[0] = new Bank("" + 0, "25 2 1 100.5 4 320.5");
bankList[1] = new Bank("" + 1, "125 2 2 40 3 85");
bankList[2] = new Bank("" + 2, "175 2 0 125 3 75");
bankList[3] = new Bank("" + 3, "75 1 0 125");
bankList[4] = new Bank("" + 4, "181 1 2 125");
}
public static void main(String[] args) {
BankTest test = new BankTest();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.