Bank Program in Java
Below is my code(haven\'t finished, but it be run without error messages.)
My problem is in Case 1. For example, if I enter two customers:
Name: John Kangas Account Number: A123 Balance: 300
Name: Mary White Account Number: B321 Balance: 600
In the output -- For testing, I expected it to be like this:
***For Testing!
John Kangas
Balance: 300.0
****
***For Testing!
Mary White
Balance: 600.0
****
However, it actually came out like this:
***For Testing!
Mary White
Balance: 600.0
****
***For Testing!
Mary White
Balance: 600.0
****
It seems that the second information replace the first one. Could anyone help me figure it out??
Thank you so much!!
=========
Bank Class
=========
import java.util.ArrayList;
import java.util.Scanner;
public class Bank {
String routingNum;
static Customer customer;
static ArrayList customerList = new ArrayList();
private static int numOfCustomers = 0;
private static double bal;
public Bank(){
routingNum = \"000000\";
}
public static void newCustomer(String f, String l, String accNum, double bal){
Account acc = Customer.OpenAccount(accNum,bal);
customer = new Customer(f,l,acc);
customerList.add(customer);
numOfCustomers ++;
//For Testing
for (int i = 0; i < numOfCustomers; i++ ){
System.out.println(\"\ ***For Testing!\ \"+ customerList.get(i).getName());
System.out.println(\"Balance: \" + customerList.get(i).getAccount().getBalance());
System.out.println(\"****\");
}
}
public static double findCustomer(String f, String l, String accNum){
//For Testing
System.out.println(\"***For Testing!\ \"+ \"numOfCustomers: \" + numOfCustomers);
for (int i = 0; i < numOfCustomers; i++){
String name = customerList.get(i).getName();
int n = name.indexOf(\" \");
int L = name.length();
String first = name.substring(0, n); //In order to separate
String last = name.substring(n+1, L); //first and last name
String accN = customerList.get(i).getAccount().getAccountNum();
//For Testing
System.out.println(\"***For Testing!\ \" + \"acc num: \" + accN);
if ( f.equals(first) && l.equals(last) || accNum.equals(accN) ){
bal = customerList.get(i).getAccount().getBalance();
System.out.print(\"\ Balance: \" + bal +\" \ \");
}else{
System.out.println(\"No Found Account.\");
}
}
return bal;
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
boolean control = true;
while(control){
System.out.println(\"Hello! Welcome to New Century Bank!\");
System.out.println(\"Enter(\'1\'): Open an Account\");
System.out.println(\"Enter(\'2\'): Get your balance\");
System.out.println(\"Enter(\'3\'): Deposit\");
System.out.println(\"Enter(\'4\'): Withdraw\");
System.out.println(\"Enter(\'5\'): Close an Account\");
System.out.println(\"Enter(\'6\'): Exit\");
int user_choice = s.nextInt();
String input1, input2, input3;
double input4;
switch(user_choice){
case 1: System.out.println(\"Your name(first last): \");
input1 = s.next();
input2 = s.next();
System.out.println(\"Enter an account number: \");
input3 = s.next();
System.ou.
Bank Program in JavaBelow is my code(havent finished, but it be .pdf
1. Bank Program in Java
Below is my code(haven't finished, but it be run without error messages.)
My problem is in Case 1. For example, if I enter two customers:
Name: John Kangas Account Number: A123 Balance: 300
Name: Mary White Account Number: B321 Balance: 600
In the output -- For testing, I expected it to be like this:
***For Testing!
John Kangas
Balance: 300.0
****
***For Testing!
Mary White
Balance: 600.0
****
However, it actually came out like this:
***For Testing!
Mary White
Balance: 600.0
****
***For Testing!
Mary White
Balance: 600.0
****
It seems that the second information replace the first one. Could anyone help me figure it out??
Thank you so much!!
=========
Bank Class
=========
import java.util.ArrayList;
import java.util.Scanner;
public class Bank {
String routingNum;
static Customer customer;
static ArrayList customerList = new ArrayList();
3. for (int i = 0; i < numOfCustomers; i++){
String name = customerList.get(i).getName();
int n = name.indexOf(" ");
int L = name.length();
String first = name.substring(0, n); //In order to separate
String last = name.substring(n+1, L); //first and last name
String accN = customerList.get(i).getAccount().getAccountNum();
//For Testing
System.out.println("***For Testing! " + "acc num: " + accN);
if ( f.equals(first) && l.equals(last) || accNum.equals(accN) ){
bal = customerList.get(i).getAccount().getBalance();
System.out.print(" Balance: " + bal +" ");
}else{
System.out.println("No Found Account.");
}
}
return bal;
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
boolean control = true;
while(control){
System.out.println("Hello! Welcome to New Century Bank!");
System.out.println("Enter('1'): Open an Account");
4. System.out.println("Enter('2'): Get your balance");
System.out.println("Enter('3'): Deposit");
System.out.println("Enter('4'): Withdraw");
System.out.println("Enter('5'): Close an Account");
System.out.println("Enter('6'): Exit");
int user_choice = s.nextInt();
String input1, input2, input3;
double input4;
switch(user_choice){
case 1: System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
System.out.println("Enter an account number: ");
input3 = s.next();
System.out.println("Enter an opening balance: ");
input4 = s.nextDouble();
newCustomer(input1, input2, input3, input4);
break;
case 2: System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
System.out.println("Enter an account number: ");
input3 = s.next();
bal = findCustomer(input1, input2, input3);
System.out.println(" Your current balance is $" + bal + ". ");
break;
5. case 3: System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
System.out.println("Enter an account number: ");
input3 = s.next();
System.out.println("Enter a deposit amount: ");
input4 = s.nextDouble();
break;
case 4: System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
break;
case 5: System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
break;
case 6: control = false;
default: control = false;
}
}
System.out.println("Thank you for using New Century Bank!");
}
}
========
Customer
6. ========
public class Customer {
static Account account;
static String first, last;
public Customer(String f, String l, Account acc){
first = f;
last = l;
account = acc;
}
public String getName(){
return first + " " + last;
}
public Account getAccount(){
return account;
}
public static Account OpenAccount(String accNum, double bal){
account = new Account(accNum, bal);
return account;
}
public void CloseAccount(String f, String l, String accNum, double bal){
f = first;
l = last;
accNum = account.getAccountNum();
bal = account.getBalance();
}
7. }
======
Account
======
public class Account {
String accountNum;
double balance, minBalance, overDraftFee;
public Account(){
accountNum = "000000";
balance = 0.0;
minBalance = 100;
overDraftFee = 10;
}
public Account(String accNum, double bal){
accountNum = accNum;
balance = bal;
}
public String getAccountNum(){
return accountNum;
}
public double getBalance(){
return balance;
}
public double getOverDraftFee(){
return overDraftFee;
}
8. public double minBalance(){
return minBalance;
}
public void DepositFunds(Customer customer, double amount){
if (amount < 0){
System.out.println("Deposit amount should not be negative.");
}else{
double bal = customer.account.getBalance();
bal += amount;
}
}
public void WithdrawFunds(Customer customer, double amount){
if (amount < 0){
System.out.println("Withdraw amount should not be negative.");
}else{
double bal = customer.account.getBalance();
bal -= amount;
if (bal < 0){
System.out.println("***Your balance is less then withdraw amount; " +
"***Overdraft fee($10) is charged now.");
bal -= overDraftFee;
}
}
}
public void PrintAccountInfo(Customer customer){
System.out.println("**** Account Information *** "+
"Name: " + customer.getName() + " " +
"Current Balance: " + customer.account.getBalance() + " " +
"Account Number: " + customer.account.getAccountNum());
}
}
Solution
9. public class Account {
String accountNum;
double balance, minBalance, overDraftFee;
public Account() {
accountNum = "000000";
balance = 0.0;
minBalance = 100;
overDraftFee = 10;
}
public Account(String accNum, double bal) {
accountNum = accNum;
balance = bal;
}
public String getAccountNum() {
return accountNum;
}
public double getBalance() {
return balance;
}
public double getOverDraftFee() {
return overDraftFee;
}
public double minBalance() {
return minBalance;
}
public void DepositFunds(Customer customer, double amount) {
if (amount < 0) {
System.out.println("Deposit amount should not be negative.");
} else {
double bal = customer.account.getBalance();
bal += amount;
}
}
public void WithdrawFunds(Customer customer, double amount) {
if (amount < 0) {
10. System.out.println("Withdraw amount should not be negative.");
} else {
double bal = customer.account.getBalance();
bal -= amount;
if (bal < 0) {
System.out
.println("***Your balance is less then withdraw amount; "
+ "***Overdraft fee($10) is charged now.");
bal -= overDraftFee;
}
}
}
public void PrintAccountInfo(Customer customer) {
System.out.println("**** Account Information *** " + "Name: "
+ customer.getName() + " " + "Current Balance: "
+ customer.account.getBalance() + " " + "Account Number: "
+ customer.account.getAccountNum());
}
}
public class Customer {
Account account;
String first, last;
public Customer(String f, String l, Account acc) {
first = f;
last = l;
account = acc;
}
public String getName() {
return first + " " + last;
}
public Account getAccount() {
return account;
}
public Account OpenAccount(String accNum, double bal) {
account = new Account(accNum, bal);
return account;
11. }
public void CloseAccount(String f, String l, String accNum, double bal) {
f = first;
l = last;
accNum = account.getAccountNum();
bal = account.getBalance();
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Bank {
String routingNum;
static Customer customer;
static ArrayList customerList = new ArrayList();
private static int numOfCustomers = 0;
private static double bal;
public Bank() {
routingNum = "000000";
}
public static void newCustomer(String f, String l, String accNum, double bal) {
Account acc = new Account(accNum, bal);
System.out.println("acc==" + acc.getBalance());
customer = new Customer(f, l, acc);
customerList.add(customer);
numOfCustomers++;
// For Testing
for (int i = 0; i < numOfCustomers; i++) {
System.out.println(" ***For Testing! "
+ customerList.get(i).getName());
System.out.println("Balance: "
+ customerList.get(i).getAccount().getBalance());
System.out.println("****");
}
}
public static double findCustomer(String f, String l, String accNum) {
// For Testing
12. System.out.println("***For Testing! " + "numOfCustomers: "
+ numOfCustomers);
for (int i = 0; i < numOfCustomers; i++) {
String name = customerList.get(i).getName();
int n = name.indexOf(" ");
int L = name.length();
String first = name.substring(0, n); // In order to separate
String last = name.substring(n + 1, L); // first and last name
String accN = customerList.get(i).getAccount().getAccountNum();
// For Testing
System.out.println("***For Testing! " + "acc num: " + accN);
if (f.equals(first) && l.equals(last) || accNum.equals(accN)) {
bal = customerList.get(i).getAccount().getBalance();
System.out.print(" Balance: " + bal + " ");
} else {
System.out.println("No Found Account.");
}
}
return bal;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean control = true;
while (control) {
System.out.println("Hello! Welcome to New Century Bank!");
System.out.println("Enter('1'): Open an Account");
System.out.println("Enter('2'): Get your balance");
System.out.println("Enter('3'): Deposit");
System.out.println("Enter('4'): Withdraw");
System.out.println("Enter('5'): Close an Account");
System.out.println("Enter('6'): Exit");
int user_choice = s.nextInt();
String input1, input2, input3;
double input4;
switch (user_choice) {
case 1:
13. System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
System.out.println("Enter an account number: ");
input3 = s.next();
System.out.println("Enter an opening balance: ");
input4 = s.nextDouble();
newCustomer(input1, input2, input3, input4);
break;
case 2:
System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
System.out.println("Enter an account number: ");
input3 = s.next();
bal = findCustomer(input1, input2, input3);
System.out.println(" Your current balance is $" + bal + ". ");
break;
case 3:
System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
System.out.println("Enter an account number: ");
input3 = s.next();
System.out.println("Enter a deposit amount: ");
input4 = s.nextDouble();
break;
case 4:
System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
break;
case 5:
System.out.println("Your name(first last): ");
input1 = s.next();
input2 = s.next();
14. break;
case 6:
control = false;
default:
control = false;
}
}
System.out.println("Thank you for using New Century Bank!");
}
}
OUTPUT:
Hello! Welcome to New Century Bank!
Enter('1'): Open an Account
Enter('2'): Get your balance
Enter('3'): Deposit
Enter('4'): Withdraw
Enter('5'): Close an Account
Enter('6'): Exit
1
Your name(first last):
John Kangas
Enter an account number:
A123
Enter an opening balance:
300
acc==300.0
***For Testing!
John Kangas
Balance: 300.0
****
Hello! Welcome to New Century Bank!
Enter('1'): Open an Account
Enter('2'): Get your balance
Enter('3'): Deposit
Enter('4'): Withdraw
Enter('5'): Close an Account
15. Enter('6'): Exit
1
Your name(first last):
Mary White
Enter an account number:
B321
Enter an opening balance:
600
acc==600.0
***For Testing!
John Kangas
Balance: 300.0
****
***For Testing!
Mary White
Balance: 600.0
****
Hello! Welcome to New Century Bank!
Enter('1'): Open an Account
Enter('2'): Get your balance
Enter('3'): Deposit
Enter('4'): Withdraw
Enter('5'): Close an Account
Enter('6'): Exit
6
Thank you for using New Century Bank!