This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes New to Java - i need help please! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New to Java - i need help please!" Watch "New to Java - i need help please!" New topic
Author

New to Java - i need help please!

Joe Grimp
Greenhorn

Joined: Apr 02, 2006
Posts: 15
I'm pretty new to Java, and have been given this problem to solve. I'm just finding it difficult to get started, so was wondering if anyone could give me some help to get the ball rolling. Lots of thanks in advance!

Here's my task -

A bank requires software to deal with customer accounts. In particular, it should keep a record of transactions that are carried out, adjusting the balance of the account accordingly.

The software should carry out the following:

1. Permit the user to enter the relevant details for up to 100 transactions for a given bank account. (In each case the user should be prompted for the date, amount of money for the transaction, and whether it was a withdrawal or a deposit.)

2. Adjust the balance after each transaction depending on whether it was a withdrawal or deposit. (These are the only two types of transactions to be considered.)

3. Print out a bank statement in the following format:

******************************************
Name: Tom White
Account Number: 89475020
Current Balance: 200.00OD
******************************************
Transactions
28/12/2005 1500.00 1500.00 D
1/1/2006 550.00 950.00 W
14/1/2006 50.00 1000.00 D
18/1/2006 1200.00 200.00OD W
******************************************

(Note that the number appearing after the date is the amount for the transaction, the next number is the resulting balance with OD for overdrawn and W and D for withdrawal and deposit respectively.)

You should write a class BankAccount, which will have relevant properties for an account including an array of instances of the class Transaction, as well as an appropriate constructor and methods. The methods should include methods for depositing or withdrawing money (see notes); an addTransaction method to add a new instance to the array, read in its details (including date, etc.) and update the balance using the deposit or withdraw methods; a printStatement method to give a printout as indicated above.

You should write an application program which makes use of the above classes. Since it is only used for testing the other classes only a single instance of BankAccount needs to be created. The user should be allowed to add as many transactions as required (up to 100). Finally, it should print out the statement.

Writing the class bankAccount ^^ and Application program ^^ are what are giving me trouble. Can anyone help? Thanks!
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
You have trouble writing the class? i assume you dont know how to do any of it then?

The class bank account will store your methods, addTransaction etc. And the application will be used to test them.





Thats what i think is meant.

[ April 02, 2006: Message edited by: Stephen Foy ]
[ April 02, 2006: Message edited by: Stephen Foy ]

Stephen Foy - Microsoft Application Development Consultant
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Joe, what's your comfort level with what Stephen suggested there? Is that starting at the right level for you?

I'm going to suggest a way to approach such a problem on the ranch or with a mentor. I'm sure your first reaction will be that it will never work and you need an answer faster than this will get it for you. Trust us a bit and give it a try.

OK, I'm looking for the smallest thing in this program that we can write and test. And I think it's so small your instructor didn't even mention it, or maybe didn't think of it - a single Transaction. See if you can make this code run and say "true" three times:

Take a shot at it and post some code. Even if you feel hopelessly stuck that's the best way for us to see just what kind of help you need and get you moving. This can be a lot of fun!

By the way ... if you're feeling tempted to start with prompting the user and reading input, don't give in! With that approach you have to type test data over and over. By 2:00 AM you'll be really tired of typing. This way you put it into JoesTest once and never type it again. We can build the user input part of the program LAST and it will be a piece of cake by then.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
This reminds me of program in the book, Java - How to program, is that your reading text book?
Joe Grimp
Greenhorn

Joined: Apr 02, 2006
Posts: 15
Aditionally, i've also been given some unfinished code and asked to complete it. This consists of two classes - the ClassDate and the ClassTransaction.

ClassDate code -

class Date
{// properties
private int day, month, year;

public Date (Date d) {
day = d.day;
month = d.month;
year = d.year;
}

public Date () {
}

// methods
public void readDate(){

//TO BE COMPLETED

}//readDate

public void printDate() {
System.out.print(day + "/" + month + "/" + year);
}//printDate

public boolean validDate()
{if (!validYear(year))
return false;
else if (!validMonth(month))
return false;
else if(!validDay(day,month))
return false;
else
return true;
}//validDate

private boolean validYear(int y)
{if ((y < 1990) || (y > 2100))
return false;
else
return true;
}//validYear

private boolean validMonth(int m)
{if ((m < 0) || (m > 12))
return false;
else
return true;
}//validMonth

private boolean validDay(int d, int m)
{if ((d < 0) || (d > daysinMonth(m)))
return false;
else
return true;
}//validDay

private int daysinMonth(int m)
{int NumDays;
switch(m)
{case 1:case 3:case 5:case 7:
case 8:case 10:case 12 : NumDays = 31;
break;
case 4:case 6:case 9:case 11:
NumDays = 30;
break;
case 2 : NumDays = 28;
break;
default: NumDays = 0;
}
return NumDays;
}//daysinMonth


} // end of class Date

The readDate method should prompt the user and read in relevant values for day, month and year and should determine whether the dates are valid by calling the method validDate.

Any ideas on how i can complete this code ^^^ ?


ClassTransaction code -

class Transaction {

private double amount;
private double newBalance;
private boolean type;
private Date d;

public void readTransaction() {
char c;
d = new Date();
d.readDate();

do
{
System.out.print("Is this transaction a withdrawal or deposit? (W or D)
");
c = uuInOut.ReadChar();
uuInOut.ReadLn();

if (c != 'W' && c != 'w' && c != 'D' && c != 'd')
System.out.println("Invalid entry. Try again.");

}while (c != 'W' && c != 'w' && c != 'D' && c != 'd');

if (c == 'W' || c =='w')
type = true;
else
type = false;
System.out.print("Enter amount: ");
amount = uuInOut.ReadDouble();
}

public void printTransaction() {

//TO BE COMPLETED

}

public boolean getType() {
return type;
}

public double getAmount() {
return amount;
}

public void setBalance(double value) {
newBalance = value;
}
}

Complete the class Transaction, by providing code for the method printTransaction.

Again any help with finishing the above code?

Thanks alot again for any help you can provide!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18535
    
  40

Joe,

Stephan and Stan gave you a skeleton and plan to get started. Don't try to do parts of the assignment before you are ready. It will be overwhelming if you do -- and you probably won't learn anything.

Start from the skeleton, try something small. And if you have any questions, ask here.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Joe Grimp
Greenhorn

Joined: Apr 02, 2006
Posts: 15
For the classDate, i have got the following so far for what has to be completed. Could anyone tell me how this is looking -



And can anyone help me with calling the validDate method.. i don't know how! Thanks. (The classDate code is ^^^)
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
To call methods you create an object to call it.



Give the print transaction an attempt, and post it here, this way we can see what your struggling with.
[ April 03, 2006: Message edited by: Stephen Foy ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18535
    
  40

Originally posted by Joe Grimp:
For the classDate, i have got the following so far for what has to be completed. Could anyone tell me how this is looking -

And can anyone help me with calling the validDate method.. i don't know how! Thanks. (The classDate code is ^^^)


Great... you got started...

But instead of us telling you how it is looking, you can tell us. Write a test class that calls your readDate() method, and print out the variables that it sets.

Does it look like it is working correctly? Are the variables set correctly?

Henry
Joe Grimp
Greenhorn

Joined: Apr 02, 2006
Posts: 15
Here is the printTransaction -



Any opinions?
 
jQuery in Action, 2nd edition
 
subject: New to Java - i need help please!
 
Similar Threads
Having troubles with a complex program, need help with an illegal start of type error.
loop for switch case
interface, exception help
How to get a transfer function to work both ways?
Bank Account Program Help