Advertisement
Guest User

Question 1

a guest
Oct 22nd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package assignmentb;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11. *
  12. * @author elyse
  13. */
  14. public class FindUnsafeBanks {
  15.  
  16. public static void main(String[] args) {
  17. //declare scanner
  18. Scanner input = new Scanner(System.in);
  19. System.out.println("Enter the number of banks here:"); //prompt user to enter # of banks
  20. int n = input.nextInt(); //where n is the number of banks entered
  21.  
  22. System.out.println("Enter presecribed limit here: "); //prompt user to enter limit of the total assets
  23. int limit = input.nextInt(); //where limit is the minimum amount of assets deemed to be safe
  24.  
  25. //establish arrays to capture values
  26. double [] balance = new double[n]; //this array will have length n (the number of banks)
  27. double [][] borrowers = new double[n][n]; //the array will have length n (number of banks) and n number of cells in each row
  28.  
  29. // if a bank is unsafe transfer values to a new array
  30. boolean[] unsafe = new boolean[n]; //the values of the banks which are deemed to be unsafe are transfered to this array
  31.  
  32. //what is the current bank's balance/asset info?
  33. for (int i = 0; i < n; i++) {
  34. System.out.print("Enter the banks " + i + " asset info:");
  35. balance[i] = input.nextDouble();
  36.  
  37. // number of banks that borrow money from current bank
  38. int m = input.nextInt();
  39. for (int j = 0; j < m; j++) {
  40. borrowers[i][input.nextInt()] = input.nextDouble();
  41. }
  42. }
  43.  
  44. // Check for unsafe banks
  45. boolean unsafeFound;
  46. do {
  47. unsafeFound = false;
  48. for (int i = 0; i < n; i++) {
  49. // calculate bank i's asset
  50. double asset = balance[i];
  51. for (int j = 0; j < borrowers[i].length; j++) {
  52. asset += borrowers[i][j];
  53. }
  54.  
  55. if (asset < limit) {
  56. unsafe[i] = true;
  57. // reset debt of the unsafe bank to zero
  58. for (double[] borrower : borrowers) {
  59. if (borrower[i] != 0) {
  60. borrower[i] = 0;
  61. // go through all the debt
  62. // default
  63. unsafeFound = true;
  64. }
  65. }
  66. }
  67.  
  68. }
  69.  
  70. } while (unsafeFound);
  71.  
  72. // Print the result out
  73.  
  74. System.out.print("The unsafe banks are:");
  75. for (int i = 0; i < unsafe.length; i++) {
  76. if (unsafe[i] == true) {
  77. System.out.print(i + " ");
  78. }
  79.  
  80. }
  81.  
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement