Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: HELP

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP

    ok I am doing a school project and trying to write this code, i need some help. The assignment is include a debit method that will withdraw money from an account. Error proof the debit method so that if the debit amount is either negative or more that the account’s balance and appropriate error message is displayed and the balance is left unchanged. Modifications also need to be made to class AccountTest to test the new debit method. i ALREADY WROTE THE CODE TO ADD MONEY I JUST CANT FIGURE THE CODE TO SUBTRACT /WITHDRAWL MONEY NEED HELP PLEASE.



    package Account;

    // Account class with a constructor to
    // initialize instance variable balance.

    public class Account
    {
    private double _balance; // instance variable that stores the balance

    // constructor
    public Account( double initialBalance )
    {
    // validate that initialBalance is greater than 0.0;
    // if it is not, balance is initialized to the default value 0.0
    if ( initialBalance > 0.0 )
    {
    _balance = initialBalance;
    }
    } // end Account constructor

    // 0credit (add) an amount to the account
    public void credit( double amount )
    {
    _balance = _balance +amount; // add amount to balance

    } // end method credit

    // credit (subtract) an amount to the account

    // return the account balance
    public double getBalance()
    {
    return _balance; // gives the value of balance to the calling method
    } // end method getBalance

    } // end class Account




    AND HERE IS THE TEST


    package Account;

    // Inputting and outputting floating-point numbers with Account objects.
    import java.util.Scanner;

    public class AccountTest
    {
    // main method begins execution of Java application
    public static void main( String args[] )
    {
    // variable declarations
    double depositAmount;

    Scanner input = new Scanner( System.in ); // create Scanner object
    Account myAccount1 = new Account( 100.00 ); // create Account object
    Account myAccount2 = new Account( -9.53 ); // create Account object

    // display initial balance of each object
    System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
    System.out.printf( "account2 balance: $%.2f\n\n", myAccount2.getBalance() );

    // prompt and get deposit amount for account1 and deposit amount
    System.out.print( "Enter deposit amount for account1: " );
    depositAmount = input.nextDouble();
    System.out.printf( "\nadding %.2f to account1 balance\n\n", depositAmount );
    myAccount1.credit( depositAmount );

    // display balances
    System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
    System.out.printf( "account2 balance: $%.2f\n\n", myAccount2.getBalance() );

    // prompt and get deposit amount for account2 and deposit amount
    System.out.print( "Enter deposit amount for account2: " );
    depositAmount = input.nextDouble();
    System.out.printf( "\nadding %.2f to account2 balance\n\n", depositAmount );
    myAccount2.credit( depositAmount );

    // display balances
    System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
    System.out.printf( "account2 balance: $%.2f\n", myAccount2.getBalance() );

    } // end main

    } // end class AccountTest

  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP

    public void debit(double amount) {
    		if (_balance < 0 || _balance < amount)
    			System.out.println("Amount withdrawn is greater than current balance");
    		else 	
    			_balance -= amount;
    	}

    You just need to update your test class.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP

    ok i still need help how do i differenciate deposit or withdrawl ??? and i still dont know how to update my test . I am a newbie still learning take it slow please thanks for being patient.
    Last edited by jaisan72980; February 11th, 2011 at 04:53 PM.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP

    Wouldn't it be enough to prompt the user that they're making either a deposit or a withdrawal? For example:

    1. Prompt to deposit to Account 1
    2. Prompt to deposit to Account 2
    3. Prompt to withdraw from Account 1
    4. Prompt to withdraw from Account 2

    Of course, you can always try and do some pseudo-menu options in your test class.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP

    yes that is what i would like to do but ive never written code like that before and ive only been doing this for 2 weeks i need some help on how to do that and help on how to do it for my test class also this assignment is already late because of my military obligation i truly am lost and in desperate need of assistance

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP

    package Account;
     
    // Inputting and outputting floating-point numbers with Account objects.
    import java.util.Scanner;
     
    public class AccountTest {
    	public static void main(String args[]) {
    		double depositAmount;
    		int option;
     
    		Scanner input = new Scanner(System.in); // create Scanner object
    		Account myAccount1 = new Account(100.00); // create Account object
    		Account myAccount2 = new Account(-9.53); // create Account object
     
    		System.out.printf("account1 balance: $%.2f\n", myAccount1.getBalance());
    		System.out.printf("account2 balance: $%.2f\n\n",
    				myAccount2.getBalance());
     
    loop:	while (true) {
    			System.out.println("1.) Deposit to account 1"); 
    			System.out.println("2.) Deposit to account 2");
    			System.out.println("3.) Withdraw from account 1");
    			System.out.println("4.) Withdraw from account 2");
    			System.out.println("5.) Exit");
    			System.out.print("Enter option: ");
    			option = input.nextInt();
     
    			switch(option) {
    			case 1: 
    				System.out.print("Enter deposit amount for account1: ");
    				depositAmount = input.nextDouble();
    				System.out.printf("\nadding %.2f to account1 balance\n\n",
    						depositAmount);
    				myAccount1.credit(depositAmount);
    				break;
    			case 2:
    				System.out.print("Enter deposit amount for account2: ");
    				depositAmount = input.nextDouble();
    				System.out.printf("\nadding %.2f to account2 balance\n\n",
    						depositAmount);
    				myAccount2.credit(depositAmount);
    				break;
    			case 3:
    				System.out.print("Enter withdrawal amount for account1: ");
    				depositAmount = input.nextDouble();
    				System.out.printf("\nsubtracting %.2f from account1 balance\n\n",
    						depositAmount);
    				myAccount1.debit(depositAmount);
    				break;
    			case 4:
    				System.out.print("Enter withdrawal amount for account2: ");
    				depositAmount = input.nextDouble();
    				System.out.printf("\nsubtracting %.2f from account2 balance\n\n",
    						depositAmount);
    				myAccount2.debit(depositAmount);
    				break;
    			case 5: break loop;
    			default : continue;
    			}
     
    			System.out.printf("account1 balance: $%.2f\n", myAccount1.getBalance());
    			System.out.printf("account2 balance: $%.2f\n\n",
    					myAccount2.getBalance());
    		}
    	}
    }