java - Getting The ArrayList To Keep Each Account Balance And Number -
i have completed program stuck on getting each account balance
for example
if enter first savings account balance work fine when open savings account account balance of first account show the last account balance entered know have correct error thanks
package object_1_programs; import java.util.date; /** * * */ public abstract class account { //instance variables decleared private string c_name; private string acc_num; private double acc_balance; java.util.arraylist transactions=new java.util.arraylist(); //default constructor public account(){ } //constructor public account(string c_name,string acc_num,double acc_balance){ this.c_name=c_name; this.acc_num=acc_num; this.acc_balance=acc_balance; } //getters , setter method public string setname(){ return this.c_name; } public string getcname(){ return this.c_name; } public string setaccnum(string acc_number){ return this.acc_num; } public string getaccnum(){ return this.acc_num; } public double setbalance(double balance){ return this.acc_balance=balance; } public double getbalance(){ return this.acc_balance; } //end of getters setters method //tostring method decleared @override public string tostring(){ return "customer name"+this.c_name +"account number:"+this.acc_num; } //beginning of withdrawal method public void withdraw(double total){ if(acc_balance<total){ system.out.print("not enough funds sorry current balance "+acc_balance); } else{ this.acc_balance -= total; system.out.print("\n"+"your balance is"+ this.acc_balance);} transaction t1 = new transaction(new date(), 'w', this.acc_balance, "withdrawal made"); transactions.add(t1); } //end of withdrawal method //beginning of deposit method public void deposit(double total){ this.acc_balance += total; system.out.printf("\n"+total+"your balance is"+ this.acc_balance); transaction t1 = new transaction(new date(), 'd', this.acc_balance, "deposit made"); transactions.add(t1); } //end of deposit method } transaction class package object_1_programs; //date decleared import java.util.date; /** * * @ */ public class transaction { //instance variables decleared private date date; private char type; private double amount; private double acc_balance; private string description; //constructor public transaction(date date, char type, double acc_balance, string description) { this.acc_balance = acc_balance; this.date = new date(); this.type = type; this.description = description; } //getters , setters method decleared public date getdate() { return date; } public date setdate() { return date; } public char gettype() { return type; } public char settype() { return type; } public double getbalance() { return acc_balance; } public double setbalance() { return acc_balance; } public double getamount() { return amount; } public double setamount() { return amount; } public string getdescription() { return description; } public string setdescription() { return description; } //tostring method @override public string tostring() { return "\n"+new date() + "type : " + type + "account balance " + acc_balance + "description " + description; } } saving account class import java.util.date; /** * * @ */ public class savingsaccount extends account { // instance variables decleared private double int_rate; //default constrictor public savingsaccount(){ int_rate=0; } //main construtor public savingsaccount(string c_name, string acc_num, double acc_balance, double int_rate) { super(c_name,acc_num, acc_balance); this.int_rate = int_rate; } //getters , setters method decleared public double setrate(double rate) { return int_rate=rate/100; } public double getrate() { return int_rate; } //payinterest method decleared public void payinterest() { double acc_amount=getbalance(); double balance=0; balance = (acc_amount + (acc_amount *getrate())); setbalance(balance); system.out.printf("account balance interest %.2f", setbalance(balance)); transaction t1 = new transaction(new date(), 'i' , setbalance(balance), "interest paid"); transactions.add(t1); } //withdrawal method override @override public void withdraw(double total) { double acc_amount = getbalance(); double balance = 0; if (acc_amount < total) { system.out.printf("not enough funds main account balance :$%.2f", acc_amount); } else { balance=acc_amount -= total; setbalance(balance); system.out.printf("your withdrawal of :$" + total + " successful new account balance :$%.2f",setbalance(balance)); } transaction t1 = new transaction(new date() , 'w' , setbalance(balance), "withdrawal made"); transactions.add(t1); } //deposit method override @override public void deposit(double total) { double balance; double acc_amount = getbalance(); balance= acc_amount+=total; setbalance(balance); system.out.printf("your deposit of :$" + total + " successful new account balance :$%.2f",setbalance(balance)); transaction t1 = new transaction(new date() , 'd' , setbalance(balance), "deposit made"); transactions.add(t1); } @override public string tostring() { return this.tostring() + "interest rate: " + int_rate; } } chequing class package object_1_programs; import java.util.date; /** * * */ public class chequingaccount extends account { // instance variable decleared private double over_draft; // default constructor public chequingaccount() { this.over_draft = 500; } //end of default constructor //main constructor begins public chequingaccount(string c_name, string acc_num, double acc_balance) { super(c_name, acc_num, acc_balance); } //end of main method // getters , setters method public double setoverdraft() { return over_draft = 500; } //to string override @override public string tostring() { return this.tostring() + "over draft limit :" + over_draft; } //withdrawal method override @override public void withdraw(double amount) { double acc_amount = getbalance(); double balance = 0; if (amount >balance && amount> over_draft) { system.out.println("sorry cannot give amount choose lower amount or tell d girl yuh cah remember yuh pin"); } else { balance= acc_amount-= amount; setbalance(balance); } system.out.printf(amount+ "your new balance %.2f", setbalance(balance)); transaction t1 = new transaction(new date() , 'w', setbalance(balance), "withdrawal made:"); transactions.add(t1); } //deposit method override @override public void deposit(double amount) { double acc_amount = getbalance(); double balance=0; balance=acc_amount += amount; setbalance(balance); system.out.printf("your deposit of :$" + amount + " successful new account balance :$%.2f",setbalance(balance)); transaction t1 = new transaction(new date() , 'd' , setbalance(balance), "deposit made:"); transactions.add(t1); } } test program public class account_records_test { public static void main(string[] args) { scanner input = new scanner(system.in); arraylist savings =new arraylist(); arraylist chequing=new arraylist(); string name; char type; char selection; double balance; double rate; string acc_num; double total; savingsaccount s1 = new savingsaccount(); chequingaccount c1 = new chequingaccount(); system.out.println("\n" + "please select 1 of following options"); system.out.println("| o-to open account |"); system.out.println("| d-to make deposit |"); system.out.println("| w-to make withdraw |"); system.out.println("| i-to pay interest |"); system.out.println("| t-to view transactions |"); system.out.println("| q-to quit |"); type = input.next().trim().touppercase().tolowercase().charat(0); while (type != 'q') { if (type == 'o') { system.out.print("please select type of account open s-saving or c-checking :"); selection = input.next().trim().touppercase().tolowercase().charat(0); if (selection == 's') { system.out.print("please enter account holder name"); name=input.next(); system.out.print("please enter savings account number : "); acc_num = input.next(); savings.add(acc_num); system.out.print("please enter starting balance :$"); balance = input.nextdouble(); s1.setbalance(balance); system.out.print("please enter courrent interest rate :"); rate = input.nextdouble(); s1.setrate(rate); savings.add(acc_num); } else if (selection == 'c') { system.out.print("please enter chequing account number :"); acc_num = input.next(); chequing.add(acc_num); system.out.print("please enter starting balance :$"); balance = input.nextdouble(); c1.setbalance(balance); } } else if (type == 'd') { system.out.print("please enter account number wish make deposit :"); acc_num= input.next(); if (savings.contains(acc_num)) { system.out.print("please enter how deposit savings account :$"); total = input.nextdouble(); s1.deposit(total); } else if (chequing.contains(acc_num)) { system.out.print("please enter how likt deposit chequing account :$"); total = input.nextdouble(); c1.deposit(total); } } else if (type == 'w') { system.out.print("please enter account number :"); acc_num = input.next(); if (savings.contains(acc_num)) { system.out.print("please enter amount wish withdraw saving account"); total = input.nextdouble(); s1.withdraw(total); } else if (chequing.contains(acc_num)) { system.out.print("please enter amount wish withdraw chequing account"); total = input.nextdouble(); c1.withdraw(total); } } else if (type == 'i') { system.out.print("please enter account"); acc_num = input.next(); if (savings.contains(acc_num)) { s1.payinterest(); } else { system.out.print("sorry account isnt savings account"); } } else if (type == 't') { system.out.print("please enter account number"); acc_num = input.next(); if (savings.contains(acc_num)) { system.out.printf("\n" + "transaction history account :%s", acc_num); system.out.println(s1.transactions.tostring()); } else if(chequing.contains(acc_num)) { system.out.printf("\n" + "transaction history account :%s", acc_num); system.out.println(c1.transactions.tostring()); } } else if(type=='q'){ break; } system.out.println("\n" + "please select 1 of following options"); system.out.println("| o-to open account |"); system.out.println("| d-to make deposit |"); system.out.println("| w-to make withdraw |"); system.out.println("| i-to pay interest |"); system.out.println("| t-to view transactions |"); system.out.println("| q-to quit |"); type = input.next().trim().touppercase().tolowercase().charat(0); } } }
your problem created 1 savingsaccount object (s1). everytime "open account" overwriting 1 savingsaccount made. suggest making arraylist of savingsaccounts so: arraylist accounts = new arraylist();. everytime open account, add savingsaccount object create "accounts."
it worth knowing "arraylist name = new arraylist();" syntax deprecated. should put identifier in arraylist declaration. identifier, denoted specifies type of object arraylist holds.
Comments
Post a Comment