Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class AccountTest
- {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- char moreAccs; //used to stop do-while loop
- int i = 0; //used to increase Array lenght
- DecimalFormat df = new DecimalFormat("#.00"); //create object to display 2 decimals
- System.out.println("Welcome to the Bank of Programmers");
- System.out.println("Please create a new account\n");
- do
- {
- Account[] acc = new Account[i + 1]; //create an Array of objects
- String name;
- double amount;
- char keep = 'y'; // used to end while loop.
- System.out.print("Enter your name: ");
- name = input.nextLine();
- System.out.print("Initial deposit: $");
- amount = input.nextDouble();
- acc[i] = new Account(name, amount); //create instance of the object
- System.out.println();
- System.out.println(acc[i].toString()); //Displays account info
- System.out.println("What would you like to do next?");
- while(keep == 'y')
- {
- int answer;
- System.out.print("Type 1 to deposit or 2 to withdraw money: ");
- answer = input.nextInt();
- //user Error prevention
- while(answer !=2 && answer !=1)
- {
- System.out.print("ERROR: Type 1 to deposit or 2 to withdraw money: ");
- answer = input.nextInt();
- }
- switch(answer) //check user answer
- {
- case 1:
- System.out.print(" Enter deposit amount: $");
- amount = input.nextDouble();
- acc[i].deposit(amount); //deposits money to account
- System.out.println( " Account balance = $" + df.format( acc[i].inquiry() ) ); //display Account balance
- System.out.println();
- break;
- case 2:
- System.out.print(" Enter withdrawal ammount: $");
- amount = input.nextDouble();
- acc[i].withDraw(amount); //withdraws money from account
- System.out.println( " Account balance = $" + df.format( acc[i].inquiry() ) ); //display Account balance
- System.out.println();
- break;
- default:
- System.out.print("Something is wrong");
- break;
- }
- System.out.print("Do you want to deposit or withdraw more money? (y or n): ");
- keep = input.next().charAt(0); //only input first char value
- while(keep !='y' && keep !='n') //user Error prevention
- {
- System.out.print(" ERROR: Enter 'y' for yes or 'n' for no: ");
- keep = input.next().charAt(0);
- }
- }
- System.out.print("Do you want to create another account? (y or n): ");
- moreAccs = input.next().charAt(0);
- while(moreAccs !='y' && moreAccs !='n') //user Error prevention
- {
- System.out.print(" ERROR: Enter 'y' for yes or 'n' for no: ");
- moreAccs = input.next().charAt(0);
- }
- input.nextLine(); //consumes \n bug fix.
- System.out.println();
- System.out.println();
- i++; //increase by one so more accounts can be created.
- }while(moreAccs == 'y');
- System.out.println("The bank has " + Account.numOfAccs() + " accounts."); //display accounts created
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement