BankAccount java程序没有显示输出

时间:2016-12-21 20:30:55

标签: java

我做了这个程序而且我不明白为什么没有显示任何东西,如果有人可以给我一个建议或任何暗示。我真的很感激。

文字的第一部分 http://i.imgur.com/4yNLefc.jpg

第二部分 http://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      
    }       
}

2 个答案:

答案 0 :(得分:0)

当我运行你的程序时,我得到了以下内容:

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!

正如其他人在评论部分中建议的那样,您需要使用System.out.println()打印输出。所以,我改变了你的一行代码如下:

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

输出:

Dan 1000.0 1 2100.0

我修改了toString() abstract class BankAccount方法,如下所示:[只是在值之间添加了空格]

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

答案 1 :(得分:0)

我认为您期望在应用程序执行时在命令行上打印一些文本。 打印信息通常由System.out.println完成,或者例如由System.out.println完成。错误System.err.println。这是一个例子:

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

在你的代码中你有很多没有效果的语句,即调用a.toString();这个返回一个字符串,但是你没有做任何事情。如果您希望将a打印到控制台,请将实施更改为

System.out.println(a);

(在这种情况下可以省略toString()。)