8 Replies - 848 Views - Last Post: 04 April 2008 - 05:35 PM Rate Topic: -----

#1 scorpionviolinist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-February 08

Unsure of where to go with this:

Post icon  Posted 04 April 2008 - 03:57 PM

For some reason, I am getting errors in the consolidation and close accounts parts

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************

public class Account
{
  private static int numAccounts;
  private double balance;
  private String name;
  private long acctNum;
  private double newAccount;

  //----------------------------------------------
  //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 account number.
  //----------------------------------------------
  public long getAcctNumber()
  {
	return acctNum;
  }

  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
	return "Name: " + name + 
	"\nAccount Number: " + acctNum +
	"\nBalance: " + balance; 
  }

  //----------------------------------------------
  // Holds number of accounts
  //----------------------------------------------
  private static int numAccounts()
  {
	numAccounts++;
	return numAccounts;
  }

  //----------------------------------------------
  // Returns number of accounts
  //----------------------------------------------
  public static int getnumAccounts()
  {
	return numAccounts;
  }
  
  //----------------------------------------------
  // Closes current account
  //----------------------------------------------
  public void close()
  {
	numAccounts--;
	balance=0;
	name=("CLOSED");
  }


  //-----------------------------------------------
  // Consolidates two accounts into one
  //-----------------------------------------------
  public static Account consolidate(Account acct1, Account acct2)
  {
	if (acct1.getname().compareTo(acct2.getname()) == 0 && acct1.getacctNum().compareTo(acct2.acctNum()) == 0)
	{
	   Account acct3 = new Account(balance, name, acctNum);
	   acct1 = CLOSED;
	   acct2 = CLOSED;
	   return acct3;
	}
	 else{
		System.out.println("Accounts could not be consolidated.");
		return null;
		 }
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Unsure of where to go with this:

#2 scorpionviolinist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-February 08

Re: Unsure of where to go with this:

Posted 04 April 2008 - 04:06 PM

I am supposed to:
Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. (The account number should remain unchanged.) Also decrement the total number of accounts.
and
Add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned. Two important rules of consolidation:

• Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number (you can pick any number).
• Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double your money!
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Unsure of where to go with this:

Posted 04 April 2008 - 04:56 PM

What errors are you getting when you try to compile?
Was This Post Helpful? 0
  • +
  • -

#4 scorpionviolinist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-February 08

Re: Unsure of where to go with this:

Posted 04 April 2008 - 05:15 PM

These are the errors:

C:\Documents and Settings\me>javac Account.java
Account.java:106: cannot find symbol
symbol : method getname()
location: class Account
if (acct1.getname().compareTo(acct2.getname()) == 0 && acct1.getacctNum(
).compareTo(acct2.acctNum()) == 0)
^
Account.java:106: cannot find symbol
symbol : method getname()
location: class Account
if (acct1.getname().compareTo(acct2.getname()) == 0 && acct1.getacctNum(
).compareTo(acct2.acctNum()) == 0)
^
Account.java:106: cannot find symbol
symbol : method acctNum()
location: class Account
if (acct1.getname().compareTo(acct2.getname()) == 0 && acct1.getacctNum(
).compareTo(acct2.acctNum()) == 0)

^
Account.java:106: cannot find symbol
symbol : method getacctNum()
location: class Account
if (acct1.getname().compareTo(acct2.getname()) == 0 && acct1.getacctNum(
).compareTo(acct2.acctNum()) == 0)
^
Account.java:108: non-static variable balance cannot be referenced from a static
context
Account acct3 = new Account(balance, name, acctNum);
^
Account.java:108: non-static variable name cannot be referenced from a static co
ntext
Account acct3 = new Account(balance, name, acctNum);
^
Account.java:108: non-static variable acctNum cannot be referenced from a static
context
Account acct3 = new Account(balance, name, acctNum);
^
Account.java:109: cannot find symbol
symbol : variable CLOSED
location: class Account
acct1 = CLOSED;
^
Account.java:110: cannot find symbol
symbol : variable CLOSED
location: class Account
acct2 = CLOSED;
^
9 errors
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Unsure of where to go with this:

Posted 04 April 2008 - 05:22 PM

You only posted your class in your original post. What is your main() so we can see what you are trying to do?

I believe the function call is getName(). Check for various capitalization/spelling errors

edit: In your code posting there is no getName() function. Were you using the built in java one or your own defined method?

This post has been edited by KYA: 04 April 2008 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#6 scorpionviolinist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-February 08

Re: Unsure of where to go with this:

Posted 04 April 2008 - 05:26 PM

Here is all I have for the program so far... but the class will not compile, so I cannot run it.
//***********************************************************
// TestAccounts1
// A simple program to test the numAccts method of the 
// Account class.  
//***********************************************************
import java.util.Scanner;

public class TestAccounts1
{
	public static void main(String[] args)
	{
	Account acct1, acct2, acct3;
	double balance;
	String name;
	
	Scanner scan = new Scanner(System.in);

	//creates account1
	System.out.println("Please enter account 1 name:");
	name = scan.nextLine();
	acct1 = new Account(100, name, 1111);

	//creates account2
	System.out.println("Please enter account 2 name:");
	name = scan.nextLine();
	acct2 = new Account(100, name, 1112);

	//creates account3
	System.out.println("Please enter account 3 name:");
	name = scan.nextLine();
	acct3 = new Account(100, name, 1113);

	//print summary for both accounts
	System.out.println (acct1);
	System.out.println (acct2);
	System.out.println (acct3);
	}
}
 


Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Unsure of where to go with this:

Posted 04 April 2008 - 05:27 PM

View PostKYA, on 4 Apr, 2008 - 06:22 PM, said:

In your code posting there is no getName() function. Were you using the built in java one or your own defined method?


edit: Your names are mismatching. You defined the getacctNumber() but call getacctNum(). It has to be consistent.

This post has been edited by KYA: 04 April 2008 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 scorpionviolinist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-February 08

Re: Unsure of where to go with this:

Posted 04 April 2008 - 05:33 PM

I am not sure??? I have to write the class to run the program... am I missing that method in my class? Or do I have to import something into my class?
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Unsure of where to go with this:

Posted 04 April 2008 - 05:35 PM

Read what I just posted. You are calling functions that you wrote without using the defined name you gave them Also there is no getName() function defined by you. Since you need its capability in your class I would define one. Correct all the name mismatches as well and it should compile.

edit: You are using variable you have not defined, such as CLOSED.

Closed should be a string right? So,


= "Closed";



This post has been edited by KYA: 04 April 2008 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1