Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. public class AccountTest
  5. {
  6. public static void main(String[] args)
  7. {
  8. Scanner input = new Scanner(System.in);
  9.  
  10. char moreAccs; //used to stop do-while loop
  11. int i = 0; //used to increase Array lenght
  12.  
  13. DecimalFormat df = new DecimalFormat("#.00"); //create object to display 2 decimals
  14.  
  15. System.out.println("Welcome to the Bank of Programmers");
  16. System.out.println("Please create a new account\n");
  17.  
  18. do
  19. {
  20. Account[] acc = new Account[i + 1]; //create an Array of objects
  21.  
  22. String name;
  23. double amount;
  24. char keep = 'y'; // used to end while loop.
  25.  
  26. System.out.print("Enter your name: ");
  27. name = input.nextLine();
  28. System.out.print("Initial deposit: $");
  29. amount = input.nextDouble();
  30. acc[i] = new Account(name, amount); //create instance of the object
  31. System.out.println();
  32.  
  33. System.out.println(acc[i].toString()); //Displays account info
  34.  
  35. System.out.println("What would you like to do next?");
  36. while(keep == 'y')
  37. {
  38. int answer;
  39.  
  40.  
  41. System.out.print("Type 1 to deposit or 2 to withdraw money: ");
  42. answer = input.nextInt();
  43.  
  44. //user Error prevention
  45. while(answer !=2 && answer !=1)
  46. {
  47. System.out.print("ERROR: Type 1 to deposit or 2 to withdraw money: ");
  48. answer = input.nextInt();
  49. }
  50.  
  51. switch(answer) //check user answer
  52. {
  53. case 1:
  54. System.out.print(" Enter deposit amount: $");
  55. amount = input.nextDouble();
  56. acc[i].deposit(amount); //deposits money to account
  57. System.out.println( " Account balance = $" + df.format( acc[i].inquiry() ) ); //display Account balance
  58. System.out.println();
  59. break;
  60. case 2:
  61. System.out.print(" Enter withdrawal ammount: $");
  62. amount = input.nextDouble();
  63. acc[i].withDraw(amount); //withdraws money from account
  64. System.out.println( " Account balance = $" + df.format( acc[i].inquiry() ) ); //display Account balance
  65. System.out.println();
  66. break;
  67. default:
  68. System.out.print("Something is wrong");
  69. break;
  70. }
  71.  
  72. System.out.print("Do you want to deposit or withdraw more money? (y or n): ");
  73. keep = input.next().charAt(0); //only input first char value
  74.  
  75. while(keep !='y' && keep !='n') //user Error prevention
  76. {
  77. System.out.print(" ERROR: Enter 'y' for yes or 'n' for no: ");
  78. keep = input.next().charAt(0);
  79. }
  80.  
  81. }
  82.  
  83. System.out.print("Do you want to create another account? (y or n): ");
  84. moreAccs = input.next().charAt(0);
  85.  
  86. while(moreAccs !='y' && moreAccs !='n') //user Error prevention
  87. {
  88. System.out.print(" ERROR: Enter 'y' for yes or 'n' for no: ");
  89. moreAccs = input.next().charAt(0);
  90. }
  91.  
  92. input.nextLine(); //consumes \n bug fix.
  93. System.out.println();
  94. System.out.println();
  95. i++; //increase by one so more accounts can be created.
  96.  
  97. }while(moreAccs == 'y');
  98.  
  99. System.out.println("The bank has " + Account.numOfAccs() + " accounts."); //display accounts created
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement