java - Combining two accounts -
this homework assignment. need consolidate 2 accounts (acct2 , acct3) , produce third 1 same name, different account number , $200. have close first 2 accounts. not issue though. can work fine if not declare public static account accountconsolidate(account acct1, account acct2) static method, , create object in main. not work though because required declare method static. also, if declare static can proper return value in if(acct1.name.equalsignorecase(acct2.name) && acct1.acctnum != acct2.acctnum)
if exclude `&& acct1.acctnum != acct2.acctnum other wise return null ("these 2 accounts not able consolidated. please check criteria again", not sure why.
any great. thanks
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, , 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 see if balance sufficient withdrawal. // if so, decrements balance 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 balance. //------------------------------------------------- public void deposit(double amount) { balance += amount; } //------------------------------------------------- // returns balance. //------------------------------------------------- public double getbalance() { return balance; } // static method keep track of incrementing account public static int getnumaccounts() { return numaccounts; } // close 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 2 accounts not able consolidated. please check criteria again"); return null; } } //------------------------------------------------- // returns string containing name, account number, , balance. //------------------------------------------------- public string tostring() { return "name:" + name + "\naccount number: " + acctnum + "\nbalance: " + balance; } } //************************************************************ //testaccounts1 //a simple program test numaccts method of //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 name account 1: "); name1 = scan.nextline(); account acct1 = new account (100, name1); system.out.println(acct1); system.out.println("now there " + account.getnumaccounts() + " accounts"); system.out.println(""); system.out.print("please enter name account 2: "); name2 = scan.nextline(); account acct2 = new account (100, name2); system.out.println(acct2); system.out.println("now there " + account.getnumaccounts() + " accounts"); system.out.println(""); system.out.print("please enter name account 3: "); name3 = scan.nextline(); account acct3 = new account (100, name3); system.out.println(acct3); system.out.println("now there " + account.getnumaccounts() + " accounts"); system.out.println(""); acct1.close(); system.out.println(""); system.out.println("there " + account.getnumaccounts() + " accounts"); system.out.println("accounts consolidated"); system.out.println(account.accountconsolidate(acct2, acct3)); } }
your account numbers randomly generated accnum
static. every account have created account number of last account. acct1.acctnum != acct2.acctnum
false.
your account name static well, each account have account name of last created account.
after changed read compiler errors. tell next. think variables of object modify. acctnum = math.abs(gen.nextdouble());
not set account number of consolidated account account2
.
Comments
Post a Comment