【发布时间】:2014-07-30 23:43:27
【问题描述】:
我遇到了这个问题。对于这个问题,我要写一个与超类交互的子类。
一家公司编写了一个大型类 BankingAccount,其中包含许多方法,包括:
方法/构造函数和描述:
- public BankingAccount(Startup s) 使用 Startup 对象 s 中的信息构造一个 BankingAccount 对象
- public void debit(Debit d) 记录给定的借记
- public void credit(Credit c) 记录给定的信用
- public int getBalance() 以美分返回当前余额
我要设计一个新的 MinMaxAccount 类,它的实例可以用来代替 BankingAccount 对象,但包括记住该帐户曾经记录的最小和最大余额的新行为。您应该提供与超类相同的方法,以及以下新行为:
方法/构造函数和描述:
- public MinMaxAccount(Startup s) 使用 构造 MinMaxAccount 对象
- Startup 对象中的信息
- 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
}
【问题讨论】:
-
使用
if而不是while会让您更接近解决方案。
标签: java inheritance superclass