Results 1 to 4 of 4
Thread: New to the forum
- 08-15-2007, 05:48 AM #1
Member
- Join Date
- Aug 2007
- Posts
- 5
- Rep Power
- 0
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 05:58 AM. Reason: Added more code...
- 08-15-2007, 09:13 AM #2
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:
Comments in code below show how these were fixed.Java 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
I changed the names of the classes so you can run this as–is.
Java 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; } } }
- 08-15-2007, 03:16 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 5
- Rep Power
- 0
Thanks a lot for the help on this. I will try to run this!
- 08-15-2007, 11:46 PM #4
Member
- Join Date
- Aug 2007
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
New to the Forum
By srock258 in forum IntroductionsReplies: 3Last Post: 11-03-2008, 05:19 AM -
New sub forum
By tim in forum Suggestions & FeedbackReplies: 3Last Post: 03-17-2008, 08:12 PM -
Welcome to our new forum: Forum Lobby
By JavaForums in forum Forum LobbyReplies: 18Last Post: 02-07-2008, 05:40 AM -
To correct forum
By Jman in forum IntroductionsReplies: 3Last Post: 01-18-2008, 02:33 AM -
New to the Forum
By JavaJunkie1983 in forum New To JavaReplies: 7Last Post: 01-14-2008, 06:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks