2016-10-02 3 views
-1

Ich mache die Übung (3.11) von Deital Wie Programm 10. Ausgabe gibt es keinen Fehler in meinem Programm, aber ich will, dass wenn das Geld abheben dann Balance dann sollte es zeigen nur "Unzureichende Balance" Nachricht, sollte es nicht zeigen, den Betrag Betrag bitte hilf mir, ich bin nur ein Anfänger.WithDraw Geld Von einem Konto (Java)

, das ist mein Hauptprogramm

package practice; 
import java.util.Scanner; 
public class apple { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     tuna account1 = new tuna ("James" , 150000); 
     tuna account2 = new tuna ("David" , 5000); 
     System.out.printf("%s Balance: %.2f" , account1.getName(),account1.getBalance()); 
     System.out.printf("%n%s Balance: %.2f" , account2.getName(), account2.getBalance()); 
     System.out.print("\nEnter the Ammount that will add in James Account: "); 
     double depositAmmount = input.nextDouble(); 
     System.out.printf("Adding %.2f to James Account", depositAmmount); 
     account1.deposit(depositAmmount); 
     System.out.printf("%n%s Balance: %.2f" , account1.getName(),account1.getBalance()); 
     System.out.print("\nEnter the Ammount that will add in David Account: "); 
     depositAmmount = input.nextDouble(); 
     System.out.printf("Adding %.2f to David Account", depositAmmount); 
     account2.deposit(depositAmmount); 
     System.out.printf("%n%s Balance: %.2f" , account2.getName(),account2.getBalance()); 
     System.out.print("\nEnter the Ammount that will withdrawal from james Account: "); 
     double wD = input.nextDouble(); 
     System.out.printf("WithDrawal %.2f from James Account",wD); 
     account1.withDrawal(wD); 
     System.out.printf("%n%s Balance: %.2f" , account1.getName(),account1.getBalance()); 
    } 
     } 

und das ist die Klasse

package practice; 

public class tuna { 
private String name; 
private double balance; 
public tuna (String name , double balance){ 
    this.name = name; 
    if (balance > 0) 
     this.balance = balance; 
} 
public void deposit(double depositAmmount){ 
    if (depositAmmount > 0) 
     balance = balance + depositAmmount; 
} 
public void withDrawal (double wD){ 
    if (wD > balance) 
     System.out.println("\nInsufficient Balance"); 
    else 
    balance = balance - wD; 
} 
public double getBalance(){ 
    return balance; 
} 
public void setName(String name){ 
    this.name = name; 
} 
public String getName(){ 
    return name; 
} 
} 
+0

Was ist das genaue Problem? Ich denke du willst wissen, ob die 'withDrawal'-Funktion tatsächlich die 'balance' Variable verändert hat? In diesem Fall könntest du (zum Beispiel) ein 'boolean' zurückgeben (' true', wenn es sich zurückzieht, 'false' wenn nicht) und darauf achten (und eine andere Zeile davon drucken) – UnholySheep

+0

ich will nur wann Abhebung Geld ist größer als das Gleichgewicht, dann zeigt es nur "Unzureichende Balance" Nachricht nicht die Höhe dieses Kontos –

+0

Ja, das ist, was mein Vorschlag würde Ihnen erlauben, zu tun. Ansonsten könntest du die print-Anweisung auch in den 'if'-Zweig der 'withDrawal'-Funktion verschieben (was eine hässlichere Lösung IMO ist, würde ich mit einem' boolean' gehen) – UnholySheep

Antwort

0

So eine mögliche Lösung der Funktion zurückkehren zu müssen, ist ein boolean die angibt, ob es erfolgreich war oder nicht:

public boolean withDrawal (double wD){ 
    if (wD > balance) { 
     return false; 
    } 
    else { 
     balance = balance - wD; 
     return true; 
    } 
} 

Und Verwendung:

if(account1.withDrawal(wD)) { 
    System.out.printf("%n%s Balance: %.2f" , account1.getName(),account1.getBalance()); 
} 
else { 
    System.out.println("\nInsufficient Balance"); 
} 
+0

Vielen Dank, Bruder –

Verwandte Themen