2016-12-21 3 views
-1

Ich habe dieses Programm gemacht und ich verstehe nicht, warum zeigt nichts, wenn jemand mir einen Rat oder einen Hinweis darüber geben kann. Ich würde das wirklich zu schätzen wissen.BankAccount Java-Programm zeigt keine Ausgabe

Der erste Teil des Texteshttp://i.imgur.com/4yNLefc.jpg

Zweiter Teilhttp://i.imgur.com/nq9FAGs.jpg

import java.util.Calendar; 

abstract class BankAccount{ 
    private double balance=0; 
    private String owner; 
    private int id; 
    private int idCurrentAccount=1; 

    public BankAccount(String owner,double balance){ 
     this.owner=owner; 
     this.id=idCurrentAccount++; 
     this.balance=balance;  
    } 

    public void deposit(double sum){ 
     this.balance=this.balance+sum; 
     System.out.println("You just added "+sum+" in your account.You have know: "+balance); 
    } 

    public void withdraw(double sum){ 
     if(sum>balance) 
      System.out.println("Sorry.Not enough money to withdraw!"); 
     else{ 
      System.out.println("You just withdrawed "+sum+" from "+balance+".Have a nice day!"); 
      balance=balance-sum; 
     } 
    } 

    public double getBalance(){ 
     return balance; 
    } 

    public String getOwner(){ 
     return owner; 
    } 

    public int getId(){ 
     return id; 
    } 

    @Override 
    public String toString(){ 
     return owner+balance+id+idCurrentAccount; 
    } 

    public abstract void endOfMonth(); 
} 

class SavingsAccount extends BankAccount{ 
    private double interestRate; 

    public SavingsAccount(String owner,double balance,double interestRate){ 
     super(owner,balance);  
     this.interestRate=interestRate; 
    } 

    public void applyInterest(){ 
     deposit(getBalance()*interestRate); 
    } 

    @Override 
    public String toString(){ 
     return super.toString()+interestRate; 
    } 

    @Override 
    public void endOfMonth(){ 
     Calendar cal=Calendar.getInstance(); 
     int currentMonth=cal.get(Calendar.MONTH); 
     cal.add(Calendar.DAY_OF_YEAR,1); 
     int nextDayMonth=cal.get(Calendar.MONTH); 
     if(currentMonth!=nextDayMonth){ 
      applyInterest();  
     } 
    } 
} 

class CurrentAccount extends BankAccount{ 
    private int transactionNo=0; 
    final int FREE_TRANSACTIONS=5; 
    double TRANSACTION_COST; 

    public CurrentAccount(String owner, double balance, int transNr, double transCost){ 
     super(owner,balance); 
     this.transactionNo=transNr; 
     this.TRANSACTION_COST=transCost; 
    } 

    public void dischargeExpenses() { 
     if(transactionNo>FREE_TRANSACTIONS) 
      withdraw(getBalance()-TRANSACTION_COST); 
    } 

    @Override 
    public String toString(){ 
     return super.toString()+transactionNo+TRANSACTION_COST; 
    } 

    @Override 
    public void deposit(double sum){ 
     super.deposit(sum); 
     transactionNo++; 
    } 

    @Override 
    public void withdraw(double sum){ 
     super.withdraw(sum); 
     transactionNo++; 
    } 

    @Override 
    public void endOfMonth(){ 
     Calendar cal=Calendar.getInstance(); 
     int currentMonth=cal.get(Calendar.MONTH); 
     cal.add(Calendar.DAY_OF_YEAR,1); 
     int nextDayMonth=cal.get(Calendar.MONTH); 
     if(currentMonth!=nextDayMonth){ 
      dischargeExpenses(); 
      transactionNo=0; 
     } 
    } 
} 

public class Llab12{ 
    public static void main(String[] args){ 
     SavingsAccount a = new SavingsAccount("Dan",1000,100); 
     SavingsAccount b = new SavingsAccount("Alex",10000,1000); 
     CurrentAccount c = new CurrentAccount("Dan",200000,10,100); 
     CurrentAccount d = new CurrentAccount("Alex",200000,33,100); 
     a.toString(); //Not showing output 
     b.toString(); //Not showing output 
     c.toString(); //Not showing output 
     d.toString(); //Not showing output 
     c.deposit(300); 
     d.deposit(900); 
     c.getBalance(); //Not showing output 
     d.getBalance(); //Not showing output 
     c.deposit(3007); 
     d.withdraw(9); 
     c.getBalance(); //Not showing output 
     d.getBalance(); //Not showing output 
     c.endOfMonth(); //Not showing output 
     d.endOfMonth(); //Not showing output 
     c.toString(); //Not showing output 
     d.toString(); //Not showing output  
    }  
} 
+4

Sie müssen 'System.out.println (Methodenaufruf tun) 'wann immer Sie eine Ausgabe wünschen. –

+0

Wenn ich Ihren Code ausführen zeigt es 'Sie haben gerade 300.0 in Ihrem Konto hinzugefügt. Sie haben wissen: 200300.0 Sie haben gerade 900.0 in Ihrem Konto.Sie wissen: 200900.0 Sie haben gerade 3007.0 in Ihrem Konto hinzugefügt.Sie ​​wissen: 203307.0 Sie haben gerade 9.0 von 200900.0 zurückgezogen. Einen schönen Tag noch! ' –

+0

Haben Sie den richtigen Code ausgeführt? –

Antwort

0

Wenn ich Ihr Programm lief, bekam ich die folgende:

You just added 300.0 in your account.You have know: 200300.0 
You just added 900.0 in your account.You have know: 200900.0 
You just added 3007.0 in your account.You have know: 203307.0 
You just withdrawed 9.0 from 200900.0.Have a nice day! 

Wie andere vorgeschlagen in Im Kommentarbereich müssen Sie die Ausgabe u ausdrucken singen System.out.println(). Also, ich geändert Sie eine Zeile Code wie folgt:

System.out.println(a.toString()); // instead of a.toString() 

Es gibt:

Dan 1000.0 1 2100.0 

ich Ihre toString() Methode von abstract class BankAccount geändert habe, wie folgt: [nur mehr Platz zwischen den Werten]

@Override 
public String toString() { 
    return owner + " " + balance + " " + id + " " + idCurrentAccount; 
} 
0

Ich denke, Sie erwartet, dass einige Text in der Befehlszeile während der Ausführung der Anwendung gedruckt wird. Drucken der Informationen wird in der Regel von System.out.println oder für z. Fehler System.err.println. Hier ein Beispiel:

System.out.println("This is a test"); 

In Ihrem Code Sie viele Aussagen ohne Wirkung haben, das heißt den Anruf a.toString(); Dieser gibt eine Zeichenfolge, aber dann nichts, was Sie tun mit ihm. Wenn Sie a gedruckt haben, um die Konsole möchten, ändern Sie bitte die Umsetzung zu

System.out.println(a); 

(toString() kann in diesem Fall verzichtet werden.)

Verwandte Themen