fork download
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. class BankAccount
  5. {
  6. private int balance;
  7.  
  8. public BankAccount(int initial_amount) {
  9. if (initial_amount >= 0)
  10. balance = initial_amount;
  11. else
  12. balance = 0;
  13. }
  14.  
  15. public int getBalance() {
  16. return balance;
  17. }
  18.  
  19. public boolean deposit(int amount) {
  20. boolean result = false;
  21. if (amount < 0)
  22. System.out.println("invalid deposit amount");
  23. else {
  24. balance = balance + amount;
  25. result = true;
  26. }
  27. return result;
  28. }
  29.  
  30. public boolean withdraw(int amount) {
  31. boolean result = false;
  32. if (amount < 0)
  33. System.out.println("invalid withdraw amount");
  34. else if (amount > balance)
  35. System.out.println("not enough balance");
  36. else {
  37. balance = balance - amount;
  38. result = true;
  39. }
  40. return result;
  41. }
  42. }
  43.  
  44. class BankReader
  45. {
  46. private String input_line = "";
  47. private Scanner scan;
  48.  
  49. public BankReader() {
  50. scan = new Scanner(System.in);
  51. }
  52. public char readCommand(String message) {
  53. System.out.print(message);
  54. input_line = scan.nextLine().toUpperCase();
  55. return input_line.charAt(0);
  56. }
  57.  
  58. public int readAmount()
  59. {
  60. int answer = 0;
  61. String s = input_line.substring(1, input_line.length());
  62.  
  63. if(s.length() > 0) {
  64. double dollars_cents = new Double(s).doubleValue();
  65. answer = (int)(dollars_cents*100);
  66. } else
  67. System.out.println("invalid command - input amount: 0");
  68. return answer;
  69. }
  70. }
  71.  
  72. class BankWriter
  73. {
  74. private BankAccount bank;
  75. private String last_transaction = "";
  76.  
  77. public BankWriter(BankAccount b) {
  78. bank = b;
  79. }
  80.  
  81. public String unconvert(int i) {
  82. return new DecimalFormat("0.00").format(i/100.0);
  83. }
  84.  
  85. public void setTransaction(String message, int amount)
  86. {
  87. last_transaction = message + " " + unconvert(amount);
  88. System.out.println("transaction: " + last_transaction);
  89. }
  90.  
  91. public void setTransaction(String message)
  92. {
  93. last_transaction = message;
  94. System.out.println("transaction: " + last_transaction);
  95. }
  96.  
  97. }
  98.  
  99. class AccountController
  100. {
  101. private BankReader reader;
  102. private BankAccount primary_account, secondary_account, account;
  103. private BankWriter primary_writer, secondary_writer, writer;
  104.  
  105. public AccountController (BankReader r, BankAccount a1,BankWriter w1, BankAccount a2, BankWriter w2)
  106. {
  107. reader = r;
  108. primary_account = a1;
  109. primary_writer = w1;
  110. secondary_account = a2;
  111. secondary_writer = w2;
  112. account = primary_account;
  113. writer = primary_writer;
  114. }
  115.  
  116. public void resetAccount (BankAccount new_account,BankWriter new_writer) {
  117. account = new_account;
  118. writer = new_writer;
  119. }
  120.  
  121. public void processTransactions()
  122. {
  123. char command = reader.readCommand("Commands (P/S/D/W/T/I/Q): ");
  124.  
  125. switch (command) {
  126. case 'P':
  127. resetAccount(primary_account, primary_writer);
  128. break;
  129. case 'S':
  130. resetAccount(secondary_account, secondary_writer);
  131. break;
  132. case 'Q':
  133. System.out.println("Quit");
  134. return;
  135. case 'D':
  136. {
  137. int amount = reader.readAmount();
  138. if (account.deposit(amount)) writer.setTransaction("deposit $", amount);
  139. else writer.setTransaction("deposit error ", amount);
  140. break;
  141. }
  142. case 'W':
  143. {
  144. int amount = reader.readAmount();
  145. if (account.withdraw(amount)) writer.setTransaction("withdraw $", amount);
  146. else writer.setTransaction("withdraw error ", amount);
  147. break;
  148. }
  149. case 'T':
  150. {
  151. int amount = reader.readAmount();
  152. if (account.withdraw(amount))
  153. {
  154. writer.setTransaction("withdraw $", amount);
  155. if(account == primary_account) resetAccount(secondary_account, secondary_writer);
  156. else resetAccount(primary_account, primary_writer);
  157. if(account.deposit(amount))
  158. {
  159. writer.setTransaction("deposit $", amount);
  160. }
  161. else
  162. {
  163. writer.setTransaction("deposit error ", amount);
  164. }
  165. }
  166. else
  167. {
  168. writer.setTransaction("withdraw error ", amount);
  169. }
  170. if(account == primary_account) resetAccount(secondary_account, secondary_writer);
  171. else resetAccount(primary_account, primary_writer);
  172. break;
  173. }
  174. case 'I':
  175. {
  176. int amount = reader.readAmount() / 100 * account.getBalance() / 100;
  177. if (account.deposit(amount)) writer.setTransaction("deposit $", amount);
  178. else writer.setTransaction("deposit error ", amount);
  179. break;
  180. }
  181. default:
  182. writer.setTransaction("invalid commpand: " + command);
  183. }
  184. System.out.println("=======================");
  185. System.out.println("== currently active : " + ((account == primary_account) ? "primary" : "secondary"));
  186. System.out.println("== primary account : " + primary_writer.unconvert(primary_account.getBalance()));
  187. System.out.println("== secondary account : " + secondary_writer.unconvert(secondary_account.getBalance()));
  188. System.out.println("=======================");
  189.  
  190. this.processTransactions();
  191. }
  192. }
  193.  
  194.  
  195. public class Main {
  196. public static void main(String[] args) {
  197. BankReader reader = new BankReader();
  198. BankAccount primary_account = new BankAccount(0);
  199. BankWriter primary_writer = new BankWriter(primary_account);
  200. BankAccount secondary_account = new BankAccount(0);
  201. BankWriter secondary_writer = new BankWriter(secondary_account);
  202. AccountController controller = new AccountController(reader,
  203. primary_account, primary_writer, secondary_account,
  204. secondary_writer);
  205. controller.processTransactions();
  206. }
  207. }
Runtime error #stdin #stdout #stderr 0.16s 38276KB
stdin
d 100
t 50
i 10
stdout
Commands (P/S/D/W/T/I/Q): transaction: deposit $ 100.00
=======================
== currently active : primary
== primary account : 100.00
== secondary account : 0.00
=======================
Commands (P/S/D/W/T/I/Q): transaction: withdraw $ 50.00
transaction: deposit $ 50.00
=======================
== currently active : primary
== primary account : 50.00
== secondary account : 50.00
=======================
Commands (P/S/D/W/T/I/Q): transaction: deposit $ 5.00
=======================
== currently active : primary
== primary account : 55.00
== secondary account : 50.00
=======================
Commands (P/S/D/W/T/I/Q): 
stderr
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
	at BankReader.readCommand(Main.java:54)
	at AccountController.processTransactions(Main.java:123)
	at AccountController.processTransactions(Main.java:190)
	at AccountController.processTransactions(Main.java:190)
	at AccountController.processTransactions(Main.java:190)
	at Main.main(Main.java:205)