Results 1 to 12 of 12
Thread: Bank Account
- 02-26-2010, 06:57 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
Bank Account
Hi I need to create Bank account which would give these values when the tester class is applied:
The tester class is
public class BankAccountTester
{
public static void main(String[] args)
{
BankAccount account = new BankAccount(100);
account.deposit(-100);
System.out.println(account.getBalance());
System.out.println("Expected: 100");
account.withdraw(-50);
System.out.println(account.getBalance());
System.out.println("Expected: 100");
account.withdraw(200);
System.out.println(account.getBalance());
System.out.println("Expected: 100");
}
}
MY CODE IS
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
private double balance;
private int transaction;
private double minimumBalance;
//define the minimum balance method
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{ if (amount<=0) {
amount=0;
double newbalance= balance +amount*0;
balance=newbalance;
transaction=transaction +1;}
if (amount>=0){
double newBalance = balance + amount;
balance = newBalance;
transaction = transaction +1; }}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{ if (amount<=balance){
double newBalance = balance + amount;
balance = newBalance;
transaction = transaction +1;
minimumBalance = Math.min(minimumBalance, balance);
}
if (amount>balance){
double newBalance = balance + amount*0;
balance = newBalance;}}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public void endOfMonth(double interestRate)
{
balance= balance - transaction;
double interest = minimumBalance * interestRate / 100 / 12;
transaction= 0;
}
// declare a minimumBlance Method
public double getminimumBalance()
{ return minimumBalance;
}}
My Values comes out to be:
100.0
Expected: 100
50.0
Expected: 100
50.0
Expected: 100
Can anyone inspect my code and tell me whats wrong?
-
Hello.
Please use code tags and also tell us what is wrong: what is your current program doing/not doing that is incorrect? What error messages are you seeing?
Best of luck!
- 02-26-2010, 01:29 PM #3
it quite confusing if you use positive and negative amounts in your transactions/method-calls. my suggestion: always use positive amounts in transactions and then subtract this amount in the withdraw-method and add the amount in the deposit-method to the balace. your problem is quite easy to solve and i hope you're not really working as a developer for a bank ;)
- 02-26-2010, 01:35 PM #4
what is this logic? Just put what u are going to do in each of the method above as comments.As per the logic its working....But really very dangerous bank.
Deposited 100 and trying to withdraw 200 .............
hahaha........Ramya:cool:
- 02-26-2010, 01:44 PM #5
- 02-26-2010, 06:12 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
No the program is suppose to block such a transaction. SO I'm having trouble with the withdraw method. Can someone help me fix my withdraw method. Which is :
THIS METHOD IS SUPPOSE THE CANCEL TRANSACTION with amount more than the current balance. Also withdraw amount can't be negative such as -50, so such a transaction should also be canceled.
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{ if (amount<=balance){
double newBalance = balance + amount;
balance = newBalance;
transaction = transaction +1;
minimumBalance = Math.min(minimumBalance, balance);
}
if (amount>balance){
double newBalance = balance + amount*0;
balance = newBalance;}}
- 02-26-2010, 06:30 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
There are no two ifs, if you want to withdraw more than is present on your account or if you want to withdraw a negative amount of money the lady at the counter just stares at you and won't listen to you:
kind regards,Java Code:if (amount <= balance && amount > 0) { // sensible amount for a withdrawal? balance-= amount; transactions++; }
Jos
- 02-26-2010, 06:40 PM #8
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
- 02-26-2010, 06:59 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
The mere fact that a book uses two if statements doesn't imply that you have to use two if statements. e.g. what is wrong with my version? Also, what does "doesn't seem to work" mean? Incorrect results? Doesn't compile? Crashes? Daemons flying out of your nose?
kind regards,
Jos
- 02-26-2010, 07:03 PM #10
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
- 02-26-2010, 07:05 PM #11
Member
- Join Date
- Feb 2010
- Posts
- 12
- Rep Power
- 0
I hope daemons dont fly out of your nose :)
- 02-26-2010, 09:05 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Transfer from one account to another
By vividcooper in forum New To JavaReplies: 3Last Post: 01-26-2010, 10:43 PM -
validating account
By ServletEst in forum Java ServletReplies: 3Last Post: 08-24-2009, 01:11 PM -
Java Question Bank
By mgm2010 in forum New To JavaReplies: 2Last Post: 07-31-2009, 06:45 PM -
Code bank
By Eranga in forum Suggestions & FeedbackReplies: 2Last Post: 01-01-2008, 05:23 AM -
Help MEEEEEEE! bank project
By subwoofer717 in forum Advanced JavaReplies: 1Last Post: 12-25-2007, 09:08 AM


LinkBack URL
About LinkBacks


Bookmarks