java - Giving wrong result -


public class account { public double balance ; public double deposite; public double withdraw; public account(double balance) {     this.balance = balance;    } public double getbalance() {     return balance; } public void setbalance(double balance) {     this.balance = balance; } public double getdeposite() {     balance = balance + deposite;     return deposite; } public void setdeposite(double deposite) {     this.deposite = deposite;  } public double getwithdraw() {  return withdraw; } public void setwithdraw(double withdraw) {     this.withdraw = withdraw;     if(withdraw <= balance){    balance =  balance - withdraw;     } }  public boolean withdraw(double wamt) {     boolean result = false;     if(withdraw <= wamt)     {     balance= balance - withdraw;     return true;     }     return result;      } 

}

my customer class

public class customer { private string firstname; private string lastname; account account; public customer(string firstname, string lastname) {     this.firstname = firstname;     this.lastname = lastname;     //this.account = account; } public string getfirstname() {     return firstname; } public void setfirstname(string firstname) {     this.firstname = firstname; } public string getlastname() {     return lastname; } public void setlastname(string lastname) {     this.lastname = lastname; } public account getaccount() {     return account; } public void setaccount(account account) {     this.account = account; }   }   public class bankprojectdemo { public static void main(string[] args) {    double balance = 500;    customer cust = new customer("asasd0","asdasda");    account accnt = new account(balance);    system.out.println("creating customer:  " +cust.getfirstname());    accnt.setwithdraw(150);    accnt.setdeposite(22.50);    system.out.println("withdraw1  "+accnt.getwithdraw());    system.out.println("depsoite  "+accnt.getdeposite());    account accnt1 = new account(balance);    accnt1.setwithdraw(47.62);    system.out.println("withdraw2 "+accnt1.getwithdraw()+"  " + accnt1.withdraw(balance));    system.out.println("balance " + accnt.getbalance());}} 

i'm trying calculate simple banking function.i passed 2 withdraw value 150 , 47.62 through 1 method execution class another.but takes 47.62 twice , giving me wrong result here execution class.

in your

public void setwithdraw(double withdraw) {     this.withdraw = withdraw;     if(withdraw <= balance){    balance =  balance - withdraw;     } } 

you reduced balance once, , in your

public boolean withdraw(double wamt) {     boolean result = false;     if(withdraw <= wamt)     {     balance= balance - withdraw;     return true;     }     return result;  } 

you reduced balance once again, double withdraw when call accnt1.setwithdraw(47.62); followed accnt1.withdraw(balance); in

accnt1.setwithdraw(47.62); system.out.println("withdraw2 "+accnt1.getwithdraw()+"  " + accnt1.withdraw(balance)); 

change setwithdraw(double withdraw) exclude the

if(withdraw <= balance){    balance =  balance - withdraw; } 

i.e.

public void setwithdraw(double withdraw) {     this.withdraw = withdraw; } 

and don't need parameter wamt in withdraw() function.

i.e.

public boolean withdraw() {     boolean result = false;     if(withdraw <= balance)     {     balance= balance - withdraw;     return true;     }     return result;  } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -