The Artima Developer Community
Sponsored Link

Java Answers Forum
Need help with BankAccount program

1 reply on 1 page. Most recent reply: Mar 30, 2006 3:46 AM by Edouard G.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jenny G

Posts: 2
Nickname: jenny04
Registered: Mar, 2006

Need help with BankAccount program Posted: Mar 28, 2006 6:42 AM
Reply to this message Reply
Advertisement
I have a java assignment to do on a bank account and i was wondering if anyone could help me becasue i keep getting errors.

Here is my code any advice would be helpful.
And uuInOut is what i use to read stuff in, if you's wanna run it you can change to whatever it you use.

--------class date------------
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(){

do
{

System.out.println("Enter Day:");
day = uuInOut.ReadInt();

System.out.println("Enter Month:");
month = uuInOut.ReadInt();

System.out.println("Enter Year:");
year = uuInOut.ReadInt();

if(validDate());
System.out.println("ERROR! Date in invalid.");
}

while (validDate()== false);
}//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

--------------class Transaction----------------
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();
}//readTransaction


public void printTransaction() {

d.printDate();
System.out.print("\t" +amount);

if(newBalance < 0)
{
System.out.print(-newBalance +"OD");
}//end if

System.out.println("\t" +type);

}//printTransaction


public boolean getType() {
return type;
}//getType

public double getAmount() {
return amount;
}//getAmount

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

}//end of class Transaction

----------------class bankaccount-------------

class BankAccount
{

//Properties
final static int SIZE = 100;

private String name;
private int accountNum;
private double currentBalance;
private int numTransactions;
private Transaction [] list;

//Constructor
public BankAccount()
{
list = new Transaction [SIZE];

numTransactions =0;
}


//Methods
public double withdraw(boolean getType())
{
if(getType() == true)
{
return currentBalance - getAmount();

}//end if

}//end withdraw


public double deposit (boolean getType())
{
if(getType() == false)
{
return currentBalance + getAmount();

}//end if

}//end deposit


public void addTransaction()
{
System.out.println("Enter Name:\t");
name = uuInOut.ReadString();
System.out.println("Enter Account Number:\t");
accountNum = uuInOut.ReadInt();
System.out.println("Enter Current Balance:\t");
currentBalance = uuInOut.ReadDouble();

for(int i = 0; i < SIZE; i++)
{
list .readDate();
list .readTransaction();
numTransactions++;

}
}


public void printTransaction()
{
System.out.println("*******************************");
System.out.println("Name:\t" + name);
System.out.println("Account Number:\t" + accountNum);
System.out.println("Current Balance:\t" + currentBalance);
System.out.println("*******************************");
System.out.println("Transactions");

for(int i = 0; i < SIZE; i++)
{
list .printTransaction();
System.out.println("*******************************");

}//end for

};//printTransaction


}// end of class BankAccount

------------------------------------------------------

Its the class BankAccount that im having trouble with.
In it i have to add an appropriate constructor and methods.
I need methods for withdrawing and depositing, 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.

I also need to add an application class which will be used to test the other classes.


If anyone can get me any advice on my bankaccount class or point out any mistakes that im making or anything which i should change, it would be very much appreciated.

Thanks in advance


Edouard G.

Posts: 1
Nickname: edouard
Registered: Mar, 2006

Re: Need help with BankAccount program Posted: Mar 30, 2006 3:46 AM
Reply to this message Reply
Hi Jenny,

There are a few things I don't really understand in your BankAccount class. It seems to me that several things got mixed up.

If I had to make a BankAccount class I would prefer the following interface:
Class BankAccount{
	public BankAccount()
	public void withdraw(double amount)
	public void deposit(double amount)
	public Transaction getTransaction(int index)
	public int transactionCount()
	public double getCurrentBalance()
	private void addTransaction(Transaction t)
}


in my withdraw and deposit method I change the currentBalance and I create a new Transaction and add it to the collection of Transactions (since I hate arrays, I would probably use one of the Collection-classes from the java.util package to store my transactions).
The PrintTransaction and ReadTransaction methods I would not place in the BankAccount-class, neither in my Transaction- class. It makes my classes depend on the UI. Prove of this is, since I don't have your uuInOut class, I need to change al lot of code in order to get things right.
It's better to do this printing-logic in a class of its own or in your Application-class.

An alternative interface could be:
Class BankAccount{
	public BankAccount()
	public Transaction getTransaction(int index)
	public int transactionCount()
	public double getCurrentBalance()
	public void addTransaction(Transaction t)
}

In this case you add a transaction to the BankAccount. In this method you change the currentBalance depending on the type of Transaction and then add it to the collection of transactions.

All this remarks apply also to the other classes in your project. They depend too heavy on the UI.

Greetings,

Edouard

Flat View: This topic has 1 reply on 1 page
Topic: how do you get an array to contain results from another array Previous Topic   Next Topic Topic: using enumerations and Switch

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use