0

所以我对java还是很陌生。我已经写了这个,但不确定为什么当我运行它时,金额不会添加到客户端中设置的内容中。

例如:我输入 500 作为起始余额,如果我点击存款,然后输入 500,然后点击案例 3,它应该说 1000 但它仍然是 500。然后如果我点击提款,它说我有 -500。

有任何想法吗?谢谢

package bankaccount;
import java.util.Random;
import java.util.Scanner;

public class Client {
 public static void main(String args[]) {
     Scanner input = new Scanner(System.in);       
     System.out.println("Enter your Name: ");
            String cusName = input.nextLine();
            Random randomGenerator = new Random();
            int accNo = randomGenerator.nextInt(100000);
            System.out.println("Enter Initial Balance: ");
            int balance = input.nextInt();
            BankAccount b1 = new BankAccount(cusName, accNo, balance);
            int menu;
            System.out.println("Menu");
            System.out.println("1. Deposit Amount");
            System.out.println("2. Withdraw Amount");
            System.out.println("3. Display Information");
            System.out.println("4. Exit");
            boolean quit = false;
            do {
                    System.out.print("Please enter your choice: ");
                    menu = input.nextInt();
                    switch (menu) {
                    case 1:            
                             System.out.print("Enter depost amount:");
                             Money.amount = input.nextInt(); 
                             b1.getDeposit(); 

                            break;

                    case 2:
                             System.out.println("Current Account Balance=" + b1.getBalance());
                             System.out.print("Enter withdrawal amount:");
                             Money.amount = input.nextInt();
                             b1.getWithdraw();

                            break;

                    case 3:
                            b1.display();
                            break;
                    case 4:
                            quit = true;
                            break;
                    }
            } while (!quit);
    }

public class Money
{

public static int accountNumber, balance=0;
static int amount;

static String name;
public void setDeposit(int amount) {   

            balance = balance + amount;   
    }
public int getDeposit()
{

            balance = balance + amount;
            if (amount < 0) {
                    System.out.println("Invalid");
            }
                                    return 1; 

}
 public void setBalance(int b)
 {
     b = balance;
 }
 public int getBalance()
 {
     return balance;
 }

 public void setWithdraw(int amount)  {


            balance = balance - amount;
    }
 public int getWithdraw()
 {
            balance = balance - amount;
     if (balance < amount) 
     {
                    System.out.println("Not enough funds.");
                    return 1;
            }
           else if (amount < 0) {
                    System.out.println("Invalid");
                    return 1;}
                    else 
                             return 0;
 }

import java.util.*;

public class BankAccount extends Money {
    static String name;
    public static int balance, amount, acctNum;
    Money customerMoney;


    BankAccount(String name, int accNo, int bal) {
            this.name = name;
            this.acctNum = accNo;
            this.balance = bal;
            this.customerMoney = new Money();
    }
            void display() {
            System.out.println("Name:" + name);
            System.out.println("Account No:" + acctNum);
            System.out.println("Balance:" + balance);

    }

    void displayBalance() {
            System.out.println("Balance:" + balance);
    }
    public Money getMoney(){
          return this.customerMoney;
    }
}
4

3 回答 3

4

最大的问题在于声明balance=0

public static int accountNumber, balance=0;
                                 ^^^^^^^^^

每次您要插入金额时,您的余额为零

你应该用过setDeposit(input.nextInt())

public void setBalance(int b),b = balance;应该是balance = b;

此外,您的amount,balance变量应该Float代替intas balance/ amountcan be 23434.22

于 2012-12-11T16:00:03.257 回答
1

I would remove all the public static int variables that you are using. These are going to cause confusion because it's much harder to follow what their values are as the program executes. Best to encapsulate your logic into BankAccount using private variables and public methods to modify them.

I would, personally, eliminate the Money class from your code. It's just going to cause confusion and with your simplified logic is not required. Let's assume the account holds some arbitrary count of 'money' but there is no real-life money actually backing it up - (kind of like real life, it's just 'numbers on a screen' right?) - in this case we wouldn't need the Money class, just an int for our BankAccount's balance.

Without trying to make too many changes to your underlying functionality, I rewrote as the below two classes:

A BankAccount class:

package banking;

public class BankAccount {

    /**
     * The balance of this account. <br/>
     * Assumes integer money (Floating point math is horrible and who really
     * needs pesky pence or cents right?!)
     */
    private int balance;
    /**
     * The account number
     */
    private final int acctNum;
    /**
     * Name of the account holder
     */
    private final String name;

    /**
     * Construct our basic account with an account number, account holder and
     * starting balance.
     *
     * @param name
     * @param accNo
     * @param bal
     */
    public BankAccount(String name, int accNo, int bal) {
        this.name = name;
        this.acctNum = accNo;
        this.balance = bal;
    }

    /**
     * Make a deposit to this account by adding a fixed sum to the existing
     * balance. <br/>
     *
     * @param amount
     */
    public void deposit(int amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("You cannot deposit zero or less");
        } else {
            this.balance += amount;
        }
    }

    /**
     * Make a withdrawal from this account by subtracting a fixed amount from
     * the existing balance. <br/>
     *
     * @param amount
     */
    public void withdraw(int amount) {
        if (amount > balance) {
            throw new IllegalArgumentException("Insufficient Funds");
        } else if (amount <= 0) {
            throw new IllegalArgumentException("You cannot withdraw zero or less");
        } else {
            balance -= amount;
        }
    }

    /**
     * Get the account holder name.
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * Get the current account balance.
     *
     * @return
     */
    public int getBalance() {
        return balance;
    }

    /**
     * Get the account identifier for this account.
     *
     * @return
     */
    public int getAcctNum() {
        return acctNum;
    }

    /**
     * Debug print method.
     */
    public void display() {
        System.out.println("Name:" + name);
        System.out.println("Account No:" + acctNum);
        System.out.println("Balance:" + balance);
    }
}

And the main class:

package banking;

import java.util.Random;
import java.util.Scanner;

public class BankMain {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter your Name: ");
        String customerName = input.nextLine();

        Random randomGenerator = new Random();
        int acctNo = randomGenerator.nextInt(100000);

        System.out.println("Enter Initial Balance: ");
        int balance = input.nextInt();

        BankAccount acct = new BankAccount(customerName, acctNo, balance);

        System.out.println("Menu");
        System.out.println("1. Deposit Amount");
        System.out.println("2. Withdraw Amount");
        System.out.println("3. Display Information");
        System.out.println("4. Exit");

        boolean quit = false;
        int menu;
        do {
            final int transaction;
            System.out.print("Please enter your choice: ");
            menu = input.nextInt();
            switch (menu) {
                case 1:
                    System.out.print("Enter depost amount:");
                    transaction = input.nextInt();
                    acct.deposit(transaction);
                    break;

                case 2:
                    System.out.println("Current Account Balance=" + acct.getBalance());
                    System.out.print("Enter withdrawal amount:");
                    transaction = input.nextInt();
                    try {
                        acct.withdraw(transaction);
                    } catch (IllegalArgumentException iaEx) {
                        System.out.println(iaEx.getMessage());
                    }
                    break;

                case 3:
                    acct.display();
                    break;

                case 4:
                    quit = true;
                    break;
            }
        } while (!quit);
    }
}

This is still far from perfect, but I feel it's easier to follow for having the static variables and Money class removed.

于 2012-12-11T16:34:09.693 回答
0

You have many problems with the code e.g. none of your fields should be static, but the main one is that you have multiple duplicated fields.

e.g. In Money you have

public static int accountNumber, balance=0;
static int amount;

static String name;

and in BankAccount you have

static String name;
public static int balance, amount, acctNum;
Money customerMoney;

which means you have multiple fields called name, balance and amount. Some of the code uses the first group of fields and some of the code uses the second. You also have a customerMoney which is not updated directly.

To avoid confusion I suggest you have each field be non-static and in only one place. Remove the customerMoney as you might be tempted to use it ;)

于 2012-12-11T16:29:15.623 回答