在题目给出的代码之后加入我的代码,一直没有结果。(题目说给出的代码可能有错,但是我找不到)
class Bank{
private Customer[] customers;
private int numOfCustomers;
public Bank(){
customers = new Customer[5];
}
public Customer getCustomer(int i) {
return customers[i];
}
public void addCustomer(String f, String l){
Customer customer = new Customer(f, l);
customers[numOfCustomers++] = customer;
}
public int getNumOfCustomers(){
return numOfCustomers;
}
}
class Account{
protected double balance;
public Account(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public boolean deposit(double amt){
balance += amt;
return true;
}
public boolean withdraw(double amt){
if (amt<=balance){
balance -= amt;
return true;
}else{
return false;
}
}
}
class SavingsAccount extends Account{
public double interestRate;
public SavingsAccount(double balance, double interestRate) {
super(balance);
this.interestRate = interestRate;
}
}
class CheckingAccount extends Account{
public double overdraftProtection;
public CheckingAccount(double balance) {
super(balance);
}
public CheckingAccount(double balance, double overdraftProtection) {
super(balance);
this.overdraftProtection = overdraftProtection;
}
public boolean withdraw(double amt){
if (amt<=balance){
balance -= amt;
return true;
}else if(amt - balance <= overdraftProtection){
balance = 0;
overdraftProtection -= amt-balance;
return true;
}else{
return false;
}
}
}
class Customer{
private String firstname;
private String lastname;
private Account[] account;
private int numOfAccounts;
public Customer(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
account = new Account[5];
}
public String getFirstName() {
return firstname;
}
public String getLastName() {
return lastname;
}
public void addAccount(Account ac){
account[numOfAccounts] = ac;
numOfAccounts++;
}
public int getNumOfAccounts(){
return numOfAccounts;
}
public Account getAccount(int index){
return account[index];
}
}
结果如下图:
这是题目给出的代码块,我觉得我的类应该是没问题的,所以我觉得是这下面出了问题,有大牛能帮忙看一下嘛,xx
/*
* This class creates the program to test the banking classes.
* It creates a set of customers, with a few accounts each,
* and generates a report of current account balances.
*/
//import banking.*;
import java.text.NumberFormat;
public class Main{
public static void main(String[] args) {
Bank bank = new Bank();
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Customer customer;
int curCustomer = 0;
Scanner s = new Scanner(System.in);
int t = s.nextInt();
s.nextLine();
// Create several customers and their accounts according to data
while (t-- > 0) {
String f = s.next();
String l = s.next();
s.nextLine();
bank.addCustomer(f, l);
customer = bank.getCustomer(curCustomer++);
int numAccount = s.nextInt();
s.nextLine();
while (numAccount-- > 0) {
String[] type = s.nextLine().split(" ");
double balance;
double interesOrProtect;
if (type[0] == "C" || type[0] == "c") {
balance = Double.parseDouble(type[1]);
if (type.length == 3) {
interesOrProtect = Double.parseDouble(type[2]);
customer.addAccount(new CheckingAccount(balance,
interesOrProtect));
} else {
customer.addAccount(new CheckingAccount(balance));
interesOrProtect = Double.parseDouble(type[2]);
}
} else if (type[0] == "S" || type[0] == "s") {
balance = Double.parseDouble(type[1]);
interesOrProtect = Double.parseDouble(type[2]);
customer.addAccount(new SavingsAccount(balance,
interesOrProtect));
} else if (type[0] == "A" || type[0] == "a") {
int cIndex = Integer.parseInt(type[1]);
int aIndex = Integer.parseInt(type[1]);
customer.addAccount(bank.getCustomer(cIndex).getAccount(
aIndex));
}
}
}
// Generate a report
System.out.println("CUSTOMERS REPORT");
System.out.println("================");
for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) {
customer = bank.getCustomer(cust_idx);
System.out.println();
System.out.println("Customer: "
+ customer.getLastName() + ", "
+ customer.getFirstName());
for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++ ) {
Account account = customer.getAccount(acct_idx);
String account_type = "";
// Determine the account type
/*** Step 1:
**** Use the instanceof operator to test what type of account
**** we have and set account_type to an appropriate value, such
**** as "Savings Account" or "Checking Account".
***/
// Print the current balance of the account
/*** Step 2:
**** Print out the type of account and the balance.
**** Feel free to use the currency_format formatter
**** to generate a "currency string" for the balance.
***/
}
}
}
}