Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Menu {
- Scanner in = new Scanner(System.in);
- Bank bank = new Bank();
- boolean exit;
- public static void main(String[] args) {
- Menu menu = new Menu();
- menu.runMenu();
- }
- public void runMenu(){
- System.out.println("********************************");
- System.out.println("* *");
- System.out.println("* Welcome to our Bank *");
- System.out.println("* *");
- System.out.println("********************************");
- while(!exit){
- int startAccounts ;
- while(true) {
- System.out.print("How many Accounts you want to create?: ");
- startAccounts = in.nextInt();
- if(startAccounts >0){
- break;
- }else{
- System.out.println("You should enter a positive number.");
- }
- }
- for (int i=0; i<startAccounts;i++){
- System.out.println("Enter the information for account no."+(i+1)+".");
- CreateAnAccount();
- }
- System.out.println("Please make a selection:");
- System.out.println("1) Create a new Account.");
- System.out.println("2) Make a Deposit.");
- System.out.println("3) Make a Withdrawal.");
- System.out.println("4) List account balance.");
- System.out.println("0) Exit.");
- int choice = -1;
- do {
- System.out.print("Enter your choice:");
- try {
- choice = in.nextInt();
- }catch (InputMismatchException e){
- System.out.println("Invalid selection, Numbers only.");
- in.next();
- continue;
- }
- if(choice <0 || choice >4) System.out.println("Choice out of range, Choose again.");
- }while (choice <0 || choice >4);
- switch (choice){
- case 0:
- System.out.println("Thank you for using our Bank.");
- System.exit(0);
- break;
- case 1:
- CreateAnAccount();
- break;
- case 2:
- MakeADeposit();
- break;
- case 3:
- MakeAWithdrawal();
- break;
- case 4:
- ListBalances();
- break;
- default:
- System.out.println("Unknown Error.");
- }
- }
- }
- private void CreateAnAccount() {
- String firstname,lastname,pass,accountType = null;
- double initialDeposit = 0;
- boolean valid = false;
- while (!valid){
- System.out.print("Please enter an account type (checking / savings):");
- accountType = in.next();
- in.nextLine();
- if(accountType.equalsIgnoreCase("checking") || accountType.equalsIgnoreCase("savings")){
- valid = true;
- }else{
- System.out.println("Invalid account type selected, enter checking or savings.");
- }
- }
- System.out.print("Enter your first name : ");
- firstname = in.next();
- in.nextLine();
- System.out.print("Enter your last name : ");
- lastname = in.next();
- in.nextLine();
- System.out.print("Enter your password : ");
- pass = in.next();
- in.nextLine();
- valid = false;
- while(!valid){
- System.out.print("Please enter an initial deposit : ");
- try {
- initialDeposit = in.nextDouble();
- }catch (InputMismatchException e){
- System.out.println("Deposit must be a number.");
- in.next();
- }
- if (accountType.equalsIgnoreCase("checking")){
- if(initialDeposit < 100){
- System.out.println("checking account require a minimum of of $100 to open.");
- }else{
- valid = true;
- }
- }else if (accountType.equalsIgnoreCase("savings")){
- if(initialDeposit < 50){
- System.out.println("checking account require a minimum of of $50 to open.");
- }else{
- valid = true;
- }
- }
- }
- Account account;
- if(accountType.equalsIgnoreCase("checking")){
- account = new Checking(initialDeposit);
- }else{
- account = new Savings(initialDeposit);
- }
- Customer customer = new Customer(firstname,lastname,pass,account);
- bank.addCustomer(customer);
- }
- private void MakeADeposit() {
- int account = selectAccount();
- if(account >=0) {
- System.out.print("How much would you like to deposit? : ");
- double amount = 0;
- amount = in.nextDouble();
- bank.getCustomer(account).getAccount().deposit(amount);
- }
- }
- private void MakeAWithdrawal() {
- int account = selectAccount();
- if(account >=0) {
- System.out.print("How much would you like to withdraw? : ");
- double amount = 0;
- amount = in.nextDouble();
- bank.getCustomer(account).getAccount().withdraw(amount);
- }
- }
- private void ListBalances() {
- int account = selectAccount();
- if(account >=0) {
- System.out.println(bank.getCustomer(account).getAccount());
- }
- }
- private int selectAccount() {
- ArrayList<Customer> customers = bank.getCustomers();
- if(customers.size() <= 0){
- System.out.println("No customers at the bank.");
- return -1;
- }
- System.out.println("Select an account: ");
- for (int i=0; i<customers.size(); i++){
- System.out.println((i+1) +") " +customers.get(i).basicInfo());
- }
- int account =0;
- System.out.print("Please enter your selection: ");
- account = in.nextInt()-1;
- if(account >= customers.size() || account < 0){
- System.out.println("Invalid Customer.");
- return -1;
- }
- return account;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement