fork(15) download
  1. import java.awt.HeadlessException;
  2. import java.text.DecimalFormat;
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6.  
  7. public class Main {
  8.  
  9. /**
  10. * Design an abstract class named BankAccount to hold the following data for a bank account:
  11. • Balance
  12. • Number of deposits this month
  13. • Number of withdrawals
  14. • Annual interest rate
  15. • Monthly service charges
  16. The class should have the following methods:
  17. // See Book
  18. *
  19. * Next, design a SavingsAccount class that extends the BankAccount class. The
  20. SavingsAccount class should have a status field to represent an active or inactive account. If the
  21. balance of a savings account falls below $25, it becomes inactive. (The status field could be a
  22. boolean variable.) No more withdrawals may be made until the balance is raised above $25, at
  23. which time the account becomes active again. The savings account class should have the following
  24. methods:
  25. // See Book
  26. *
  27. *
  28. */
  29. public static class SavingsAccount extends BankAccount {
  30. protected boolean isActive = (super.balance > 25);
  31.  
  32. // Constructors
  33. public SavingsAccount() {
  34. super();
  35. }
  36. public SavingsAccount(double balance, double annualInterest) {
  37. super(balance, annualInterest);
  38. }
  39.  
  40. public double getMonthlyCharge() {
  41. this.monthlyProcess();
  42. return monthlyCharge;
  43. }
  44.  
  45. // Monthly charge process.
  46. public void monthlyProcess() {
  47. if (super.numberOfWithdrawals > 4) {
  48. super.monthlyCharge += super.numberOfWithdrawals - 4;
  49. if (super.balance < 25) {
  50. this.isActive = false;
  51. }
  52. }
  53. }
  54.  
  55.  
  56. // Withdraw.
  57. public boolean withdraw(double amount) {
  58. if (isActive) {
  59. return super.withdraw(amount);
  60. } else {
  61. return false;
  62. }
  63. }
  64.  
  65. // Deposit
  66. public boolean deposit(double amount) {
  67. super.deposit(amount);
  68. if ((!this.isActive) && super.balance > 25) {
  69. this.isActive = true;
  70. return true;
  71. } else {
  72. return false;
  73. }
  74.  
  75. }
  76. public boolean getIsActive() {
  77. return isActive;
  78. }
  79. public void setActive(boolean isActive) {
  80. this.isActive = isActive;
  81. }
  82.  
  83. }
  84.  
  85. public static abstract class BankAccount {
  86. protected double balance;
  87. protected int numberOfDeposits;
  88. protected int numberOfWithdrawals;
  89. protected double annualInterest;
  90. protected double monthlyCharge;
  91.  
  92. // Constructor accepts annual interest and
  93. // the balance.
  94. public BankAccount(double balance, double annualInterest) {
  95. this.setAnnualInterest(annualInterest);
  96. this.setBalance(balance);
  97. }
  98. // Default constructor.
  99. public BankAccount() {}
  100.  
  101. // Monthly charge process.
  102. public void monthlyProcess() {
  103. this.balance -= this.monthlyCharge;
  104. this.calcInterest();
  105. this.monthlyCharge = 0;
  106. this.numberOfDeposits = 0;
  107. this.numberOfWithdrawals = 0;
  108. }
  109.  
  110. // Calculate the interest.
  111. protected void calcInterest() {
  112. double monthlyInterestRate;
  113. double monthlyInterest;
  114.  
  115. monthlyInterestRate = this.annualInterest / 12;
  116. monthlyInterest = this.balance * monthlyInterestRate;
  117. this.balance = this.balance + monthlyInterest;
  118. }
  119.  
  120. // Withdraw.
  121. public boolean withdraw(double amount) {
  122. if ((this.balance > amount) && (amount > 0)) {
  123. this.balance -= amount;
  124. this.numberOfWithdrawals++;
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130.  
  131. // Deposit
  132. public boolean deposit(double amount) {
  133. if (amount > 0) {
  134. this.balance += amount;
  135. this.numberOfDeposits++;
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. }
  141.  
  142. // Accessors and mutators.
  143. public double getBalance() {
  144. return balance;
  145. }
  146.  
  147. public void setBalance(double balance) {
  148. this.balance = balance;
  149. }
  150.  
  151. public int getNumDepositsThisMonth() {
  152. return numberOfDeposits;
  153. }
  154.  
  155. public void setNumDepositsThisMonth(int numDepositsThisMonth) {
  156. this.numberOfDeposits = numDepositsThisMonth;
  157. }
  158.  
  159. public double getAnnualInterest() {
  160. return annualInterest;
  161. }
  162.  
  163. public void setAnnualInterest(double annualInterest) {
  164. this.annualInterest = annualInterest;
  165. }
  166.  
  167. public double getMonthlyCharge() {
  168. return monthlyCharge;
  169. }
  170.  
  171. public void setMonthlyCharge(double monthlyCharge) {
  172. this.monthlyCharge = monthlyCharge;
  173. }
  174. public boolean getIsActive() {
  175. // TODO Auto-generated method stub
  176. return (Boolean) null;
  177. }
  178. }
  179.  
  180. public static void main(String[] args) {
  181. double balance = 0;
  182. double annualInterest = 0;
  183. String output;
  184. String input;
  185. DecimalFormat df = new DecimalFormat("#0.00");
  186.  
  187. do {
  188. try {
  189. output = "Enter your account balance $:";
  190. input = JOptionPane.showInputDialog(output);
  191.  
  192. balance = Double.parseDouble(input);
  193. if (balance < 0) {
  194. throw new NumberFormatException();
  195. }
  196. break;
  197. } catch (HeadlessException e) {
  198. errorMsg();
  199. } catch (NumberFormatException e) {
  200. errorMsg();
  201. }
  202. } while (true);
  203.  
  204. do {
  205. try {
  206. output = "Please enter your annual interest rate: \n"
  207. + "EXAMPLE: A 4% interst rate is 0.04";
  208. input = JOptionPane.showInputDialog(output);
  209.  
  210. annualInterest = Double.parseDouble(input);
  211. if (annualInterest < 0) {
  212. throw new NumberFormatException();
  213. }
  214. break;
  215. } catch (HeadlessException e) {
  216. errorMsg();
  217. } catch (NumberFormatException e) {
  218. errorMsg();
  219. }
  220. } while (true);
  221.  
  222. // Instantiate your object.
  223. BankAccount savings = new SavingsAccount(balance, annualInterest);
  224.  
  225. do {
  226. try {
  227. output = "Enter the number of withdrawals:";
  228. input = JOptionPane.showInputDialog(output);
  229.  
  230. savings.numberOfWithdrawals = Integer.parseInt(input);
  231. if (savings.numberOfWithdrawals < 0) {
  232. throw new NumberFormatException();
  233. }
  234. break;
  235. } catch (HeadlessException e) {
  236. errorMsg();
  237. } catch (NumberFormatException e) {
  238. errorMsg();
  239. }
  240. } while (true);
  241.  
  242. do {
  243. try {
  244. output = "Enter the number of deposits:";
  245. input = JOptionPane.showInputDialog(output);
  246.  
  247. savings.numberOfDeposits = Integer.parseInt(input);
  248. if (savings.numberOfDeposits < 0) {
  249. throw new NumberFormatException();
  250. }
  251. break;
  252. } catch (HeadlessException e) {
  253. errorMsg();
  254. } catch (NumberFormatException e) {
  255. errorMsg();
  256. }
  257. } while (true);
  258.  
  259. // Add the interest to the balance:
  260. savings.calcInterest();
  261.  
  262. output = "Account Balance with Interest: $" + df.format(savings.getBalance());
  263. output += "\nMonthly Charge that will be deducted: $" + df.format(savings.getMonthlyCharge());
  264. output += "\nAccount Status: ";
  265. output += savings.getIsActive() ? "Is Active" : "Not Active";
  266.  
  267. JOptionPane.showMessageDialog(null, output);
  268.  
  269. }
  270.  
  271. private static void errorMsg() {
  272. String output;
  273. output = "Error: There was an error with your entry";
  274. JOptionPane.showMessageDialog(null, output);
  275. }
  276.  
  277. }
  278.  
Runtime error #stdin #stdout #stderr 0.31s 323840KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.UnsatisfiedLinkError: /opt/jdk/jre/lib/i386/libfontmanager.so: libgcc_s.so.1: cannot open shared object file: No such file or directory
	at java.lang.ClassLoader$NativeLibrary.load(Native Method)
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1929)
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1835)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1119)
	at sun.font.FontManagerNativeLibrary$1.run(FontManagerNativeLibrary.java:61)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.font.FontManagerNativeLibrary.<clinit>(FontManagerNativeLibrary.java:32)
	at sun.font.SunFontManager$1.run(SunFontManager.java:339)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.font.SunFontManager.<clinit>(SunFontManager.java:335)
	at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:264)
	at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1113)
	at javax.swing.JComponent.getFontMetrics(JComponent.java:1623)
	at javax.swing.plaf.basic.BasicGraphicsUtils.getPreferredButtonSize(BasicGraphicsUtils.java:276)
	at javax.swing.plaf.basic.BasicButtonUI.getPreferredSize(BasicButtonUI.java:376)
	at javax.swing.plaf.basic.BasicButtonUI.getMinimumSize(BasicButtonUI.java:366)
	at javax.swing.JComponent.getMinimumSize(JComponent.java:1741)
	at javax.swing.plaf.basic.BasicOptionPaneUI.addButtonComponents(BasicOptionPaneUI.java:693)
	at javax.swing.plaf.basic.BasicOptionPaneUI.createButtonArea(BasicOptionPaneUI.java:630)
	at javax.swing.plaf.basic.BasicOptionPaneUI.installComponents(BasicOptionPaneUI.java:178)
	at javax.swing.plaf.basic.BasicOptionPaneUI.installUI(BasicOptionPaneUI.java:141)
	at javax.swing.JComponent.setUI(JComponent.java:663)
	at javax.swing.JOptionPane.setUI(JOptionPane.java:1860)
	at javax.swing.JOptionPane.updateUI(JOptionPane.java:1882)
	at javax.swing.JOptionPane.<init>(JOptionPane.java:1845)
	at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:568)
	at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:524)
	at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:474)
	at javax.swing.JOptionPane.showInputDialog(JOptionPane.java:440)
	at Main.main(Main.java:190)