Question

In: Computer Science

how to create BANKACCOUNT program using Arrays in JAVA.

how to create BANKACCOUNT program using Arrays in JAVA.

Solutions

Expert Solution

import java.util.HashMap;

import java.util.Scanner;

// main class

public class BankAccount {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

HashMap<Integer, Account> accountMap = new HashMap<Integer, Account>();

//Options

while(true) {

System.out.println("Enter the option for the operation you need:");

System.out.println("****************************************************");

System.out.println("[ Options: ne - New Account de - Delete Account ]");

System.out.println("[ dp - Deposit wi - Withdraw ]");

System.out.println("[ se - Select Account ex - Quit ]");

System.out.println("****************************************************");

System.out.print("> "); //indicator for user input

String choice = input.next();

System.out.println("Your choice: " + choice);

if(choice.equals("ne")) {

Integer newAccountNumber;

Double initialBalance;

Account newAccount;

// Array for account and balance

System.out.print("Insert account number: ");

newAccountNumber = input.nextInt(); //-- Input nr for array insertion

System.out.print("Enter initial balance: ");

initialBalance=input.nextDouble(); //-- Input nr for array insertion

newAccount = new Account(newAccountNumber, initialBalance);

accountMap.put(newAccountNumber, newAccount);

System.out.println("New Account " + newAccountNumber + " created with balance: " + initialBalance);

}

//select account

else if(choice.equals("se")) {

System.out.println("Enter number of account to be selected: ");

Integer accountToGetNumber = input.nextInt();

Account returnedAccount = accountMap.get(accountToGetNumber);

if (returnedAccount != null)

{

System.out.println("Account open. Current balance: " + returnedAccount.getBalance());

}

else

{

//user input for account nr from array

System.out.println("Account does not exist.");

}

}

//close account

else if(choice.equals("de"))

{

System.out.println("Enter number of account to be selected: ");

Integer accountToDeleteNumber = input.nextInt();

Account removedAccount = accountMap.remove(accountToDeleteNumber);

if (removedAccount != null)

{

System.out.println("Account " + removedAccount.getAccountNumber() + " has been closed with balance: " + removedAccount.getBalance());

}

else

{

//user input for account nr from array

System.out.println("Account does not exist.");

}

}

// deposit

else if(choice.equals("dp")) {

System.out.println("Enter number of account to deposit: ");

Integer accountToDeposit = input.nextInt();

System.out.print("Enter amount to deposit: ");

double amount = input.nextDouble();

if(amount <= 0){

System.out.println("You must deposit an amount greater than 0.");

} else {

accountMap.get(accountToDeposit).deposit(amount);

System.out.println("You have deposited " + (amount));

System.out.println("Current balance " + accountMap.get(accountToDeposit).getBalance());

}

}

// withdrawal

else if(choice.equals("wi")) {

System.out.println("Enter number of account to withdraw: ");

Integer accountToWithdraw = input.nextInt();

System.out.print("Enter amount to withdraw: ");

double amount = input.nextDouble();

if(amount <= 0) {

System.out.println("You must deposit an amount greater than 0.");

} else {

accountMap.get(accountToWithdraw).withdraw(amount);

System.out.println("You have deposited " + (amount));

System.out.println("Current balance " + accountMap.get(accountToWithdraw).getBalance());

}

}

//quit

else if(choice.equals("ex")) {

break;

} else {

System.out.println("Wrong option.");

} //end of if

} //end of loop

input.close();

} //end of main

} //end of class


Related Solutions

Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made. public class BankAccount { private int id; private String name private double balance; private int numTransactions; // ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName();...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Write a program to create a bank account and to process transactions. Call this class bankAccount...
Write a program to create a bank account and to process transactions. Call this class bankAccount A bank account can only be given an initial balance when it is instantiated. By default, a new bank account should have a balance of 0. A bank account should have a public get method, but no public set method. A bank account should have a process method with a double parameter to perform deposits and withdrawals. A negative parameter represents a withdrawal. It...
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
For this question we will be using arrays and classes in Java to compute the min,...
For this question we will be using arrays and classes in Java to compute the min, max, and average value of items for a given array of integers. Complete the following using the base template provided below: -Create methods for min, max, and average and call them from main to print out their values. -Add a method to determine the median (http://www.mathsisfun.com/median.html) and print that value. This method is currently not in the template, so you will need to add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT