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);

    }}
}}}

}

Recommended Answers

All 2 Replies

Well, first of all, your withdraw method is wrong. You have:

if (withdraw >= balance)

but you haven't initialised the withdraw variable. You don't even use it.

Okay, put that all aside for a bit.

Make a class, call it "IfStatements".

In that class, translate the following statements into java, and print the responses to the console, using System.out.println()

if 1 is greater than 2, print "In union there is strength"
if 2 is greater than 1, print "There is strength in numbers"
if "red" is equal to ( String.equals() ) "green" print "I am colorblind".
if ((a+b+c) = (c + a + b)) and (((a +b) +c) = (a +(b+c))) print "Addition is commutative" and also print "Addition is associative", as two separate instructions, or else print "Math just broke." (you'll have to give values to a, b, and c)

If you can do those, you have no trouble with if statements, and your trouble is elsewhere.


Now as Akill10 says, you do have some problems with your methods. withdraw() has two parameters whose names tell me nothing about what they are. You don't refer to either of them, instead you refer to a nonexistent "withdraw" variable.
Fixing this will help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.