fork download
  1. import java.util.Scanner;
  2. public class BankApp {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in);
  5. Bank myBank = new Bank();
  6.  
  7. int user_choice = 2;
  8.  
  9. do {
  10. //display menu to user
  11. //ask user for his choice and validate it (make sure it is between 1 and 6)
  12. System.out.println();
  13. System.out.println("1) Open a new bank account");
  14. System.out.println("2) Deposit to a bank account");
  15. System.out.println("3) Withdraw to bank account");
  16. System.out.println("4) Print short account information");
  17. System.out.println("5) Print the detailed account information including last transactions");
  18. System.out.println("6) Quit");
  19. System.out.println();
  20. System.out.print("Enter choice [1-6]: ");
  21. user_choice = s.nextInt();
  22. switch (user_choice) {
  23. case 1: System.out.println("Enter a customer name");
  24. String cn = s.next();
  25. System.out.println("Enter a opening balance");
  26. double d = s.nextDouble();
  27. System.out.println("Account was created and it has the following number: " + myBank.openNewAccount(cn, d));
  28. break;
  29. case 2: System.out.println("Enter a account number");
  30. int an = s.nextInt();
  31. System.out.println("Enter a deposit amount");
  32. double da = s.nextDouble();
  33. myBank.depositTo(an, da);
  34. break;
  35. case 3: System.out.println("Enter a account number");
  36. int acn = s.nextInt();
  37. System.out.println("Enter a withdraw amount");
  38. double wa = s.nextDouble();
  39. myBank.withdrawFrom(acn, wa);
  40. break;
  41. case 4: System.out.println("Enter a account number");
  42. int anum = s.nextInt();
  43. myBank.printAccountInfo(anum);
  44. break;
  45. //case 5: ... break;
  46. }
  47. }
  48. while (user_choice != '6');
  49. }
  50.  
  51. static class Bank {
  52. private BankAccount[] accounts; // all the bank accounts at this bank
  53. private int numOfAccounts; // the number of bank accounts at this bank
  54.  
  55. // Constructor: A new Bank object initially doesn’t contain any accounts.
  56. public Bank() {
  57. accounts = new BankAccount[100];
  58. numOfAccounts = 0;
  59. }
  60.  
  61. // Creates a new bank account using the customer name and the opening balance given as parameters
  62. // and returns the account number of this new account. It also adds this account into the account list
  63. // of the Bank calling object.
  64. public int openNewAccount(String customerName, double openingBalance) {
  65.  
  66. BankAccount b = new BankAccount(customerName, openingBalance);
  67. accounts[numOfAccounts] = b;
  68. numOfAccounts++;
  69. return b.getAccountNum();
  70. }
  71.  
  72. // Withdraws the given amount from the account whose account number is given. If the account is
  73. // not available at the bank, it should print a message.
  74. public void withdrawFrom(int accountNum, double amount) {
  75. for (int i =0; i<numOfAccounts; i++) {
  76. if (accountNum == accounts[i].getAccountNum() ) {
  77. accounts[i].withdraw(amount);
  78. System.out.println("Amount withdrawn successfully");
  79. return;
  80. }
  81. }
  82. System.out.println("Account number not found.");
  83. }
  84.  
  85. // Deposits the given amount to the account whose account number is given. If the account is not
  86. // available at the bank, it should print a message.
  87. public void depositTo(int accountNum, double amount) {
  88. for (int i =0; i<numOfAccounts; i++) {
  89. if (accountNum == accounts[i].getAccountNum() ) {
  90. accounts[i].deposit(amount);
  91. System.out.println("Amount deposited successfully");
  92. return;
  93. }
  94. }
  95. System.out.println("Account number not found.");
  96. }
  97.  
  98. // Prints the account number, the customer name and the balance of the bank account whose
  99. // account number is given. If the account is not available at the bank, it should print a message.
  100. public void printAccountInfo(int accountNum) {
  101. for (int i =0; i<numOfAccounts; i++) {
  102. if (accountNum == accounts[i].getAccountNum() ) {
  103. System.out.println(accounts[i].getAccountInfo());
  104. return;
  105. }
  106. }
  107. System.out.println("Account number not found.");
  108. }
  109.  
  110. // Prints the account number, the customer number and the balance of the bank account whose
  111. // account number is given, together with last n transactions on that account. If the account is not
  112. // available at the bank, it should print a message.
  113. public void printAccountInfo(int accountNum, int n) {
  114. for (int i =0; i<numOfAccounts; i++) {
  115. if (accountNum == accounts[i].getAccountNum() ) {
  116. System.out.println(accounts[i].getAccountInfo());
  117. System.out.println(accounts[i].getTransactionInfo(n));
  118. return;
  119. }
  120. }
  121. System.out.println("Account number not found.");
  122. }
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. static class BankAccount{
  131.  
  132. private int accountNum;
  133. private String customerName;
  134. private double balance;
  135. private double[] transactions;
  136. private int numOfTransactions;
  137. private static int noOfAccounts=0;
  138.  
  139. public String getAccountInfo(){
  140. return "Account number: " + accountNum + "\nCustomer Name: " + customerName + "\nBalance:" + balance +"\n";
  141. }
  142.  
  143. public String getTransactionInfo(int n)
  144. {
  145. numOfTransactions = n;
  146. return n;
  147.  
  148. }
  149.  
  150. public BankAccount(String abc, double xyz){
  151. customerName = abc;
  152. balance = xyz;
  153. noOfAccounts ++;
  154. accountNum = noOfAccounts;
  155. transactions = new double[100];
  156. transactions[0] = balance;
  157. numOfTransactions = 1;
  158. }
  159.  
  160. public int getAccountNum(){
  161. return accountNum;
  162. }
  163. public void deposit(double amount){
  164.  
  165. if (amount<=0) {
  166. System.out.println("Amount to be deposited should be positive");
  167. } else {
  168. balance = balance + amount;
  169. transactions[numOfTransactions] = amount;
  170. numOfTransactions++;
  171. }
  172. }
  173. public void withdraw(double amount)
  174. {
  175. if (amount<=0){
  176. System.out.println("Amount to be withdrawn should be positive");
  177. }
  178. else
  179. {
  180. if (balance < amount) {
  181. System.out.println("Insufficient balance");
  182. } else {
  183. balance = balance - amount;
  184. transactions[numOfTransactions] = amount;
  185. numOfTransactions++;
  186. }
  187. }
  188. }
  189.  
  190. }//end of class
  191. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:2: error: class BankApp is public, should be declared in a file named BankApp.java
public class BankApp {
       ^
Main.java:146: error: incompatible types: int cannot be converted to String
            return n;
                   ^
2 errors
stdout
Standard output is empty