12 Replies - 6690 Views - Last Post: 25 October 2009 - 06:33 PM Rate Topic: -----

#1 cicsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 09

Bank Account with Random File Access

Post icon  Posted 24 October 2009 - 08:35 AM

Good morning,

I am working on an assignement for class that is to create a program that manages bank accounts with acct number, customer name and balance. Need to use Random Access File, accessing records by index and not directly by account number.

What I am trying to do is to get the basic programs to work before I add the file access and hashtable portion. Found some sample code for Account and AccountTest but the Account Test does not compile. Code is below......

import java.util.Scanner;
 
public class TestAccount
{
	public static void main(String[] args)
	{
		String name;
		double balance;
		long acctNum;
		Account acct;
 
		Scanner scan = new Scanner(System.in);
 
		System.out.println("Enter account holder's first name");
		name = scan.next();
		acct = new Account(name);
		System.out.println("Account for " + name + ":");
		System.out.println(acct);
 
		System.out.println("\nEnter initial balance");
		balance = scan.nextDouble();
		acct = new Account(balance,name);
		System.out.println("Account for " + name + ":");
		System.out.println(acct);
 
		System.out.println("\nEnter account number");
		acctNum = scan.nextLong();
		acct = new Account(balance,name,acctNum);
		System.out.println("Account for " + name + ":");
		System.out.println(acct);
 
		System.out.print("\nDepositing 100 into account, balance is now ");
		acct.deposit(100);
		System.out.println(acct.getBalance());
		System.out.print("\nWithdrawing $25, balance is now ");
		acct.withdraw(25);
		System.out.println(acct.getBalance());
		System.out.print("\nWithdrawing $25 with $2 fee, balance is now ");
		acct.withdraw(25,2);
		System.out.println(acct.getBalance());
 
		System.out.println("\nBye!");
	}
}


Error code:

cannot find symbol  : constructor Account(java.lang.String)
location: class Account
		acct = new Account(name);
			   ^
TestAccount.java:29: cannot find symbol


Although this is not the final version of the AccountTest program I just wanted to get a 'start'.

Any ideas? Project is due tomorrow night at midnight..........

Thanks!!!!!!!!

Edited by Locke. In the future, please :code:. Thanks! :)

This post has been edited by Locke: 24 October 2009 - 08:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Bank Account with Random File Access

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,766
  • Joined: 27-December 08

Re: Bank Account with Random File Access

Posted 24 October 2009 - 08:42 AM

:code:

The error is saying that it can't find a constructor for your Account class that accepts a String value as a parameter. So in other words, you are trying to use a constructor that doesn't exist.
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Bank Account with Random File Access

Posted 24 October 2009 - 08:52 AM

Please, :code:. I edited your post this time, but please remember in the future. Thanks.

To tell you how to fix this error, we'd need to see the Account class.

But like macosxnerd101 said, you're trying to call a constructor using the wrong parameters.

This post has been edited by Locke: 24 October 2009 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 cicsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 09

Re: Bank Account with Random File Access

Posted 24 October 2009 - 09:04 AM

Here is the Account Code......it has three parms in the constructor...

public class Account
{
private double balance;
String name;
private long acctNum;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
// If so, decrements balance by amount; if not, prints message.
//----------------------------------------------
public void withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
}
//----------------------------------------------
// Adds deposit amount to balance.
//----------------------------------------------
public void deposit(double amount)
{
balance += amount;
}
//----------------------------------------------
// Returns balance.
//----------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name:" + name + "\nAccount Number: " + acctNum + "\nBalance: " + balance; 
}
}



Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,766
  • Joined: 27-December 08

Re: Bank Account with Random File Access

Posted 24 October 2009 - 04:35 PM

With methods, you cannot pick and choose which parameters to use. If a method has 3 parameters, all 3 spots have to be filled. Since you are trying to call a constructor method with only a String parameter, the compiler is telling you that no constructor with only a single String parameter exists. You have to define it.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Bank Account with Random File Access

Posted 24 October 2009 - 04:36 PM

View Postcicsguru, on 24 Oct, 2009 - 07:35 AM, said:

Need to use Random Access File, accessing records by index and not directly by account number.

What is this inepstie ?

You will have to define a class FileRecord which will contain a String that will have always the same length
store your data into it and find a way to bind record nummber to bank accoiunt number
Was This Post Helpful? 0
  • +
  • -

#7 cicsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 09

Re: Bank Account with Random File Access

Posted 25 October 2009 - 09:29 AM

Hello again......I found some sample code to do file access, however it is not compiling.

import java.io.IOException;
import java.io.RandomAccessFile;
import java.*;
 
/**
   This class is a conduit to a random access file
   containing savings account data.
*/
public class BankAcct
{
   /**
	  Constructs a BankData object that is not associated
	  with a file.
   */
   public BankAcct()
	{
	  file = null;
   }

   /**
	  Opens the data file.
	  @param filename the name of the file containing savings
	  account information
	*/
   public void open(String filename)
		 throws IOException
	{
	  if (file != null) file.close();
	  file = new RandomAccessFile(filename, "rw");
   }
  /**
	  Gets the number of accounts in the file.
	  @return the number of accounts
   */
   public int size()
		 throws IOException
   {
	 return (int) (file.length() / RECORD_SIZE);
   }

   /**
	  Closes the data file.
   */
   public void close()
		 throws IOException
   {
	  if (file != null) file.close();
	  file = null;
   }
  /**
	  Reads a savings account record.
	  @param n the index of the account in the data file
	  @return a savings account object initialized with the file data
   */
  public BankAccount read(int n)
		 throws IOException
   {  
	  file.seek(n * RECORD_SIZE);	  
	  int accountNumber = file.readInt();
	  double balance = file.readDouble();
	  return new BankAccount(accountNumber, balance);
   }

   /**
	  Finds the position of a bank account with a given number
	  @param accountNumber the number to find
	  @return the position of the account with the given number, 
	  or -1 if there is no such account
   */
   public int find(int accountNumber)
		 throws IOException
   {
	  for (int i = 0; i < size(); i++)
	  {
		 file.seek(i * RECORD_SIZE);
		 int a = file.readInt();		 
		 if (a == accountNumber) // Found a match
			return i;
	  } 
	  return -1; // No match in the entire file
   }

   /**
	  Writes a savings account record to the data file
	  @param n the index of the account in the data file
	  @param account the account to write
   */
   public void write(int n, BankAccount account)
		throws IOException
   {  
	  file.seek(n * RECORD_SIZE);
	  file.writeInt(account.getAccountNumber());
	  file.writeDouble(account.getBalance());
   }

   private RandomAccessFile file;

   public static final int INT_SIZE = 4;  
   public static final int DOUBLE_SIZE = 8;  
   public static final int RECORD_SIZE 
		 = INT_SIZE + DOUBLE_SIZE;
}



Error messsage:

BankAcct.java:61: cannot find symbol
symbol : constructor BankAccount(int,double)
location: class BankAccount
return new BankAccount(accountNumber, balance);
^
BankAcct.java:92: cannot find symbol

accountNumber is defined as an int and balance is defined as double. A bit confused, as usual..

Thanks!
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Bank Account with Random File Access

Posted 25 October 2009 - 12:37 PM

Where is your BankAccount class that you refer in that statement

return new BankAccount(accountNumber, balance);

you will need somewhere

class BankAccount {

	 int accountNumber;
	 double balance;

	 BankAccount(int num, double bal) {
		 accountNumber = num;
		 balance = bal;
	  }
}


Was This Post Helpful? 0
  • +
  • -

#9 cicsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 09

Re: Bank Account with Random File Access

Post icon  Posted 25 October 2009 - 05:04 PM

Hello! I am having an issue with an error message on a BankDataTester program - Code below:


va.io.RandomAccessFile;
import java.util.Scanner;
 
 /**
	This program tests random access. You can access existing
	accounts and deposit money, or create new accounts. The
	accounts are saved in a random access file.
 */
 public class BankDataTester
 {  
 int selection;
 public static int position;
 public static double amount;
	public static void main(String[] args)
		  throws IOException
	{  
	   Scanner in = new Scanner(System.in);
	   BankData data = new BankData();
		try
	   {  
		  data.open("Listofaccounts.dat");
 
		  boolean done = false;
		  while (!done)
		  {  
			 	 System.out.print("Enter Account Number:");
			 int accountNumber = in.nextInt();
				 position = data.find(accountNumber);
			BankAccount account; 
				 System.out.print("Enter 1 for Account Balance:");
				 System.out.print("Enter 2 for Deposit:");
				 System.out.print("Enter 3 for Withdrawal:");
			 int selection = in.nextInt();
				 switch (selection)
			 {
	 			case '1': 
					account = data.read(position);
				System.out.println("Balance for Account: " + accountNumber + " is: " 
				 + account.getBalance()); 
				break;					
				case '2':
				System.out.println("Enter Deposit Amount:");
				  amount = in.nextDouble();
			 position = data.find(accountNumber);
	   /*	  BankAccount account; */
				 account = data.read(position);
				account.deposit(amount);
				System.out.println("New Balance for Account: " + accountNumber + 
					  " is: " + account.getBalance());
			   break;
				case '3':
				System.out.println("Enter Withdrawal Amount:");
			amount = in.nextDouble();
			position = data.find(accountNumber);
		BankAccount account; 
				 account = data.read(position);
				account.withdraw(amount);
				System.out.println("New Balance for Account: " + accountNumber + 
					  " is: " + account.getBalance());
			   break;
				}
				data.write(position, account);
	 
	   }
   }	finally
	
	   {
		  data.close();
	   }
	}
 }




Receiving error message:

BankDataTester.java:56: account is already defined in main(java.lang.String[])
BankAccount account;
^
1 error

----jGRASP wedge2: exit code for process is 1.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Bank Account with Random File Access

Posted 25 October 2009 - 05:16 PM

If you look in your code for the main function you will notice that you have the line BankAccount account; shown twice. You can only declare the variable account once. Remember, you can only use the variable named once and once you define it, you can't define it again in the same scope. So remove the second of the two lines and make sure that you are working with the same account object. That or give the second line occurrence a different variable name like BankAccount checkingAccount;.

:)

This post has been edited by Martyr2: 25 October 2009 - 05:17 PM

Was This Post Helpful? 0
  • +
  • -

#11 cicsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 09

Re: Bank Account with Random File Access

Posted 25 October 2009 - 05:28 PM

View PostMartyr2, on 25 Oct, 2009 - 04:16 PM, said:

If you look in your code for the main function you will notice that you have the line BankAccount account; shown twice. You can only declare the variable account once. Remember, you can only use the variable named once and once you define it, you can't define it again in the same scope. So remove the second of the two lines and make sure that you are working with the same account object. That or give the second line occurrence a different variable name like BankAccount checkingAccount;.

:)



View Postcicsguru, on 25 Oct, 2009 - 04:26 PM, said:

View PostMartyr2, on 25 Oct, 2009 - 04:16 PM, said:

If you look in your code for the main function you will notice that you have the line BankAccount account; shown twice. You can only declare the variable account once. Remember, you can only use the variable named once and once you define it, you can't define it again in the same scope. So remove the second of the two lines and make sure that you are working with the same account object. That or give the second line occurrence a different variable name like BankAccount checkingAccount;.

:)


Thank you. I commented the second one out and now I am getting:

BankDataTester.java:63: variable account might not have been initialized
data.write(position, account);
^
1 error

This is occuring on the data.write line from the code above.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Bank Account with Random File Access

Posted 25 October 2009 - 05:43 PM

Don't see anything in your code justifying a new Topic
code merged
Avoid duplicated topics :angry:
Was This Post Helpful? 0
  • +
  • -

#13 cicsguru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-October 09

Re: Bank Account with Random File Access

Posted 25 October 2009 - 06:33 PM

Thank you....it appears that I did not have a default constuctor in BankAccount. So, I got past that one, now I am getting a bad return code trying to find the record in the .DAT file.

Code to find the position:


 	 System.out.print("Enter Account Number:");
			 int accountNumber = in.nextInt();
				 position = data.find(accountNumber);



And the program to read the record to find the position.


   public int find(int accountNumber)
		 throws IOException
   {
	  for (int i = 0; i < size(); i++)
	  {
		 file.seek(i * RECORD_SIZE);
		 int a = file.readInt();		 
		 if (a == accountNumber) // Found a match
			return i;
	  } 
	  return -1; // No match in the entire file
   }




I am using a file named Listofaccounts.dat as the fine the programs are going against. Is that correct or is there another file format that I need to use?

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1