Sunday 28 August 2016

Chapter 8 Exercise 17, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

***8.17 (Financial tsunami) 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. A bank’s total assets are its current balance plus its loans to other banks. The diagram in Figure 8.8 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.


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 the 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 is the minimum total assets for keeping a bank safe. It then reads n lines that describe the information for n banks with IDs 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 8.8 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), which is under 201, so bank 3 is unsafe. After bank 3 becomes unsafe, the total assets of bank 1 fall below (125 + 40). Thus, 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.) 



import java.util.Scanner;
 
 
 
public class ProgrammingEx8_17 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter number banks:");
  int n = input.nextInt();
 
  System.out.print("Enter the limit:");
  int limit = input.nextInt();
 
  double[] balance = new double[n];
  double[][] borrowers = new double[n][n];
  boolean[] unsafe = new boolean[n];
 
  for (int i = 0; i < n; i++) {
   // current bank's balance
   System.out.print("Enter the banks " + i + " asset info:");
   balance[i] = input.nextDouble();
   // number of banks that borrow money from current bank
   int m = input.nextInt();
   for (int j = 0; j < m; j++) {
    borrowers[i][input.nextInt()] = input.nextDouble();
   }
  }
 
  // Check for unsafe banks
  boolean unsafeFound = false;
  do {
   unsafeFound = false;
   for (int i = 0; i < n; i++) {
    // calculate bank i's asset
    double asset = balance[i];
    for (int j = 0; j < borrowers[i].length; j++) {
     asset += borrowers[i][j];
    }
 
    if (asset < limit) {
     unsafe[i] = true;
     // reset debt of the unsafe bank to zero
     for (int j = 0; j < borrowers.length; j++) {
      if (borrowers[j][i] != 0) {
       borrowers[j][i] = 0;
       // go trough all the banks again if some debt go
       // default
       unsafeFound = true;
      }
     }
    }
 
   }
 
  } while (unsafeFound);
 
  // Print the result out
 
  System.out.print("The unsafe banks are:");
  for (int i = 0; i < unsafe.length; i++) {
   if (unsafe[i] == true) {
    System.out.print(i + " ");
   }
 
  }
 
 }
 
}

No comments :

Post a Comment