java - Removing element from TreeMap -
i creating project allows users create, delete etc bank accounts. have been struggling case 3 (remove method). want user enter account id remove treemap. how should method laid out?
package mainsample; import java.util.*; public class mainsample { public static void main(string[] args) { scanner scan = new scanner(system.in); bankprocess bankprocess = new bankprocess(); transactionprocess transactionprocess = new transactionprocess(); bank bank = new bank(); bankaccount bankaccount = new bankaccount(); int input; int selection; while (true) { system.out.println(""); system.out.println("######## menu ########"); system.out.println("[1] create account"); system.out.println("[2] print existing accounts"); system.out.println("[3] delete account"); system.out.println("[4] deposit"); system.out.println("[5] withdraw"); system.out.println("[6] print transactions"); system.out.println("[0] exit"); system.out.println("######################"); system.out.println("please choose 1 of following: "); selection = scan.nextint(); switch (selection) { case 0: system.out.println("exit successful"); system.exit(0); break; case 1: system.out.println("'[1] create account' has been selected."); system.out.print("account id: "); int accountid = scan.nextint(); scan.nextline(); system.out.print("holder name: "); string holdername = scan.nextline(); system.out.print("holder address: "); string holderaddress = scan.nextline(); system.out.print("opening balance: "); double openingbalance = scan.nextdouble(); system.out.print("open date: "); string opendate = scan.next(); bankaccount = new bankaccount(accountid, holdername, openingbalance, holderaddress, opendate); bank.setaccounts(bankprocess.opennewaccount(bank.getaccounts(), bankaccount)); system.out.println("successfully added."); break; case 2: system.out.println("'[2] display existing accounts' has been selected"); system.out.println("-----------------------------------------------------"); bank.getaccounts().foreach((i, b) - > system.out.println(b)); system.out.println("-----------------------------------------------------"); break; case 3: system.out.println("[3] delete account has been selected"); system.out.println("enter account id: "); int accountno = scan.nextint(); bank.removeaccounts(bankprocess.removeaccount(bank.getaccounts(), bankaccount)); break; case 4: system.out.println("[4] deposit has been selected"); system.out.println("enter account id: "); int accountnumber = scan.nextint(); system.out.println("enter deposit amount: "); double depositamount = scan.nextdouble(); transactionprocess.deposit(bankaccount, depositamount); system.out.println(depositamount + " has been deposited."); break; case 5: system.out.println("[5] withdraw has been selected"); system.out.println("enter account id: "); int accountnu = scan.nextint(); system.out.println("enter withdraw amount: "); double withdrawamount = scan.nextdouble(); transactionprocess.withdraw(bankaccount, withdrawamount); system.out.println(withdrawamount + " has been withdrawed."); break; case 6: system.out.println("[6] print transaction has been selected"); system.out.println("enter account id: "); int accountn = scan.nextint(); break; default: system.out.println("your choice not valid!"); } } } }
/* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ package mainsample; import java.util.*; /** * * @author khalid */ public class bankaccount { private int accountid; private string holdername; private string holderaddress; private string opendate; private double currentbalance; private list < transaction > transactions = new arraylist < transaction > (); //provide blank constructor public bankaccount() {} //constructor arguments. public bankaccount(int accountnum, string holdernam, double currentbalance, string holderadd, string opendate) { this.accountid = accountnum; this.holdername = holdernam; this.holderaddress = holderadd; this.opendate = opendate; this.currentbalance = currentbalance; } // provide setter , getters public int getaccountid() { return accountid; } public void setaccountid(int accountid) { this.accountid = accountid; } public string getholdername() { return holdername; } public void setholdername(string holdername) { this.holdername = holdername; } public string getholderaddress() { return holderaddress; } public void setholderaddress(string holderaddress) { this.holderaddress = holderaddress; } public string getopendate() { return opendate; } public void setopendate(string opendate) { this.opendate = opendate; } public double getcurrentbalance() { return currentbalance; } public void setcurrentbalance(double currentbalance) { this.currentbalance = currentbalance; } public list < transaction > gettransactions() { return transactions; } public void settransactions(list < transaction > transactions) { this.transactions = transactions; } public void addtransaction(transaction transaction) { if (transactions.size() >= 6) { // test if list has 6 or more transactions saved transactions.remove(0); // if so, remove first (it's oldest) } transactions.add(transaction); // new transaction added, no matter how many other transactions there in list } public string tostring() { return "\naccount number: " + accountid + "\nholder's name: " + holdername + "\nholder's address: " + holderaddress + "\nopen date: " + opendate + "\ncurrent balance: " + currentbalance; } }
package mainsample; import java.util.*; /** * * @author khalid */ public class bank { private treemap < integer, bankaccount > bankaccounts = new treemap < integer, bankaccount > (); public treemap < integer, bankaccount > getaccounts() { return bankaccounts; } public void setaccounts(treemap < integer, bankaccount > accounts) { this.bankaccounts = accounts; } public bankaccount getaccount(integer accountnumber) { return bankaccounts.get(accountnumber); } public void removeaccounts(treemap < integer, bankaccount > accounts) { this.bankaccounts = accounts; } }
package mainsample; import java.util.*; /** * * @author khalid */ public class bankprocess { // return updated list of bankaccounts public treemap < integer, bankaccount > opennewaccount(treemap < integer, bankaccount > bankaccounts, bankaccount bankaccount) { //get list of existing bank accounts add new bankaccount it. bankaccounts.put(bankaccount.getaccountid(), bankaccount); return bankaccounts; } public treemap < integer, bankaccount > removeaccount(treemap < integer, bankaccount > bankaccounts, bankaccount bankaccount) { bankaccounts.remove(bankaccount.getaccountid(), bankaccount); return bankaccounts; } }
you have bankaccount
first accountno
getting console because removeaccount
method in bankprocess
expects bankaccount object removal.
try
case 3: system.out.println("[3] delete account has been selected"); system.out.println("enter account id: "); int accountno = scan.nextint(); bankaccount = bank.getaccount(accountno); // bankaccount account id bank.removeaccounts(bankprocess.removeaccount(bank.getaccounts(), bankaccount)); break;
Comments
Post a Comment