Guest User

Untitled

a guest
Apr 25th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Prog8 {
  4. public static void main(String[] args) {
  5.  
  6. //variables
  7. char operation;
  8. Scanner reader = new Scanner (System.in);
  9. double amt = 0.0;
  10. Account acc = new Account();
  11.  
  12. while (reader.hasNext()) {
  13. operation = reader.next(".").charAt(0);
  14.  
  15. if (operation == 'd') {
  16. amt = reader.nextDouble();
  17. acc.deposit(amt);
  18. }
  19. else if (operation == 'w') {
  20. amt = reader.nextDouble();
  21. acc.withdraw(amt);
  22. }
  23. else if (operation == 'i') {
  24. acc.addInterest();
  25. }
  26. else if (operation == 'p') {
  27. acc.getBalance();
  28. }
  29. else {
  30. System.out.println("** Invalid Operation **");
  31. }
  32. }
  33. }
  34. }
  35.  
  36. public class Account {
  37. //variables
  38. private int accountID;
  39. private double accountBalance;
  40.  
  41. public Account() {
  42. accountID = (int)(Math.random() * (9000)) + 1000;
  43. accountBalance = 0.0;
  44. }
  45.  
  46. //withdraw operation
  47. public void withdraw(double amount) {
  48. if(amount > accountBalance)
  49. System.out.printf("Error: cannot withdraw $%.2fn",amount);
  50. else {
  51. accountBalance -= amount;
  52. System.out.printf("$%.2f withdrawn from account %dn",amount,accountID);
  53. }
  54. }
  55.  
  56. //deposit operation
  57. public void deposit(double amount) {
  58. accountBalance += amount;
  59. System.out.printf("$%.2f deposited into account %dn",amount,accountID);
  60. }
  61.  
  62. //add interest
  63. public void addInterest() {
  64. double interest = accountBalance * 0.005;
  65. accountBalance += interest;
  66. System.out.printf("$%.2f interest added to accountn",interest,accountID);
  67. }
  68.  
  69. //account balance
  70. public void getBalance() {
  71. System.out.printf("Current balance for account %d:
  72. $%.2fn",accountID,accountBalance);
  73. }
  74. }
Add Comment
Please, Sign In to add comment