Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Constructor help
    primarykey
    data
    text
    <pre><code>//******************************************************* // Account.java // // A bank account class with methods to deposit to, withdraw from, // change the name on, and get a String representation // of the account. //******************************************************* import java.util.Random; public class Account { private double balance; private String name; private long acctNum; //---------------------------------------------- //Constructor -- initializes balance, owner, and account number //---------------------------------------------- public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; } //---------------------------------------------- // 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 &gt;= amount) balance -= amount; else System.out.println("Insufficient funds"); } //---------------- //Track how many accounts //---------------- private static int numAccounts=0; { numAccounts++; } public static int getNumAccounts() { return numAccounts; } //---------------------------------------------- // Adds deposit amount to balance. //---------------------------------------------- public void deposit(double amount) { balance += amount; } //---------------------------------------------- // Returns balance. //---------------------------------------------- public double getBalance() { return balance; } // Get name of account public String getName() { return name; } //---------------------------------------------- // Returns account number. //---------------------------------------------- public long getAcctNumber() { return acctNum; } //---------------- //Void and close the accounts //---------------- public void close() { balance = 0; name += "CLOSE"; numAccounts--; } //---------------- //Consolidating accounts //---------------- public static Account consolidate(Account acct1,Account acct2) { Account newAccount=null; if((acct1.getName()).equals(acct2.getName())) if(acct1.getAcctNumber()!=acct2.getAcctNumber()) {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner); Random generator = new Random(); acctNum= generator.nextInt(); acct1.close(); acct2.close(); } else System.out.println("Not allow,same account number"); else System.out.println("Can't use other people account"); return newAccount; } //---------------------------------------------- // Returns a string containing the name, account number, and balance. //---------------------------------------------- public String toString() { return "Name: " + name + "\nAccount Number: " + acctNum + "\nBalance: " + balance; } } </code></pre> <p>Please look at the //consolidate section. What I'm trying to do is, consolidate acct1 and acct2 into one new account, with the restrictions that is acct1 and acct2 has to has the same name, acct1 and acct2 account number has to be different from each other, and if those are met, create a new account with a new balance from the two old account, keep the same name and also randomly generate a new account number. Is there something missing in my code? It wont compile. These are errors i got</p> <pre> Account.java:95: ')' expected {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner); ^ Account.java:95: illegal start of expression {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner); ^ </pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload