I have a bank project in java i need help to make it work right please!! The ban
ID: 3911564 • Letter: I
Question
I have a bank project in java i need help to make it work right please!!
The bank.java is the program and it have the list from 1 to 10.
When i do creat an account to go to the proccess it did secussfuly but when you make a deposite to the same account you already creat it told you their is no account not found?!!!!! I don't know what is the problem can you help in that please?
Here is my code:
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
//import com.bank.account.Account;
//import com.bank.account.CheckingAccount;
public class Bank {
// arrayList to store the information for the bank system
static ArrayList<Account> accounts = new ArrayList<Account>();
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// creating a file for the project and make the arrayList save the information in this file
File file = new File ("AccountData.ser");
if (file.exists()){
try{
@SuppressWarnings("resource")
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
accounts = (ArrayList<Account>)in.readObject();
}
catch (IOException ioException){
ioException.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// we use Scanner to make the user enter his information
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
// Start the menu to make the user chose from it
boolean keepLooking = true;
while(keepLooking){
int choice;
{
System.out.println("");
System.out.println(" Bank Menu");
System.out.println(" =====================");
System.out.println(" 1. Create Checking Account");
System.out.println(" 2. Create Gold Account");
System.out.println(" 3. Create Regular Account");
System.out.println(" 4. Deposit");
System.out.println(" 5. Withdrawl");
System.out.println(" 6. Display Account Info");
System.out.println(" 7. Remove an Account");
System.out.println(" 8. Apply end of month <Interest/Fees>");
System.out.println(" 9. Display Bank Statistics");
System.out.println(" 10. Exit");
System.out.println("Enter your choice (1-10): " );
choice = input.nextInt();
// we do switch to make each number do something
// or we can use if statement will work too
switch(choice)
{
case 1:
//Creating an account to the customer
String name;
String customerID;
long accountNumber;
// creating a CheckingAccount for the customer
// we use the user input here to save the information
System.out.println(" Please input the customer name: ");
name = input.next();
System.out.println(" Please input the customer ID : ");
customerID = input.next();
System.out.println(" Please input the account number: ");
accountNumber = input.nextLong();
// Create a Customer in order to Create an account
Customer sam = new Customer(customerID, name,"419 W.Lincolnn Rd","615-713-6049");
//Create an account for the user
Calendar dateOpened = Calendar.getInstance();
Account myCheckingAccount = new CheckingAccount(120, accountNumber, dateOpened, sam);
System.out.print(myCheckingAccount);
accounts.add(myCheckingAccount); // adding the checking account to the ArryList
System.out.println(" Your Checking Account was created successfully!");
keepLooking = true;continue;
case 2:
//create a Gold Account
System.out.print(" Please input the customer name: ");
name = input.next();
System.out.print(" Please input the customer ID : ");
customerID = input.next();
System.out.print(" Please input the account number: ");
accountNumber = input.nextLong();
// Create a Customer in order to Create an account
Customer cal = new Customer(customerID,name,"Carmel","615-713-6049");
// Create the Gold Account for the Customer
Calendar dateOpened1 = Calendar.getInstance();
Account myGoldAccount = new GoldAccount(1500, accountNumber, dateOpened1, cal);
System.out.print(myGoldAccount);
accounts.add(myGoldAccount);
System.out.print(" Your Gold Account was created successfully!");
keepLooking = true;continue;
case 3:
//create a Regular Account
System.out.print(" Please input the customer name: ");
name = input.next();
System.out.print(" Please input the customer ID : ");
customerID = input.next();
System.out.print(" Please input the account number: ");
accountNumber = input.nextLong();
// Create a Customer in order to Create an account
Customer andrew = new Customer(customerID,name,"Indiana","615-713-6049");
// Create the Gold Account for the Customer
Calendar dateOpened11 = Calendar.getInstance();
Account myRegularAccount = new RegularAccount(6000, accountNumber, dateOpened11, andrew);
System.out.print(myRegularAccount);
accounts.add(myRegularAccount);
System.out.print(" Your Regular Account was created successfully!");
keepLooking = true;continue;
case 4:
// make the system search for the account in order to let the user make a deposit
int accNum;
double amount;
System.out.print("Please input the account number: ");
accNum = input.nextInt();
Account ac = find(accNum);
if(ac == null)
System.out.println(" Account number not found!");
else
{
System.out.print("Please input the amount: ");
amount = input.nextDouble();
ac.makeDeposit(amount);
System.out.println(" Account updated successfully! ");
}
keepLooking = true;continue;
case 5:
// Making the system search for the account in order to let the user make a withdraw
System.out.print("Please input the account number: ");
accNum = input.nextInt();
Account acc = find(accNum);
if(acc == null)
System.out.println(" Account number not found!");
else
{
System.out.print("Please input the amount: ");
amount = input.nextDouble();
acc.makeWithdrawal(amount);
System.out.println(" Account updated successfully! ");
}
keepLooking = true;continue;
case 6:
// Here the Information for the account well display
for (Account c: accounts){
System.out.print(c);
}
keepLooking = true;continue;
case 7:
// here we make the user remove the account from the the system and from the arraylist
Account accountt = null;
//Remove an Account
System.out.println("Please enter account number: ");
accountNumber = input.nextLong();
accounts.remove(accountt);
System.out.println("Account successfully removed!");
keepLooking = true;continue;
case 8:
// here we make the user to apply each month transaction
double principal = 0;
double rate = 0;
double time = 0;
double compoundInterest = 0;
System.out.print("Enter the Principal amount : ");
principal = input.nextDouble();
System.out.print("Enter the Rate : ");
rate = input.nextDouble();
System.out.print("Enter the Time : ");
time = input.nextDouble();
compoundInterest = principal * Math.pow((1 + rate/100),time);
System.out.println("");
System.out.println("The Compound Interest is : "
+ compoundInterest);
keepLooking = true;continue;
case 9:
// display the statistic for the bank system
for (Account s: accounts){
accounts.add(s);
}
keepLooking = true;continue;
case 10:
//Exit from the system
System.exit(10);
break;
// here if the user chose a wrong number
default:
System.out.println("Invalid Choice");
}
}
//Store the ArrayList into a file named EmployeeData.ser
//Arrays and ArrayLists are objects
try{
@SuppressWarnings("resource")
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("AccountData.ser"));
out.writeObject(accounts);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
public static Account find(int accountNumber)
{
for (Account a : accounts)
{
if (a.getAccountNumber() == accountNumber) // Found a match
return a;
}
return null; // No match in the entire ArrayList
}
}
_____________________
/*
* Account.java
*
* This is a super class for other accounts
* to inherit. A generic account cannot be
* created and is therefore abstract. Also, if we want
* to force future functionality to be implemented
* by different account types, abstract will allow for this.
*/
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public abstract class Account{
// All accounts will have the following attributes
// Declaring these protected allows for subclass access
protected long accountNumber; // used to show the account number for the customer
protected double balance; // used to show the balance in the account
protected Calendar dateOpened; // used to show the date opened
protected Customer customer; // Used to tie the account to a customer
/**
*
* Account constructor requiring five
* attributes to create. Subclasses will be forced to call
* this constructor and set these required values.
*/
public Account(long accountNumber, double balance, Calendar dateOpened, Customer customer){
this.accountNumber = accountNumber;
this.balance = balance;
this.dateOpened = dateOpened;
this.customer = customer;
}
/**
*
* makeDeposit is used to add funds to an account
* Note this method assumes valid data.
*/
public void makeDeposit(double depositAmount) {
balance += depositAmount;
}
/**
*
* makeWithdrawal is used to withdraw funds from an account
* Note this method assumes valid data.
* Assumes no further action if there is an overdraft
*/
public void makeWithdrawal(double withdrawalAmount) {
balance -= withdrawalAmount;
}
// these methods are abstract because we want to implement them in the supclass
public abstract void deducteFee(double fee);
public abstract void calculateIntrest();
public long getAccountNumber(){
return accountNumber;
}
public void setAccountNumber(long accountNumber){
this.accountNumber = accountNumber;
}
public double getBalance(){
return balance;
}
public void setBalance(double balance){
this.balance = balance;
}
public Calendar getDateOpened(){
return dateOpened;
}
public void setDateOpened(Calendar dateOpened){
this.dateOpened = dateOpened;
}
public Customer getCustomer(){
return customer;
}
public void setCustomer(Customer customer){
this.customer = customer;
}
/**
*
* String representation of this object
*/
public String toString() {
String output = "";
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
output += " Account Number: " + this.accountNumber;
output += " Account Balance: " + this.balance;
output += " Account Date open: " + dateFormat.format(dateOpened.getTime());
output += " Customer Account: " + this.customer;
return output;
}
}
_______________
/*
* CheckingAccount.java
* A specialized account representing a checking
* account
*
*/
import java.util.Calendar;
public class CheckingAccount extends Account {
private int transactionFee = 3;
/**
*
* Checking account constructor requiring five
* attributes to create
*
*/
public CheckingAccount(long accountNumber, double balance, Calendar dateOpened,Customer customer){
// Call superclass constructor
super(accountNumber, balance, dateOpened, customer);
}
// CheckingAccount have interest free in the first two month.
// it charge 3$ for every transaction(deposit/withdrawal)
public void deducteFee(double fee){
fee = balance - transactionFee;
}
public void calculateIntrest(){
balance = balance + balance * transactionFee;
}
/**
*
* String representation of this object
*
*/
public String toString(){
String output = "";
return output;
}
}
_________________
/*
* Customer.java
*
* This is a super class for other customers
* to inherit. A generic customer cannot be
* created and is therefore abstract. Also, if we want
* to force future functionality to be implemented
* by different customer types, abstract will allow for this.
*/
public class Customer {
// All customers will have the following attributes
// Declaring these protected allows for subclass access
protected String customerID;
protected String name;
protected String address;
protected String phoneNumber;
public Customer(){
this.customerID = "";
this.name = "";
this.address = "";
this.phoneNumber = "";
}
public Customer (String customerID,String name,String address,String phoneNumber){
this.customerID = customerID;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public void setCustomerID(String customerID){
this.customerID = customerID;
}
public String getCustomerID(){
return customerID;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void setPhoneNumber(String phoneNumber){
this.phoneNumber = phoneNumber;
}
public String getPhoneNumber(){
return phoneNumber;
}
//String representation of this object
public String toString(){
String output = "";
output += " Customer ID: " + this.customerID;
output += " Customer Name: " + this.name;
output += " Customer Address: " + this.address;
output += " Customer Phone Number: " + this.phoneNumber;
return output;
}
}
______________________
/*
* GoldAccount.java
* A specialized account representing a gold
* account
*
*/
import java.util.Calendar;
public class GoldAccount extends Account {
// GoldAccount will have the following attributes
// Declaring these private secures the attributes
private double fixedRate;
/**
*
* gold account constructor requiring five
* attributes to create
*
*/
public GoldAccount(long accountNumber, double balance, Calendar dateOpened, Customer customer) {
// Call superclass constructor
super(accountNumber, balance, dateOpened, customer);
}
//GoldAccount have to deductFee 10$
public void deducteFee(double fee) {
fee = balance = balance -10;
}
//GoldAccount have to calculateIntrest
public void calculateIntrest() {
balance = (balance + fixedRate )/ 100;
}
/**
*
* String representation of this object
*
*/
public String toString(){
String output = "";
return output;
}
}
_________________
/*
* RegularAccount.java
* A specialized account representing a Regular
* account
*
*/
import java.util.Calendar;
public class RegularAccount extends Account {
// RegulareAccount will have the following attributes
// Declaring these private secures the attributes
private double fixedRate;
/**
*
* regular account constructor requiring five
* attributes to create
*
*/
public RegularAccount(long accountNumber, double balance, Calendar dateOpened, Customer customer) {
// Call superclass constructor
super(accountNumber, balance, dateOpened, customer);
}
//RegularAccount have to deductFee 10$
public void deducteFee(double fee) {
fee = balance = balance -10;
}
//RegularAccount have to calculateIntrest
public void calculateIntrest() {
balance = (balance + fixedRate )/ 100;
}
/**
*
* String representation of this object
*
*/
public String toString(){
String output = "";
return output;
}
}
Explanation / Answer
I have made changes in two files: Bank.java and CheckingAccount.java.
Below are the mistakes I found and I have pasted the code for the above modified files below the mistakes. Hope this solves your problem. (I have only solved problems related to your question and some problems I found by looking at the code.. I haven't tested every possible scenario, you have to do it yourself)
1. This line:
Account myCheckingAccount = new CheckingAccount(120, accountNumber, dateOpened, sam);
should be changed to:
Account myCheckingAccount = new CheckingAccount(accountNumber, 120, dateOpened, sam);
Since the constructor for CheckingAccount takes parameters in the following order:
accountnumber, balance, dateOpened,customer
and your mistake was to put balance(120) before account number in the parameters.
You have made similar mistakes in case 2 and case 3:
Make the following changes:
Change:
Account myGoldAccount = new GoldAccount(1500, accountNumber, dateOpened1, cal);
To:
Account myGoldAccount = new GoldAccount(accountNumber, 1500, dateOpened1, cal);
and
Change:
Account myRegularAccount = new RegularAccount(6000, accountNumber, dateOpened11, andrew);
to:
Account myRegularAccount = new RegularAccount(accountNumber,6000, dateOpened11, andrew);
2. In case 4, you defined accNum as integer but it should be defined as long as you have used long to store it in Account class and everywhere else.
3. Similarly, in case 4 and 5, the line:
accNum = input.nextInt();
should be changed to:
accNum = input.nextLong();
due to changes in second point.
3. Your find(int accountNumber) function should be changed to find(long accountNumber).
4. Your statement in case 1:
System.out.print(myCheckingAccount);
does not print anything after adding account. This is because of your declaration of toString() function in CheckingAccount.java.
Change it to this:
public String toString(){
String output = super.toString();
return output;
}
Here is the updated code for Bank.java:
//Bank.java
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
//import com.bank.account.Account;
//import com.bank.account.CheckingAccount;
public class Bank {
// arrayList to store the information for the bank system
static ArrayList<Account> accounts = new ArrayList<Account>();
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// creating a file for the project and make the arrayList save the information in this file
File file = new File ("AccountData.ser");
if (file.exists()){
try{
@SuppressWarnings("resource")
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
accounts = (ArrayList<Account>)in.readObject();
}
catch (IOException ioException){
ioException.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// we use Scanner to make the user enter his information
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
// Start the menu to make the user chose from it
boolean keepLooking = true;
while(keepLooking){
int choice;
{
System.out.println("");
System.out.println(" Bank Menu");
System.out.println(" =====================");
System.out.println(" 1. Create Checking Account");
System.out.println(" 2. Create Gold Account");
System.out.println(" 3. Create Regular Account");
System.out.println(" 4. Deposit");
System.out.println(" 5. Withdrawl");
System.out.println(" 6. Display Account Info");
System.out.println(" 7. Remove an Account");
System.out.println(" 8. Apply end of month <Interest/Fees>");
System.out.println(" 9. Display Bank Statistics");
System.out.println(" 10. Exit");
System.out.println("Enter your choice (1-10): " );
choice = input.nextInt();
// we do switch to make each number do something
// or we can use if statement will work too
switch(choice)
{
case 1:
//Creating an account to the customer
String name;
String customerID;
long accountNumber;
// creating a CheckingAccount for the customer
// we use the user input here to save the information
System.out.println(" Please input the customer name: ");
name = input.next();
System.out.println(" Please input the customer ID : ");
customerID = input.next();
System.out.println(" Please input the account number: ");
accountNumber = input.nextLong();
// Create a Customer in order to Create an account
Customer sam = new Customer(customerID, name,"419 W.Lincolnn Rd","615-713-6049");
//Create an account for the user
Calendar dateOpened = Calendar.getInstance();
Account myCheckingAccount = new CheckingAccount(accountNumber, 120, dateOpened, sam);
System.out.print(myCheckingAccount);
accounts.add(myCheckingAccount); // adding the checking account to the ArryList
System.out.println(" Your Checking Account was created successfully!");
keepLooking = true;continue;
case 2:
//create a Gold Account
System.out.print(" Please input the customer name: ");
name = input.next();
System.out.print(" Please input the customer ID : ");
customerID = input.next();
System.out.print(" Please input the account number: ");
accountNumber = input.nextLong();
// Create a Customer in order to Create an account
Customer cal = new Customer(customerID,name,"Carmel","615-713-6049");
// Create the Gold Account for the Customer
Calendar dateOpened1 = Calendar.getInstance();
Account myGoldAccount = new GoldAccount(1500, accountNumber, dateOpened1, cal);
System.out.print(myGoldAccount);
accounts.add(myGoldAccount);
System.out.print(" Your Gold Account was created successfully!");
keepLooking = true;continue;
case 3:
//create a Regular Account
System.out.print(" Please input the customer name: ");
name = input.next();
System.out.print(" Please input the customer ID : ");
customerID = input.next();
System.out.print(" Please input the account number: ");
accountNumber = input.nextLong();
// Create a Customer in order to Create an account
Customer andrew = new Customer(customerID,name,"Indiana","615-713-6049");
// Create the Gold Account for the Customer
Calendar dateOpened11 = Calendar.getInstance();
Account myRegularAccount = new RegularAccount(6000, accountNumber, dateOpened11, andrew);
System.out.print(myRegularAccount);
accounts.add(myRegularAccount);
System.out.print(" Your Regular Account was created successfully!");
keepLooking = true;continue;
case 4:
// make the system search for the account in order to let the user make a deposit
Long accNum;
double amount;
System.out.print("Please input the account number: ");
accNum = input.nextLong();
Account ac = find(accNum);
if(ac == null)
System.out.println(" Account number not found!");
else
{
System.out.print("Please input the amount: ");
amount = input.nextDouble();
ac.makeDeposit(amount);
System.out.println(" Account updated successfully! ");
}
keepLooking = true;continue;
case 5:
// Making the system search for the account in order to let the user make a withdraw
System.out.print("Please input the account number: ");
accNum = input.nextLong();
Account acc = find(accNum);
if(acc == null)
System.out.println(" Account number not found!");
else
{
System.out.print("Please input the amount: ");
amount = input.nextDouble();
acc.makeWithdrawal(amount);
System.out.println(" Account updated successfully! ");
}
keepLooking = true;continue;
case 6:
// Here the Information for the account well display
for (Account c: accounts){
System.out.print(c);
}
keepLooking = true;continue;
case 7:
// here we make the user remove the account from the the system and from the arraylist
Account accountt = null;
//Remove an Account
System.out.println("Please enter account number: ");
accountNumber = input.nextLong();
accounts.remove(accountt);
System.out.println("Account successfully removed!");
keepLooking = true;continue;
case 8:
// here we make the user to apply each month transaction
double principal = 0;
double rate = 0;
double time = 0;
double compoundInterest = 0;
System.out.print("Enter the Principal amount : ");
principal = input.nextDouble();
System.out.print("Enter the Rate : ");
rate = input.nextDouble();
System.out.print("Enter the Time : ");
time = input.nextDouble();
compoundInterest = principal * Math.pow((1 + rate/100),time);
System.out.println("");
System.out.println("The Compound Interest is : "
+ compoundInterest);
keepLooking = true;continue;
case 9:
// display the statistic for the bank system
for (Account s: accounts){
accounts.add(s);
}
keepLooking = true;continue;
case 10:
//Exit from the system
System.exit(10);
break;
// here if the user chose a wrong number
default:
System.out.println("Invalid Choice");
}
}
//Store the ArrayList into a file named EmployeeData.ser
//Arrays and ArrayLists are objects
try{
@SuppressWarnings("resource")
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("AccountData.ser"));
out.writeObject(accounts);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
public static Account find(Long accountNumber)
{
for (Account a : accounts)
{
if (a.getAccountNumber() == accountNumber) // Found a match
return a;
}
return null; // No match in the entire ArrayList
}
}
Code for CheckingAccount.java:
/*
* CheckingAccount.java
* A specialized account representing a checking
* account
*
*/
import java.util.Calendar;
public class CheckingAccount extends Account {
private int transactionFee = 3;
/**
*
* Checking account constructor requiring five
* attributes to create
*
*/
public CheckingAccount(long accountNumber, double balance, Calendar dateOpened,Customer customer){
// Call superclass constructor
super(accountNumber, balance, dateOpened, customer);
}
// CheckingAccount have interest free in the first two month.
// it charge 3$ for every transaction(deposit/withdrawal)
public void deducteFee(double fee){
fee = balance - transactionFee;
}
public void calculateIntrest(){
balance = balance + balance * transactionFee;
}
/**
*
* String representation of this object
*
*/
public String toString(){
String output = super.toString();
return output;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.