I'm taking a Java Class and this is the assignment:
"..Modify your withdraw method so that it that checks the customers balance against a potential withdrawal. If the amounted requested for withdrawal exceeds the balance, deny the attempt to withdraw money and let the customer know."
I have the if statement in two places because I was trying to see where it would work, but I am obviously clueless and need some help. Thanks :)
import java.text.NumberFormat;
import javax.swing.JOptionPane;
public class BankingU4 {
double balance;
double withdraw;
double deposit;
double firstname;
double lastname;
NumberFormat dollar = NumberFormat.getCurrencyInstance();//Currency
public BankingU4(double init){
balance = init;
}
public void withdraw(double x1, double x3) {
{
if (withdraw >= balance)
System.out.println("Withdrawal can't be completed.");
else {
balance = balance - x1-x3;
}}
}
public void deposit(double x2, double x4){
balance = balance + x2+x4;
}
public double value () {
return balance;
}
public void output(double balance2) {
String lastname = JOptionPane.showInputDialog("Enter Customer's Last Name:");
String firstname = JOptionPane.showInputDialog("Enter Customer's First Name:");
System.out.println("Customer [" + lastname + ", " + firstname + "] has an initial balance of " +dollar.format(balance2));
}
public static void main(String[] args) {
double balance1 = 0;
double n1, n2, n3, n4;
double v1;
String n1Val, n2Val, n3Val, n4Val;
String v1Val;
v1Val = JOptionPane.showInputDialog("Enter initial balance:");
n1Val = JOptionPane.showInputDialog("Enter first withdrawal amount:");
n2Val = JOptionPane.showInputDialog("Enter first deposit amount:");
n3Val = JOptionPane.showInputDialog("Enter second withdrawal amount:");
n4Val = JOptionPane.showInputDialog("Enter second deposit amount:");
v1 = Double.parseDouble(v1Val);
n1 = Double.parseDouble(n1Val);
n2 = Double.parseDouble(n2Val);
n3 = Double.parseDouble(n3Val);
n4 = Double.parseDouble(n4Val);
BankingU4 stats = new BankingU4(v1);
System.out.println("For an account with an initial balance of " +v1Val);
{
if (n1Val.compareTo(v1Val) < 0)
System.out.println("Withdrawal can't be completed.");
else {
System.out.println("First Withdrawal= " +n1Val);
{
if (n2Val.compareTo(v1Val)==0)
System.out.println("Withdrawal can't be completed.");
else {
System.out.println("Second Withdrawal= " +n2Val);
System.out.println("First Deposit= " +n3Val);
System.out.println("Second Deposit= " +n4Val);
stats.withdraw(n1, n3);
stats.deposit(n2, n4);
balance1 = stats.value();
stats.output(balance1);
}}
}}}
}