Thursday, December 15, 2011

JAVA program help! Please

I'm suppose to create a program using BankAccount and BankAccountTester classes, and am suppose to be able to create an account with a specified opening balance, make bank deposits, withdrawls and check balance.





My program compiles correctly, but it doesn't run, I am not sure what else I need to do to make sure that it runs correctly and accurately does all four tasks?








This is what I have so far:





public class BankAccount {





double balance;


String name;





public BankAccount( String name, double balance ) {


name = name;


balance = balance;


}





public void deposit(double amount) {


balance *= amount;


}





public void withdraw( double amount ) {


balance -= amount;


}





public void setBalance( double b ) {


b = balance;


}





public double getBalance() {


return balance;


}





public String toString() {


return "Name: " name " " balance;


}


}




















import javax.swing.*;





public class BankAccountTester {





public static final int QUIT = 0;


public static final int DEPOSIT = 1;


public static final int WITHDRAW = 2;


public static final int INQUIRY = 3;





public static void main(String[] s) {





String lName = JOptionPane.showInputDialog( "Enter last name" );


String fName = JOptionPane.showInputDialog( "Enter first name" );


double openingBalance = Double.parseDouble( JOptionPane.showInputDialog( "Enter opening balance" ) );


String name = lName ", " fName;


BankAccount a = new BankAccount ( name, openingBalance );





Object[] options = {"Quit", "Deposit", "Withdraw", "Balance Inquiry"};


int chosenOption = 999; // A garbage value to get me into the while loop the first time.





do {





chosenOption = JOptionPane.showOptionDialog( null, "What transaction would you like to do?", "Transaction",


JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0] );





switch ( chosenOption ) {


case QUIT:


// Do nothing, will fall out of loop;


break;





case DEPOSIT:


double dep = Double.parseDouble( JOptionPane.showInputDialog( "Enter Amount of Deposit" ) );


a.deposit( dep );





case WITHDRAW:


double wd = Double.parseDouble( JOptionPane.showInputDialog( "Enter Amount of Withdrawal" ) );


a.deposit( wd );


break;





case INQUIRY:


JOptionPane.showMessageDialog( null, "Balance = " a.getBalance() );


break;





default:


JOptionPane.showMessageDialog( null, "Invalid Option" );


break;


}








} while ( chosenOption %26gt; 0 );





}


}








please help


thanks|||Hello,





This code here will not compile. Why do you think that it does?





Here are three errors my compiler found, all are string concatenation mistakes:


"Balance = " a.getBalance() should be: ("Balance = "+a.getBalance())





String name = lName ", " fName; should be: String name = lName+", "+fName;





return "Name: " name " " balance; should be: return "Name: "+name+" "+balance;





Remember, use the '+' sign to concat (aka combine) strings.|||your first code uses 2 variables with the same name


public BankAccount( String name, double balance ) {


name = name;


balance = balance;


}


either change parameter name or use this.name=name


also method deposit - need + not *


public void deposit(double amount) {


balance *= amount;


}

No comments:

Post a Comment