Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Student Banking Problem

Reply
Thread Tools

Student Banking Problem

 
 
rgt
Guest
Posts: n/a
 
      04-11-2008
I know this is probably beneath everyone here, but I am looking for some
guidance on this assignment. I can't seem to get the balance to update
correctly.

Requirements:
1. Main Bank Account
2. Checking with overdraft protection
3. Savings no overdraft protection
I can get the accounts do withdraw just fine, but when the overdraft
kicks in, it doesn't seem to know that the limit has been exceeded. Can
anyone give some feedback?

Thanks in advance, code below:

/**
* @(#)BankAccount.java
*
* BankAccount application
*
* @author
* @version 1.00 2008/4/6
*/
import java.util.Date;

public class BankAccount {

public static void main(String[] args) {

System.out.println("Account Program Output:");
/*Account instantiation*/
Account idNum = new Account(1122, 20000.00, 4.50);
Account c = new Checking(9850, 20000.00, 3.82, 1500.00);
Account s = new Savings(1768, 15000.00, 7.90);

System.out.println("\nParent Account: " + idNum);
System.out.println("\nChecking: " + c);


/*Checking Transactions*/
System.out.println("Transactions:");
c.withDraw(19000.00);
c.withDraw(1400.00);
c.withDraw(1800.00);
/*Savings Transactions*/
System.out.println("\nSavings: " + s);
System.out.println("Transactions:");
s.withDraw(14000.00);
s.withDraw(1400.00);
s.withDraw(300.00);
}
}

class Account{

protected int id = 0;
protected double balance = 0.00;
protected double annualInterestRate = 0.00;
double monthlyInterestRate;

Date dateCreated;

/************************************************** ****************
**************************Consructors************* ****************
************************************************** ****************/

//no-arg constructor that creates a default account
Account(){
}

//construct new account with id, balance, APR
Account(int id, double balance, double annualInterestRate){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
//this.monthlyInterestRate;
dateCreated = new java.util.Date();
}

/************************************************** ****************
**Accessor and Mutator methods for id, balance, APR, dateCreated**
************************************************** ****************/
//accessors:
public int getId(){
return id;
}
public double getBalance(){
return balance;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public String getDate(){
return dateCreated.toString();
}
public double getMonthlyInterstRate(){
return monthlyInterestRate;
}
//mutators:
public void setId(int newId){
this.id = newId;
}
public void setBalance(double newBalance){
this.balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate){
annualInterestRate = newAnnualInterestRate;
}
public void setMonthlyInterestRate(double newMIR){
monthlyInterestRate = (newMIR >= 0) ? newMIR : 0;
}

/************************************************** ****************
**********************Class Methods*******************************
************************************************** ****************/

public String toString(){
return "\nAcct ID: " + id + "\tBalance: $"
+ balance + "\tAnn. Int. Rate: " +
annualInterestRate + "%" + "\tMonthly Int. Rate: " +
getMonthlyInterstRate();
}

public void printBalance(){
System.out.println("Current balance: $" + balance +
"\n");
}

public double getMonthlyInterestRate(double APR){
double monthlyInterestRate = APR / 12.00;
return monthlyInterestRate;
}

public void withDraw (double amountW){
balance -= amountW;
}

public void deposit (double amountD){
balance += amountD;
}
}

/************************************************** ****************
********************Sub Class Checking****************************
************************************************** ****************/
class Checking extends Account{
private double oDraft = 1500.00;

/*Constructors*/
Checking(){
}
Checking(int id, double balance, double annualInterestRate, double
newODraft){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
oDraft = newODraft;
dateCreated = new java.util.Date();
}

/*Accessor/Mutators*/
public double getLimit(){
return oDraft;
}
public void setLimit(double limit){
oDraft = limit;
}

/*Withdraw*/
public void withDraw(double amount){
System.out.println("\nTransaction: Withdraw: " + amount);
double credit = 500.00;
double temp = balance - amount;
double tempODraft = oDraft - credit;
if (balance > 0.00){
balance = temp;
System.out.println("Checking debited: \t\t" + amount);
System.out.println("New balance: \t\t\t" + balance);
}
while(balance < 0.00 && oDraft > 0.00){
while (oDraft > 0.00){
System.out.println("Checking overdrawn by: " +
balance);
System.out.println("Overdraft balance: \t\t" +
oDraft);
if(oDraft >= 0.00){
balance += credit;
oDraft = tempODraft;
System.out.println("Over draft credit: \t
\t" + credit);
System.out.println("New balance: \t\t\t" +
balance);
}
}
}
if(balance < 0.00 && oDraft <= 0.00){{
System.out.print("You have exceeded the over draft
limit of: 1500.00");
System.out.print("Ending Balance: \t\t\t" + balance);
}

//}
}
}

/************************************************** ****************
********************Sub Class Savings****************************
************************************************** ****************/
class Savings extends Account{
private double oDraft = 0;
/*Constructors*/
public Savings(){
}
public Savings(int id, double balance, double annualInterestRate){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new java.util.Date();
}
/*Withdrawls*/
public void withDraw(double amount){
System.out.println("\nTransaction: Withdraw: " + amount);
double credit = 500.00;
double temp = balance - amount;
double tempODraft = oDraft - credit;
if (temp > 0.00){
balance = temp;
System.out.println("Savings debited: " + amount);
System.out.println("New check balance: " + balance);
}
else if(temp < 0.00 && tempODraft > 0.00){
while (temp < 0.00 && tempODraft > 0.00){
System.out.println("Savings overdrawn by: " +
balance);
//if(tempODraft >= 0.00){
balance += credit;
oDraft = tempODraft;
System.out.println("Over draft credit: " +
credit);
System.out.println("New balance: " +
balance);
System.out.println("New oDraft balance:" +
balance);
//}
}
}
else{
System.out.print("You have exceeded the over draft
limit of: 0.00");
System.out.print("Ending Balance: " + balance);
}
}
}
}
 
Reply With Quote
 
 
 
 
GArlington
Guest
Posts: n/a
 
      04-11-2008
On Apr 11, 3:20 am, rgt <(E-Mail Removed)> wrote:
> I know this is probably beneath everyone here, but I am looking for some
> guidance on this assignment. I can't seem to get the balance to update
> correctly.
>
> Requirements:
> 1. Main Bank Account
> 2. Checking with overdraft protection
> 3. Savings no overdraft protection
> I can get the accounts do withdraw just fine, but when the overdraft
> kicks in, it doesn't seem to know that the limit has been exceeded. Can
> anyone give some feedback?
>
> Thanks in advance, code below:

<snap>

Think about what your function withDraw() do...
1) get current balance
2) check if current balance (from 1) + overdraft limit (set for the
account) is greater or equal to the withdrawal amount
3) if 2) is true -> you have enough funds [plus overdraft] to withdraw
requested amount
4) if 2) is false -> you do not...
 
