Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. package lab6;
  2.  
  3. /**
  4. *
  5. * @author LAPPY
  6. */
  7. //*******************************************************
  8. // Account.java
  9. //
  10. // A bank account class with methods to deposit to, withdraw from,
  11. // change the name on, charge a fee to, and print a summary of the account.
  12. //*******************************************************
  13.  
  14. public class Account
  15. {
  16. private double balance;
  17. private String name;
  18. private long acctNum;
  19.  
  20. //----------------------------------------------
  21. //Constructor -- initializes balance, owner, and account number
  22. //----------------------------------------------
  23. public Account(double initBal, String owner, long number)
  24. {
  25. balance = initBal;
  26. name = owner;
  27. acctNum = number;
  28. }
  29.  
  30. //----------------------------------------------
  31. // Checks to see if balance is sufficient for withdrawal.
  32. // If so, decrements balance by amount; if not, prints message.
  33. //----------------------------------------------
  34. public void withdraw(double amount)
  35. {
  36. if (balance >= amount)
  37. balance -= amount;
  38. else
  39. System.out.println("Insufficient funds");
  40. }
  41.  
  42. //----------------------------------------------
  43. // Adds deposit amount to balance.
  44. //----------------------------------------------
  45. public void deposit(double amount)
  46. {
  47. balance += amount;
  48. }
  49.  
  50. //----------------------------------------------
  51. // Returns balance.
  52. //----------------------------------------------
  53. public double getBalance()
  54. {
  55. return balance;
  56. }
  57.  
  58.  
  59. //----------------------------------------------
  60. // Returns a string containing the name, account number, and balance.
  61. //----------------------------------------------
  62. public String toString()
  63. {
  64. return "Name: " + name + ", Account Number: " + acctNum + ", Balance: " + balance;
  65. }
  66.  
  67. //----------------------------------------------
  68. // Deducts $10 service fee
  69. //----------------------------------------------
  70. public void chargeFee()
  71. {
  72. balance = balance - 10;
  73. }
  74.  
  75. //----------------------------------------------
  76. // Changes the name on the account
  77. //----------------------------------------------
  78. public void changeName(String newName)
  79.  
  80. {
  81. name = newName;
  82. }
  83.  
  84. }
  85.  
  86. package lab6;
  87.  
  88.  
  89. /**
  90. *
  91. * @author LAPPY
  92. */
  93. // ****************************************************************
  94. // ManageAccounts.java
  95. //
  96. // Use Account class to create and manage Sally and Joe's
  97. // bank accounts
  98. // ****************************************************************
  99.  
  100. public class ManageAccounts
  101. {
  102. public static void main(String[] args)
  103. {
  104. Account acct1, acct2;
  105.  
  106. //create account1 for Sally with $1000
  107. acct1 = new Account(1000, "Sally", 1111);
  108.  
  109. //create account2 for Joe with $500
  110. acct2 = new Account(500, "Joe", 1112);
  111.  
  112. //deposit $100 to Joe's account
  113. acct2.deposit(100);
  114.  
  115. //print Joe's new balance (use getBalance())
  116. System.out.println("Joe's balance is: " + acct2.getBalance());
  117.  
  118. //withdraw $50 from Sally's account
  119. acct1.withdraw(50);
  120.  
  121. //print Sally's new balance (use getBalance())
  122. System.out.println("Sally's balance is: " + acct1.getBalance());
  123.  
  124. //charge fees to both accounts
  125. acct1.chargeFee();
  126. acct2.chargeFee();
  127.  
  128. //change the name on Joe's account to Joseph
  129. acct2.changeName("Joseph");
  130.  
  131. //print summary for both accounts (use toString() implicitly)
  132. System.out.println ("Account Summary: \n" + acct1.toString() + "\n" + acct2.toString());
  133.  
  134.  
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement