合并两个帐户

时间:2017-04-07 06:58:07

标签: java

这是一项家庭作业。我需要合并两个帐户(acct2和acct3)并生成第三个帐户,名称相同,帐号和200美元。我还必须关闭前两个帐户。但这不是真正的问题。如果我不将公共静态帐户accountConsolidate(帐户acct1,帐户acct2)声明为静态方法,并且只是在main中创建一个对象,我可以使一切正常工作。这不起作用,因为我需要将此方法声明为静态。此外,如果我将其声明为静态,我可以在if(acct1.name.equalsIgnoreCase(acct2.name) && acct1.acctNum != acct2.acctNum)中获得正确的返回值,如果我排除了 `&安培;&安培; acct1.acctNum!= acct2.acctNum  另外,它将返回null(“这两个帐户无法合并。请再次检查标准”,不确定原因。

任何帮助都会很棒。感谢

import java.util.Random;

    public class Account
    {
        private static int numAccounts = 0;
        private double balance;
        private static String name;
        private static double acctNum;
        static Random gen = new Random();

    //-------------------------------------------------
    //Constructor -- initializes balance, owner, and account number
    //-------------------------------------------------
        public Account(double initBal, String owner, double number)
        {
            balance = initBal;
            name = owner;
            acctNum = number;
            numAccounts++;
        }

        public Account(double initBal, String owner)
        {
            balance = initBal;
            name = owner;
            acctNum =  Math.abs(gen.nextDouble());
            numAccounts++;
        }

        public Account(String owner)
        {
            balance = 0;
            name = owner;
            acctNum =  Math.abs(gen.nextDouble());
            numAccounts++;
        }

    //-------------------------------------------------
    // Checks to see if balance is sufficient for withdrawal.
    // If so, decrements balance by amount; if not, prints message.
    //-------------------------------------------------
        public void withdraw(double amount)
        {
            if (balance >= amount)
            {   
                balance -= amount;
            }   
            else
                System.out.println("Insufficient funds");
        }

        public void withdraw(double amount, double fee)
        {
            if (balance >= amount)
                {
                    balance -= amount;
                    balance -= fee; 
                }   
            else
                System.out.println("Insufficient funds");   
        }

    public String getName()
    {
        return name;
    }

    public double getNum()
    {
        return acctNum;
    }

    //-------------------------------------------------
    // Adds deposit amount to balance.
    //-------------------------------------------------
        public void deposit(double amount)
        {
            balance += amount;
        }

    //-------------------------------------------------
    // Returns balance.
    //-------------------------------------------------
        public double getBalance()
        {
            return balance;
        }

    // Static method to keep track of incrementing Account
        public static int getNumAccounts()
        {
            return numAccounts;
        }

    // Close the account
        public void close()
        {
            balance = 0;
            name = "CLOSED";
            numAccounts--;
        }


    // Consolidate accounts
        public static Account accountConsolidate(Account acct1, Account acct2)
        {

            if(acct1.name.equalsIgnoreCase(acct2.name)  && acct1.acctNum != acct2.acctNum)
            {
                Account Account2 = new Account(0, acct1.name);
                Account2.balance = acct1.getBalance() + acct2.getBalance(); 
                acctNum = Math.abs(gen.nextDouble());

                acct1.close();
                acct2.close();  
                return Account2;

            }
            else
            {
                System.out.println("These two accounts are not able to be consolidated. Please check the criteria again");
                return null;
            }   
        }

    //-------------------------------------------------
    // Returns a string containing the name, account number, and balance.
    //-------------------------------------------------
        public String toString()
        {
            return "Name:" + name +
                "\nAccount Number: " + acctNum +
                "\nBalance: " + balance;
        }
    }

//************************************************************
//TestAccounts1
//A simple program to test the numAccts method of the
//Account class.
//************************************************************
import java.util.Scanner;

    public class TestAccount1
    {
        public static void main(String[] args)
        {
            String name1;
            String name2;
            String name3;

            Scanner scan = new Scanner(System.in);

            System.out.print("Please enter the name for account 1: ");
            name1 = scan.nextLine();
            Account acct1 = new Account (100, name1);
            System.out.println(acct1);
            System.out.println("Now there are " + Account.getNumAccounts() + " accounts");
            System.out.println("");

            System.out.print("Please enter the name for account 2: ");
            name2 = scan.nextLine();
            Account acct2 = new Account (100, name2);
            System.out.println(acct2);
            System.out.println("Now there are " + Account.getNumAccounts() + " accounts");
            System.out.println("");

            System.out.print("Please enter the name for account 3: ");
            name3 = scan.nextLine();
            Account acct3 = new Account (100, name3);
            System.out.println(acct3);
            System.out.println("Now there are " + Account.getNumAccounts() + " accounts");
            System.out.println("");

            acct1.close();
            System.out.println("");
            System.out.println("There are now "  + Account.getNumAccounts() + " accounts");


            System.out.println("Accounts consolidated");
            System.out.println(Account.accountConsolidate(acct2, acct3));
}
}

1 个答案:

答案 0 :(得分:0)

您的帐号是随机生成的,但accNum是静态的。因此,每个帐户都将拥有最后一个帐户的已创建帐号。所以acct1.acctNum != acct2.acctNum永远都是假的。

您的帐户名称也是静态的,因此每个帐户都将拥有上次创建帐户的帐户名称。

更改后,读取编译错误。它会告诉你下一步该做什么。考虑要修改哪个对象的变量。与acctNum = Math.abs(gen.nextDouble());一样,我们也不会设置合并帐户的帐号Account2