Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-15-2007, 06:48 AM
Member
 
Join Date: Aug 2007
Posts: 5
Try2Live4God is on a distinguished road
New to the forum and with a question!!
Just dropping by to say hello. I am taking a Java class this semester and may be needing help as I am a newb when it comes to Java.

My professor gave an assignment to create a banking program and i'm running into trouble. The program is basically to prompt the user to enter an option to either deposit, withdraw, view balance or terminate the session. I have to use a switch statement in the while loop. The customer class is where all the calculations take place. Here is what I have so far.


import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

class Customer
{
//attributes
String customerName;
int customerAcct;
double accountBalance, totalDeposits, totalWithdrawals;

//methods
void processDeposit(double depositAmount)
{
accountBalance += depositAmount;
totalDeposits +=depositAmount;
}


boolean processWithdrawal(double withdrawalAmount)
{
if (withdrawalAmount <= accountBalance)
{
accountBalance -= withdrawalAmount;
totalWithdrawals += withdrawalAmount;
return true;
}
else
{
String errorMessage = "Insufficient Funds!! ";
errorMessage += "Your current balance is " + accountBalance;

JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}


//constructor
Customer()
{
customerName = JOptionPane.showInputDialog("Please enter the Customer's name");
customerAcct = Integer.parseInt(JOptionPane.showInputDialog("Plea se enter the Customer's Account Number"));
accountBalance = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter the starting balance"));
totalDeposits= 0;
totalWithdrawals=0;
}

}

public class Banking
{
public Banking() {


public static void main(String[] args) {

//Housekeeping
int depCount = 0, withdrawalCount = 0, choices;
double startingBalance = 0, deposit = 0, withdrawal = 0;
boolean validWithdrawal = true;

DecimalFormat twoDigits = new DecimalFormat("0.00");

//Processing
Customer cust = new Customer(); //Instantiate customer object

startingBalance = cust.accountBalance; //populates the starting balance field with accountBalance from customer class


choices = Integer.parseInt(JOptionPane.showInputDialog("Menu :\nEnter 1 - For Deposit\nEnter 2 - For Withdrawal\nEnter3 - For Balance\nEnter 9 - To Terminate\n"));


while (choices != 9)
{

switch(choices)
{
case 1:
deposit = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter a deposit amount."));
cust.processDeposit(deposit);
break;

case 2:

withdrawal = Double.parseDouble(JOptionPane.showInputDialog("Pl ease enter a withdrawal."));
cust.validWithdrawal(withdrawal);
break;

case 3:

JOptionPane.showMessageDialog("Your remaining balance is:" + cust.accountBalance);
break;


default:
JOptionPane.showMessageDialog(null,output,"not valid choice",JOptionPane.INFORMATION_MESSAGE);

}

}


String output = "Customer Name : " + cust.customerName + "\n";
output += "Customer Account # : " + cust.customerAcct + "\n";
output += "Starting Balance : " + twoDigits.format(startingBalance) + "\n\n";
output += "Total Deposit : " + twoDigits.format(cust.totalDeposits) + "\n";
output += "Total Withdrawal : " + twoDigits.format(cust.totalWithdrawals) + "\n\n";
output += "Closing Balance : " + twoDigits.format(cust.accountBalance) + "\n";

JOptionPane.showMessageDialog(null, output, "Transaction Summary", JOptionPane.INFORMATION_MESSAGE);

//System.exit(0);

}
}

}


It still seems to not work.....any help would be appreciated. Thanks!

Last edited by Try2Live4God : 08-15-2007 at 06:58 AM. Reason: Added more code...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-15-2007, 10:13 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,015
hardwired is on a distinguished road
The main method does not go inside any other method or constructor.
It stands by itself as a method inside a class body, viz, the curley braces {}.
Compile errors:
Code:
C:\jexp>javac bankingrx.java bankingrx.java:46: cannot find symbol symbol : method validWithdrawal(double) location: class CustomerRx cust.validWithdrawal(withdrawal); ^ bankingrx.java:49: cannot find symbol symbol : method showMessageDialog(java.lang.String) location: class javax.swing.JOptionPane JOptionPane.showMessageDialog("Your remaining balance is:" + ^ bankingrx.java:53: cannot find symbol symbol : variable output location: class BankingRx JOptionPane.showMessageDialog(null,output,"not valid choice" , ^ 3 errors
Comments in code below show how these were fixed.
I changed the names of the classes so you can run this as–is.
Code:
import java.io.*; import javax.swing.JOptionPane; import java.text.DecimalFormat; public class BankingRx { public BankingRx() {} public static void main(String[] args) { //Housekeeping int depCount = 0, withdrawalCount = 0, choices; double startingBalance = 0, deposit = 0, withdrawal = 0; boolean validWithdrawal = true; DecimalFormat twoDigits = new DecimalFormat("0.00"); //Processing CustomerRx cust = new CustomerRx(); //Instantiate customer object // populates the starting balance field with // accountBalance from customer class startingBalance = cust.accountBalance; do { choices = Integer.parseInt( JOptionPane.showInputDialog("Menu :\n" + "Enter 1 - For Deposit\n" + "Enter 2 - For Withdrawal\n" + "Enter3 - For Balance\n" + "Enter 9 - To Terminate\n")); switch(choices) { case 1: deposit = Double.parseDouble( JOptionPane.showInputDialog("Pl ease enter " + "a deposit amount.")); cust.processDeposit(deposit); break; case 2: withdrawal = Double.parseDouble( JOptionPane.showInputDialog("Pl ease enter " + "a withdrawal.")); // CustomerRx class does not have this method. // cust.validWithdrawal(withdrawal); break; case 3: // JOptionPane class does not have a method that takes // these type of arguments. // JOptionPane.showMessageDialog("Your remaining balance is:" + // cust.accountBalance); // Try this JOptionPane.showMessageDialog(null,"Your remaining balance " + "is:" + cust.accountBalance); break; default: // Variable "output" has not been declared. String output = "hello world"; JOptionPane.showMessageDialog(null,output,"not valid choice", JOptionPane.INFORMATION_MESSAGE); } String output = "Customer Name : " + cust.customerName + "\n"; output += "Customer Account # : " + cust.customerAcct + "\n"; output += "Starting Balance : " + twoDigits.format(startingBalance) + "\n\n"; output += "Total Deposit : " + twoDigits.format(cust.totalDeposits) + "\n"; output += "Total Withdrawal : " + twoDigits.format(cust.totalWithdrawals) + "\n\n"; output += "Closing Balance : " + twoDigits.format(cust.accountBalance) + "\n"; JOptionPane.showMessageDialog(null, output, "Transaction Summary", JOptionPane.INFORMATION_MESSAGE); // System.exit(0); } while (choices != 9); } } class CustomerRx { // member variables in class scope String customerName; int customerAcct; double accountBalance, totalDeposits, totalWithdrawals; //constructor CustomerRx() { customerName = JOptionPane.showInputDialog("Please enter the Customer's name"); customerAcct = Integer.parseInt( JOptionPane.showInputDialog("Plea se enter the " + "Customer's Account Number")); accountBalance = Double.parseDouble( JOptionPane.showInputDialog("Pl ease enter the " + "starting balance")); totalDeposits= 0; totalWithdrawals=0; } //methods void processDeposit(double depositAmount) { accountBalance += depositAmount; totalDeposits +=depositAmount; } boolean processWithdrawal(double withdrawalAmount) { if (withdrawalAmount <= accountBalance) { accountBalance -= withdrawalAmount; totalWithdrawals += withdrawalAmount; return true; } else { String errorMessage = "Insufficient Funds!! "; errorMessage += "Your current balance is " + accountBalance; JOptionPane.showMessageDialog(null, errorMessage, "Error", JOptionPane.ERROR_MESSAGE); return false; } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-15-2007, 04:16 PM
Member
 
Join Date: Aug 2007
Posts: 5
Try2Live4God is on a distinguished road
Thanks a lot for the help on this. I will try to run this!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-16-2007, 12:46 AM
Member
 
Join Date: Aug 2007
Posts: 5
Try2Live4God is on a distinguished road
Hey Hardwired,

It worked! Thanks a bunch for your help!

And out of curiosity.....is this program possible without a do while loop? Would a regular while loop have worked like I had in my original code??
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
New sub forum tim Suggestions & Feedback 3 03-17-2008 09:12 PM
Welcome to our new forum: Forum Lobby JavaForums Forum Lobby 18 02-07-2008 06:40 AM
New to the Forum srock258 Introductions 2 01-26-2008 07:37 AM
To correct forum Jman Introductions 3 01-18-2008 03:33 AM
New to the Forum JavaJunkie1983 New To Java 7 01-14-2008 07:07 AM


All times are GMT +3. The time now is 03:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org