Reply With Quote
 
 
 
 
Arved Sandstrom
Guest
Posts: n/a
 
      04-11-2008
"rgt" <(E-Mail Removed)> wrote in message
news:Xns9A7CD9200D260rgtrgtcom@207.115.33.102...
>I know this is probably beneath everyone here, but I am looking for some
> guidance on this assignment. I can't seem to get the balance to update
> correctly.
>
> Requirements:
> 1. Main Bank Account
> 2. Checking with overdraft protection
> 3. Savings no overdraft protection
> I can get the accounts do withdraw just fine, but when the overdraft
> kicks in, it doesn't seem to know that the limit has been exceeded. Can
> anyone give some feedback?
>
> Thanks in advance, code below:

[ SNIP ]

Some general points:

1. Pin down your logic a bit. Is a Savings account ever going to have
overdraft? Is it a future possibility, or simply not going to happen?

If Savings accounts will never have overdraft, don't put any code in the
class that relates to overdraft. Keep it in Checking.

If Savings accounts may eventually have overdraft, or you're hedging your
bets, then both Checking and Savings have the same overdraft code. So that
logic should be in Account. Account could actually have 2 withdraw methods,
a vanilla withdraw and a withdrawWithOverdraft.

2. If you don't have a decent implementation for a method in a parent class,
keep it abstract.

3. Your class methods in Account aren't. They are instance methods. Which
actually makes sense.

4. There isn't much reason for a no-arg constructor here. There are no
sensible defaults. Some situations do require empty constructors - this one
doesn't.

5. Use super() in constructors where you can. Here you can.

6. Try not to hardcode problem-specific constants. I have no problem with 12
for #months (using integer makes the meaning clearer), but 1500.00 and 0.00
for default overdraft amounts, and 500.00 for a credit amount, makes for
maintainability issues. Regardless of whether they are class constants or
instance constants, they should be (logically) settable/gettable from
outside. True constants, of course, can be hardcoded...like the value of the
atomic fine constant.

7. Pseudocode your logic for withdraw with overdraft. Once you're happy with
it then code it.

AHS


 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-11-2008
rgt wrote:

> I can get the accounts do withdraw just fine, but when the overdraft
> kicks in, it doesn't seem to know that the limit has been exceeded. Can
> anyone give some feedback?


I couldn't follow your logic for overdraft. I think you may know what
to do, but you need to simply your logic (if/else statements) greatly.

Consider something like this:

balance -= amount;
if( balance >= 0 )
//... ok, normal
else if( balance >= oDraft )
// ... overdraft
else
// over drawn!

That's what I see off the top of my head, three cases that need to be
dealt with. The math for each test should be very simple.


Here's some other off the wall thoughts by me:

1. Doubles for money are fine for this assignment. In a real program,
accounting functions like this almost always require BigDecimal. If you
have time, switch your program over to BigDecimal (but get it working
first, and save a copy!)

2. System.out.println is good! You should always instrument your code
like that to make it easier to debug. But println can be clumsy too if
you lots of them. Consider using assert() and/or the Logger class in
the future. These can be controlled by the command line or inside the
program, to turn them off when you don't need them anymore.
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-12-2008
Lew wrote:

> "assert()"? That is not a legal method, since 'assert' is a keyword.


Good point. I'm used to putting my assert expressions in parenthesis
for clarity, especially when the second expression is used. But they
are not actually part of the assert syntax.

Regarding the use of println's, the OP is obviously a student, and doing
anything at all beside poking the keyboard and grunting is a vast
improvement over most. (I was a computer lab teaching assistant for
several years while college.)
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-14-2008
On Fri, 11 Apr 2008 20:17:25 GMT, Mark Space
<(E-Mail Removed)>

Is your name really Mark Space , or is this a reference for the old
timers about mark=1 and space=0 in modems?
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-14-2008
On Fri, 11 Apr 2008 20:17:59 -0400, Lew <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>Very few people take the time or care to design good logging strategies.


I think the key idea is logging is not something you put in then
remove as soon as the bug is fixed. You LEAVE it there, but partially
deactivate it, ready to help track down any future problems. You also
leave it partially activated, even in production to help locate the
source of problems in production, and to produce the routine
statistics.

You can fine tune just how much detail you want where.

Real logging is easy to turn on and off, and adjust the detail level,
without having to recompile the universe.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-14-2008
On Sat, 12 Apr 2008 00:49:37 GMT, Mark Space
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>Regarding the use of println's, the OP is obviously a student, and doing
>anything at all beside poking the keyboard and grunting is a vast
>improvement over most. (I was a computer lab teaching assistant for
>several years while college.)


I agree, a formal logger would probably overwhelm the student. But
when he or anyone else feels ready to tackle one, have a look at
http://mindprod.com/jgloss/logging.html
to get you started.

You will need to understand the basic idea if you ever work on a team
project. They will almost always have some sort of logging engaged.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-15-2008
Roedy Green wrote:
> On Fri, 11 Apr 2008 20:17:25 GMT, Mark Space
> <(E-Mail Removed)>
>
> Is your name really Mark Space , or is this a reference for the old
> timers about mark=1 and space=0 in modems?


Close! My name is not Mark Space. It's a pseudonym I've been using
online for about 15 years.

The terms mark and space go back a lot further than modems. The terms
are from communication theory and go all the way back to telegraphy and
Morse code. Morse code was encoded using marks and spaces.

<http://zone.ni.com/devzone/cda/ph/p/id/119>
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      04-15-2008
Roedy Green wrote:
> On Fri, 11 Apr 2008 20:17:59 -0400, Lew <(E-Mail Removed)> wrote,
> quoted or indirectly quoted someone who said :
>> Very few people take the time or care to design good logging strategies.

>
> I think the key idea is logging is not something you put in then
> remove as soon as the bug is fixed. You LEAVE it there, but partially
> deactivate it, ready to help track down any future problems. You also
> leave it partially activated, even in production to help locate the
> source of problems in production, and to produce the routine
> statistics.
>
> You can fine tune just how much detail you want where.
>
> Real logging is easy to turn on and off, and adjust the detail level,
> without having to recompile the universe.


That is how logging frameworks work. And have for approx. a decade
(for Java at least).

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ANZ banking problem with Opera 8.5 GraB NZ Computing 8 05-12-2006 08:19 AM
Banking Problem ashu C++ 13 10-21-2005 01:11 AM
Firefox 1.0.4 ANZ online banking problem GraB NZ Computing 15 05-24-2005 11:38 AM
Can't Confirm Banking Transfers- Online Banking Bank of America (Firefox1.0) totsob Firefox 1 10-22-2004 10:19 PM
Firefox and online banking Hilary A Croughton Firefox 7 07-30-2004 02:00 PM



Advertisments