Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package assignmentb;
- import java.util.Scanner;
- /**
- *
- * @author elyse
- */
- public class FindUnsafeBanks {
- public static void main(String[] args) {
- //declare scanner
- Scanner input = new Scanner(System.in);
- System.out.println("Enter the number of banks here:"); //prompt user to enter # of banks
- int n = input.nextInt(); //where n is the number of banks entered
- System.out.println("Enter presecribed limit here: "); //prompt user to enter limit of the total assets
- int limit = input.nextInt(); //where limit is the minimum amount of assets deemed to be safe
- //establish arrays to capture values
- double [] balance = new double[n]; //this array will have length n (the number of banks)
- double [][] borrowers = new double[n][n]; //the array will have length n (number of banks) and n number of cells in each row
- // if a bank is unsafe transfer values to a new array
- boolean[] unsafe = new boolean[n]; //the values of the banks which are deemed to be unsafe are transfered to this array
- //what is the current bank's balance/asset info?
- for (int i = 0; i < n; i++) {
- 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;
- 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 (double[] borrower : borrowers) {
- if (borrower[i] != 0) {
- borrower[i] = 0;
- // go through all the debt
- // 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 + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement