12 Replies - 63168 Views - Last Post: 06 November 2009 - 01:44 PM Rate Topic: -----

#1 Thenextneo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-December 07

programming a bank account

Post icon  Posted 13 February 2008 - 06:27 AM

Alright, this problem that I am encountering right now is that the Account class below, is being called and a method inside is being called to the Bank class. For a reason not known to me, it is rejecting it saying that a non static variable cannot be referenced to a static variable. Can anyone help with this because I have been working for a day and cant seem to find the source of the problem.

import java.util.Scanner; import java.text.NumberFormat;

public class Bank
{
	public static void main(String[] args) 
	{
	 Account munozAccount = new Account (250, "Maria", "Munoz", "110 Glades Road","My town", "FL", "33445"); 
	 Scanner input = new Scanner(System.in);
	 double data;
	 NumberFormat money = NumberFormat.getCurrencyInstance();

	 System.out.println(munozAccount);

	 System.out.print ( "Enter deposit amount: ");
	 data = input.nextDouble();
	 munozAccount.deposit(data);
	 System.out.println ( "Balance is: " + money.format(munozAccount.getBalance()));

	 System.out.print ("Enter withdrawal amount: ");
	 data = input.nextDouble();
	 munozAccount.withdrawal (data);
	 System.out.println ( "Balance is: " + money. format(munozAccount.getBalance()));
	}


public class Account
{ 
	private double balance; 
	private Customer cust;

	/**
	* constructor
	* * pre: none
	* * * post: An account cree * customer data initia: */
	public void Account(double bal, String fName, String lName,  String str, String city, String st, String zip)
	{
		balance = bal;
		cust = new Customer(fName, lName, str, city, st, zip);
	}

/**
* Returns the current balance
* pre: none
* post: The account balance has been returned
 */
public double getBalance()
{
	return (balance);
}

/**
* A deposit is made to the account
* pre: none
* post: The balance has been increased by the amount of the deposit.
 */
public void deposit(double amt)
{
	balance += amt;
}



/**
* A withdrawal is made from the account if there is enough money
* pre: none
* post: The balance has been decreased by the amount withdrawn.
 */
public void withdrawal(double amount)
{
   if (amt <= balance) 
	balance -= amt;
else 
	System.out.println("Not enough money in account.");
}
/**
* Returns a String that represents the Account object. 
* pre: none
* post: A string representing the Account object has been returned.
*/
public String toString()
{
	String accountString;
	NumberFormat money = NumberFormat.getCurrencyInstance();

accountString = cust.toString();
accountString += "Current balance is " + money.format (balance);
return (accountString);
}

// The Customer class is implemented below

public class Customer 
{
	private String firstName, lastName, street, city,state, zip_;

/**
* constructor
* pre: none
* post: A Customer object has been created.
* Customer data has been initialized with parameters
*/
public Customer(String fName, String lName, String str,
String c, String s, String z) 
{
  firstName = fName;
  last Name = lName;
  street = str;

  city = c; 
  state = s; 
  zip = z;
}

/** 
* Returns a String that represents the Customer Object 
* pre: none
* post: A string representing the Account object has been returned.
*/
public String toString() 
{ 
String custString;

custString=firstName + " " + lastName + "\n";
custString +=street + "\n";
custString += city + ", "+state+ "  " + zip + "\n";

Return(custString);
}
}
}
}



Is This A Good Question/Topic? 1
  • +

Replies To: programming a bank account

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: programming a bank account

Posted 13 February 2008 - 06:40 AM

public class Bank
	{
		public static void main(String[] args)
		{
		 Bank bank1 = new Bank();
		 Account munozAccount = bank1.new Account (250, "Maria", "Munoz", "110 Glades Road","My town", "FL", "33445");
		 Scanner input = new Scanner(System.in);
		 double data;
		 NumberFormat money = NumberFormat.getCurrencyInstance();

		 System.out.println(munozAccount);

		 System.out.print ( "Enter deposit amount: ");
		 data = input.nextDouble();
		 munozAccount.deposit(data);
		 System.out.println ( "Balance is: " + money.format(munozAccount.getBalance()));

		 System.out.print ("Enter withdrawal amount: ");
		 data = input.nextDouble();
		 munozAccount.withdrawal (data);
		 System.out.println ( "Balance is: " + money. format(munozAccount.getBalance()));
		}


	public class Account
	{
		private double balance;
		private Customer cust;

		/**
		* constructor
		* * pre: none
		* * * post: An account cree * customer data initia: */
		public Account(double bal, String fName, String lName,  String str, String city, String st, String zip)
		{
			balance = bal;
			cust = new Customer(fName, lName, str, city, st, zip);
		}

	/**
	* Returns the current balance
	* pre: none
	* post: The account balance has been returned
	*/
	public double getBalance()
	{
		return (balance);
	}

	/**
	* A deposit is made to the account
	* pre: none
	* post: The balance has been increased by the amount of the deposit.
	*/
	public void deposit(double amt)
	{
		balance += amt;
	}



	/**
	* A withdrawal is made from the account if there is enough money
	* pre: none
	* post: The balance has been decreased by the amount withdrawn.
	*/
	public void withdrawal(double amount)
	{
	   if (amount <= balance)
		balance -= amount;
	else
		System.out.println("Not enough money in account.");
	}
	/**
	* Returns a String that represents the Account object.
	* pre: none
	* post: A string representing the Account object has been returned.
	*/
	public String toString()
	{
		String accountString;
		NumberFormat money = NumberFormat.getCurrencyInstance();

	accountString = cust.toString();
	accountString += "Current balance is " + money.format (balance);
	return (accountString);
	}

//	 The Customer class is implemented below

	public class Customer
	{
		private String firstName, lastName, street, city,state, zip;

	/**
	* constructor
	* pre: none
	* post: A Customer object has been created.
	* Customer data has been initialized with parameters
	*/
	public Customer(String fName, String lName, String str,
	String c, String s, String z)
	{
	  firstName = fName;
	  lastName = lName;
	  street = str;

	  city = c;
	  state = s;
	  zip = z;
	}

	/**
	* Returns a String that represents the Customer Object
	* pre: none
	* post: A string representing the Account object has been returned.
	*/
	public String toString()
	{
	String custString;

	custString=firstName + " " + lastName + "\n";
	custString +=street + "\n";
	custString += city + ", "+state+ "  " + zip + "\n";

	return(custString);
	}
	}
	}
	}



There were many syntax errors. I removed them. Also if you want to use a child-class you need to have a instance of the parent class before you can do that. So check the first two lines of this code with your code in the main method.

Also try the inputs : negative deposit amount and negative withdrawal amount.

More help? Keep Posting Questions! :)

This post has been edited by letthecolorsrumble: 13 February 2008 - 06:56 AM

Was This Post Helpful? 0
  • +
  • -

#3 Thenextneo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 04-December 07

Re: programming a bank account

Posted 13 February 2008 - 03:04 PM

Thanks. I just had to find out what I was getting wrong. I see what you edited from the original code.

Also, what do you mean about a child class and a parent class. I not sure that is included in the asignment but it would be nice to know.
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: programming a bank account

Posted 13 February 2008 - 03:11 PM

View PostThenextneo, on 13 Feb, 2008 - 03:04 PM, said:

Thanks. I just had to find out what I was getting wrong. I see what you edited from the original code.

Also, what do you mean about a child class and a parent class. I not sure that is included in the asignment but it would be nice to know.



A class defined inside another class is called a child class. That is the case with your coding. I do not know if you meant it to be that way.

More help? Keep your posts coming! :)

This post has been edited by letthecolorsrumble: 13 February 2008 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 erguvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-October 09

Re: programming a bank account

Posted 22 October 2009 - 10:27 AM

[quote name='letthecolorsrumble' date='13 Feb, 2008 - 05:40 AM' post='309315']
[code]

Dear "letthecolorsrumble"
I found your reply on bank account quite impressive.I have been trying to understand the logic.
Therefore I want to add an extra specification into this bank account program.
Let's assume there is a Transaction class.(as you said a child class)
strating with
public class Transaction
{


..but there should be some private operations like;
// account for which a transaction is required
// amount of money involved in that transaction (deposit or withdrawal)
// constructor(s)

and also
public void process() // method to process a transaction which is non-private

}

How would you implement this Transaction class.
Make sure we can only see two digits customer number (11 or 22) instead of customer name in this section

I would be very happy if you could add this class with explanations
Kind regards
ERGUVAN
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: programming a bank account

Posted 22 October 2009 - 11:00 AM

Firstly, please dont revive year old post. Secondly, do you have a specific question other than do this for me? No one will do your work for you. You can ask questions about specific things, and help will be offered. Please state a proper question.
Was This Post Helpful? 0
  • +
  • -

#7 eazyigz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 09

Re: programming a bank account

Posted 06 November 2009 - 10:22 AM

View Postletthecolorsrumble, on 13 Feb, 2008 - 05:40 AM, said:

public class Bank
	{
		public static void main(String[] args)
		{
		 Bank bank1 = new Bank();
		 Account munozAccount = bank1.new Account (250, "Maria", "Munoz", "110 Glades Road","My town", "FL", "33445");
		 Scanner input = new Scanner(System.in);
		 double data;
		 NumberFormat money = NumberFormat.getCurrencyInstance();

		 System.out.println(munozAccount);

		 System.out.print ( "Enter deposit amount: ");
		 data = input.nextDouble();
		 munozAccount.deposit(data);
		 System.out.println ( "Balance is: " + money.format(munozAccount.getBalance()));

		 System.out.print ("Enter withdrawal amount: ");
		 data = input.nextDouble();
		 munozAccount.withdrawal (data);
		 System.out.println ( "Balance is: " + money. format(munozAccount.getBalance()));
		}


	public class Account
	{
		private double balance;
		private Customer cust;

		/**
		* constructor
		* * pre: none
		* * * post: An account cree * customer data initia: */
		public Account(double bal, String fName, String lName,  String str, String city, String st, String zip)
		{
			balance = bal;
			cust = new Customer(fName, lName, str, city, st, zip);
		}

	/**
	* Returns the current balance
	* pre: none
	* post: The account balance has been returned
	*/
	public double getBalance()
	{
		return (balance);
	}

	/**
	* A deposit is made to the account
	* pre: none
	* post: The balance has been increased by the amount of the deposit.
	*/
	public void deposit(double amt)
	{
		balance += amt;
	}



	/**
	* A withdrawal is made from the account if there is enough money
	* pre: none
	* post: The balance has been decreased by the amount withdrawn.
	*/
	public void withdrawal(double amount)
	{
	   if (amount <= balance)
		balance -= amount;
	else
		System.out.println("Not enough money in account.");
	}
	/**
	* Returns a String that represents the Account object.
	* pre: none
	* post: A string representing the Account object has been returned.
	*/
	public String toString()
	{
		String accountString;
		NumberFormat money = NumberFormat.getCurrencyInstance();

	accountString = cust.toString();
	accountString += "Current balance is " + money.format (balance);
	return (accountString);
	}

//	 The Customer class is implemented below

	public class Customer
	{
		private String firstName, lastName, street, city,state, zip;

	/**
	* constructor
	* pre: none
	* post: A Customer object has been created.
	* Customer data has been initialized with parameters
	*/
	public Customer(String fName, String lName, String str,
	String c, String s, String z)
	{
	  firstName = fName;
	  lastName = lName;
	  street = str;

	  city = c;
	  state = s;
	  zip = z;
	}

	/**
	* Returns a String that represents the Customer Object
	* pre: none
	* post: A string representing the Account object has been returned.
	*/
	public String toString()
	{
	String custString;

	custString=firstName + " " + lastName + "\n";
	custString +=street + "\n";
	custString += city + ", "+state+ "  " + zip + "\n";

	return(custString);
	}
	}
	}
	}



There were many syntax errors. I removed them. Also if you want to use a child-class you need to have a instance of the parent class before you can do that. So check the first two lines of this code with your code in the main method.

Also try the inputs : negative deposit amount and negative withdrawal amount.

More help? Keep Posting Questions! :)


This was a very good post, good coding style.
Was This Post Helpful? 0
  • +
  • -

#8 eazyigz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 09

Re: programming a bank account

Posted 06 November 2009 - 10:29 AM

View PostThenextneo, on 13 Feb, 2008 - 02:04 PM, said:

Thanks. I just had to find out what I was getting wrong. I see what you edited from the original code.

Also, what do you mean about a child class and a parent class. I not sure that is included in the asignment but it would be nice to know.


A very good source on nested type declarations I found is Addison Wesley's A Programmer's Guide to Java SCJP Certifications, 3rd Edition, 2008. Chapter 8 of that book discusses in detail topics such as "Static Member Types," "Non-Static Member Classes," "Local Classes," and "Anonymous Classes."
Was This Post Helpful? 0
  • +
  • -

#9 eazyigz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 09

Re: programming a bank account

Posted 06 November 2009 - 01:31 PM

Quote

Dear "letthecolorsrumble"
I found your reply on bank account quite impressive.I have been trying to understand the logic.
Therefore I want to add an extra specification into this bank account program.
Let's assume there is a Transaction class.(as you said a child class)
strating with
public class Transaction
{


..but there should be some private operations like;
// account for which a transaction is required
// amount of money involved in that transaction (deposit or withdrawal)
// constructor(s)

and also
public void process() // method to process a transaction which is non-private

}

How would you implement this Transaction class.
Make sure we can only see two digits customer number (11 or 22) instead of customer name in this section

I would be very happy if you could add this class with explanations
Kind regards
ERGUVAN



Dear ERG UVAN,
here is how I would implement this Transaction class:


 /**
 * The following class specifies which account
 * a transaction (deposit/withdraw) should occur in.
 * Also, it prints the 2-digit account number instead
 * of the customer name.  
 * Provides a process() method to handle transactions.
 * @author Igor Ganapolsky
*
 */
public class Transaction {

	private int accountNumber;
	Account munozAccount;

	public Transaction(int accountNumber){
		this.accountNumber = accountNumber;
		if(this.accountNumber==11)
			munozAccount = new Account (250, this.accountNumber, "110 Glades Road","My town", "FL", "33445");
		//print the account details
		System.out.println(munozAccount);
	}

	/**
	 * Returns the current balance
	 * pre: none
	 * post: The account balance has been returned
	 */
	public double getBalance()
	{
		return munozAccount.getBalance();
	}

	/*
	 * Processes the transaction.
	 * Parameter - Transact enum.
	 */
	public void process(Transact transactionType,double data){
		if(transactionType.equals(Transact.DEPOSIT)){
			munozAccount.deposit(data);
		}
		else if(transactionType.equals(Transact.WITHDRAWAL)){
			munozAccount.withdrawal(data);
		}
	}
}



The main method, as well as Account constructor, and Customer class need to be modified as well. So I attached the full program.

Attached File(s)

  • Attached File  Bank.txt (4.2K)
    Number of downloads: 720

This post has been edited by eazyigz: 06 November 2009 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

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

Re: programming a bank account

Posted 06 November 2009 - 01:35 PM

Can we not revive dead topics please? Don't make me close the thread. ;)
Was This Post Helpful? 1
  • +
  • -

#11 eazyigz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 09

Re: programming a bank account

Posted 06 November 2009 - 01:40 PM

View PostLocke, on 6 Nov, 2009 - 12:35 PM, said:

Can we not revive dead topics please? Don't make me close the thread. ;)


Hi, thank you for your concern. The reason that I posted the reply is that I found this user's program request to be of interest to many viewers who will stumble upon this post. Hence they will be able to see my solution and judge for themselves. I understand your logic on reviving old posts, but if this is an ongoing discussion then how else would viewers be able to follow the entire post?
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: programming a bank account

Posted 06 November 2009 - 01:40 PM

Tabboo, had to be asked twice not ro do that in the same thread!!
Also...

Please do not attach files that may be harmful to other users. All files may be harmful so what im trying to say, don't attach them. :)

Instead, post the code using the code tags and that way no one can get viruses bugs ect.

:code:

Happy Coding!

Edit - I would hope the users make their own thread so that we may help them figure out their problems instead of them saying "OOOh! code is already fixed here Copy and paste and done!" Education is purpose here.

This post has been edited by Fuzzyness: 06 November 2009 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

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

Re: programming a bank account

Posted 06 November 2009 - 01:44 PM

View Posteazyigz, on 6 Nov, 2009 - 02:40 PM, said:

View PostLocke, on 6 Nov, 2009 - 12:35 PM, said:

Can we not revive dead topics please? Don't make me close the thread. ;)


Hi, thank you for your concern. The reason that I posted the reply is that I found this user's program request to be of interest to many viewers who will stumble upon this post. Hence they will be able to see my solution and judge for themselves. I understand your logic on reviving old posts, but if this is an ongoing discussion then how else would viewers be able to follow the entire post?


I understand your logic as well. However, the fact still remains that, more often than not, the problem has been solved in a way almost exclusive to the implementation that the creator of the thread used. Sometimes, the searches are almost futile for the specific needs of the searcher's assignment/task. You could be looking at 2 COMPLETELY different ways of doing it, and if the searcher's way is supposed to be different than the thread solution...then the help is fairly useless.

I understand your position, but please understand mine. Every assignment is unique in its own way.

This post has been edited by Locke: 06 November 2009 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1