通过继承查找最小值和最大值

时间:2014-06-10 09:14:14

标签: java inheritance superclass

我遇到了这个问题。对于这个问题,我要编写一个与超类交互的子类。

一家公司编写了一个大型的BankingAccount,其中包括许多方法,包括:

方法/构造函数和描述:

  • public BankingAccount(Startup s)使用Startup对象中的信息构建BankingAccount对象
  • public void debit(借记d)记录给定的借记
  • 公共无效信用(信用证c)记录给定的信用
  • public int getBalance()以便士的形式返回当前余额

我将设计一个新类MinMaxAccount,其实例可用于代替BankingAccount对象,但包括记住帐户记录的最小和最大余额的新行为。您应该提供与超类相同的方法,以及以下新行为:

方法/构造函数和描述:

  • public MinMaxAccount(Startup s)使用
  • 构造MinMaxAccount对象
  • “启动对象”中的信息
  • public int getMin()以便士的形式返回最低余额
  • public int getMax()以便士的形式返回最大余额

帐户的构造函数根据启动信息设置初始余额。假设只有借方和贷方方法改变了账户的余额。

我遇到记录最小值和最大值的问题,b / c我不确定我是否在正确的轨道上。目前,它只返回最新的输入。任何建议将不胜感激。

// This is the subclass
public class MinMaxAccount extends BankingAccount{
private Startup thing;
private int min;
private int max;

    // constructor
    public MinMaxAccount(Startup s) {
        super(s);
        Startup thing = s;
        min = Integer.MAX_VALUE;
        max = Integer.MIN_VALUE;
    }

    // returns the lowest balance
    public int getMin() { 
        while (super.getBalance()<min) {
            min = super.getBalance();
        }
        return min;
    }

    // returns the highest value
    public int getMax() {
        while (super.getBalance()>max) {
            max = super.getBalance();
        }
        return max;
    }
}

Marty Stepp提供的超类,这里需要的不仅仅是:

import java.util.LinkedList;
import java.util.List;

public class BankingAccount {
private int balance;

private List<String> historyTransaction;
private List<String> historyBalance;

public BankingAccount() {
    historyTransaction = new LinkedList<String>();
    historyBalance = new LinkedList<String>();
}

public BankingAccount(Startup s) {
    this.balance = s.startup_getBalance();
    historyTransaction = new LinkedList<String>();
    historyBalance = new LinkedList<String>();

    historyTransaction.add(valueToHistory(s.startup_getBalance()));
    historyBalance.add(toString());
}

public void debit(Debit d) {
    balance += d.debit_getBalance();

    historyTransaction.add(valueToHistory(d.debit_getBalance()));
    historyBalance.add(toString());
}

public void credit(Credit c) {
    balance += c.credit_getBalance();

    historyTransaction.add(valueToHistory(c.credit_getBalance()));
    historyBalance.add(toString());
}

public int getBalance() {
    return balance;
}

public boolean equals(Object o) {
    if(o instanceof BankingAccount) {
        return (this.getBalance() == ((BankingAccount) o).getBalance());
    }
    return false;
}

private String valueToHistory(int value) {
    int absValue = Math.abs(value);
    return (value < 0 ? "(-" : "") + (absValue / 100) + "." + (absValue % 100 / 10) + (absValue % 100 % 10) + (value < 0 ? ")" : " ");
}

public String toString() {
    int absBalance = Math.abs(balance);
    return (balance < 0 ? "-" : "") + "$" + (absBalance / 100) + "." + (absBalance % 100 / 10) + (absBalance % 100 % 10);
}

public String historyBalanceToString() {
    /*int maxLength = 0;
    for(String piece : historyBalance) {
        maxLength = Math.max(maxLength, piece.length());
    }*/
    int maxLength = 8;

    String build = "";
    for(int i = 0; i < historyBalance.size(); i++) {
        for(int j = 0; j < maxLength - historyBalance.get(i).length(); j++) {
            build += " ";
        }
        build += historyBalance.get(i);
        if(i != historyBalance.size() - 1) {
            build += "\n";
        }
    }

    return build;
}

public String historyTransactionToString() {
    String total = toString() + " ";

    int maxLength = 0;
    for(String piece : historyTransaction) {
        maxLength = Math.max(maxLength, piece.length() + 2);
    }
    maxLength = Math.max(maxLength, total.length() + 2);

    String build = "";
    for(int i = 0; i < historyTransaction.size() - 1; i++) {
        for(int j = 0; j < maxLength - historyTransaction.get(i).length(); j++) {
            build += " ";
        }
        build += historyTransaction.get(i);
        build += "\n";
    }

    build += "+";
    for(int i = 0; i < maxLength - (historyTransaction.get(historyTransaction.size() - 1).length() + 1); i++) {
        build += " ";
    }
    build += historyTransaction.get(historyTransaction.size() - 1);
    build += "\n";

    for(int i = 0; i < maxLength; i++) {
        build += "-";
    }
    build += "\n";

    for(int i = 0; i < maxLength - total.length(); i++) {
        build += " ";
    }
    build += total;

    return build;
}

public static class Startup {
    private int balance;

    public Startup(int balance) {
        this.balance = balance;
    }

    public int startup_getBalance() {
        return balance;
    }
}

public static class Debit {
    private int balance;

    public Debit(int balance) {
        this.balance = balance;
    }

    public int debit_getBalance() {
        return balance;
    }
}

public static class Credit {
    private int balance;

    public Credit(int balance) {
        this.balance = balance;
    }

    public int credit_getBalance() {
        return balance;
    }
}

//  REPLACEME

}

1 个答案:

答案 0 :(得分:0)

你缺少的是你需要在天平变化时记录最小值和最大值,而不是在查询时记录。

如果设置了新的最小值或最大值,则覆盖更改余额的方法并记录最小值/最大值。查询时返回存储的最小值或最大值。