Java Forum / General / April 2008
Student Banking Problem
rgt - 11 Apr 2008 03:20 GMT 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); } } } }
GArlington - 11 Apr 2008 15:05 GMT > 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 [quoted text clipped - 9 lines] > > 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...
Arved Sandstrom - 11 Apr 2008 19:48 GMT >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 [quoted text clipped - 9 lines] > > 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
Mark Space - 11 Apr 2008 21:17 GMT > 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.
Lew - 12 Apr 2008 01:17 GMT > 2. System.out.println is good! You should always instrument your code Actually println() is a very weak logging tool, good only for learning. After one gets some confidence in other matters, use a real logging tool like log4j or the java.util.logging package.
> 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 "assert()"? That is not a legal method, since 'assert' is a keyword. It is not a logging tool; its purpose is to enforce programming invariants.
I see you already made the point about logging packages.
> 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. Logging doesn't usually get turned off at all. Its level gets turned up or down, usually not through the command line but through configuration or properties files.
Very few people take the time or care to design good logging strategies. Logging supports operations and maintenance, more than development. You have to be smarter than a developer usually is to appreciate the issues involved. Go talk to operations folks to get the necessary insight.
 Signature Lew
Mark Space - 12 Apr 2008 01:49 GMT > "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.)
Roedy Green - 15 Apr 2008 00:33 GMT On Sat, 12 Apr 2008 00:49:37 GMT, Mark Space <markspace@sbc.global.net> 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.
 Signature
Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Roedy Green - 14 Apr 2008 23:44 GMT >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.
 Signature
Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Arne Vajhøj - 15 Apr 2008 02:24 GMT >> Very few people take the time or care to design good logging strategies. > [quoted text clipped - 9 lines] > 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
Roedy Green - 14 Apr 2008 23:41 GMT On Fri, 11 Apr 2008 20:17:25 GMT, Mark Space <markspace@sbc.global.net>
Is your name really Mark Space , or is this a reference for the old timers about mark=1 and space=0 in modems?
 Signature
Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Mark Space - 15 Apr 2008 02:23 GMT > On Fri, 11 Apr 2008 20:17:25 GMT, Mark Space > <markspace@sbc.global.net> > > 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>
Roedy Green - 24 Apr 2008 09:58 GMT On Mon, 14 Apr 2008 18:23:53 -0700, Mark Space <markspace@sbc.global.net> wrote, quoted or indirectly quoted someone who said :
>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. That takes me back. My Dad was into ham radio, VE7CH, and set my sister and I up with a key and some wire and taught us how to send "secret" messages to each other tapping them out on an antique brass device that looked something like this:
http://www.antiqbuyer.com/ARCHIVEPICS/keys/bunell-key.jpg
 Signature
Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|