Kendred

Bank Account

Mar 28th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. public class Project {
  2.  
  3. public static void main(String[] args) {
  4. /*
  5. * Kendred Thompson
  6. * 4th Period
  7. * CS 2
  8. * Bank Account
  9. */
  10.  
  11. BankAccount myMoney = new BankAccount(500, 9999);
  12. myMoney.withdraw(10, 9999);
  13. myMoney.withdraw(-5, 8888);
  14. myMoney.withdraw(90, 9999);
  15.  
  16. myMoney.deposit(10, 9999);
  17. myMoney.deposit(-5, 8888);
  18. myMoney.deposit(90, 9999);
  19.  
  20. System.out.println(myMoney.getBalance());
  21.  
  22. }
  23.  
  24. }
  25. class BankAccount{
  26. private double balance;
  27. private int pin;
  28.  
  29. public BankAccount(double startingBalance, int newPin) {
  30. balance = startingBalance;
  31. pin = newPin;
  32. }
  33.  
  34. public double getBalance() {
  35.  
  36. return balance;
  37. }
  38.  
  39. public void withdraw(double amount, int p) {
  40. if(p == pin) {
  41.  
  42. if(amount > 0) {
  43.  
  44. balance = balance - amount; //Ok to withdraw money
  45.  
  46. System.out.println("Money dispensed. New balance: $" + balance);
  47. }
  48. else {
  49. System.out.println("Error: Amount Must be greater than $0");
  50.  
  51. }
  52. }
  53. else {
  54. System.out.println("Error: PIN incorrect");
  55. }
  56.  
  57.  
  58. }
  59. public void deposit(double amount, int p) {
  60. if(p == pin) {
  61.  
  62. if(amount > 0) {
  63.  
  64. balance = balance + amount;
  65.  
  66. System.out.println("Money dispensed. New balance: $" + balance);
  67. }
  68. else {
  69. System.out.println("Error: Amount Must be greater than $0");
  70.  
  71. }
  72. }
  73. else {
  74. System.out.println("Error: PIN incorrect");
  75. }
  76.  
  77.  
  78. }
  79. }
Add Comment
Please, Sign In to add comment