求助

QEC568320642 2011-10-12 02:23:17
我是个新手写了这么一个程序:我的运行结果是这样的

run:
CUSTOMERS REPORT
================

Customer: Simms, Jane
Savings Account:current balance is¥500.00
Checking Account:current balance is¥200.00

Customer: Bryant, Owen
Checking Account:current balance is¥200.00

Customer: Soley, Tim
Savings Account:current balance is¥1,500.00
Checking Account:current balance is¥200.00

Customer: Soley, Maria
Savings Account:current balance is¥150.00
成功生成(总时间:0 秒)
但最后一个人Soley, Maria少了一个Checking Account:current balance is $ 200.00我不知道是什么原因看不出哪有漏洞
希望能给点建议谢谢

下面的这才是正确答案对于钱的符号不做要求

CUSTOMERS REPORT
Customer:Simms,Jane
Savings Account:current balance is $500.00
Checking Account:current balance is $200.00
Customer:Bryant,Owen
Checking Account:current balance is $200.00
Customer:Soley, Tim
Savings Account:current balance is $1,500.00
Checking Account:current balance is $ 200.00
Customer:Soley, Maria
Checking Account:current balance is $ 200.00
Savings Account:current balance is $ 150.00



package exercise2;

public class Account {

protected double balance;

public Account(double init_balance) {
this.balance = init_balance;
}

protected double getBalance() {
return balance;
}

public boolean deposit(double amt) {
balance += amt;
return true;

}

public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
return false;
}
}
}




package exercise2;
public class Bank{
private Customer[] customers;
private static int numberOfCustomers;

public Bank() {
customers = new Customer[5];
}

public void addCustomer(String firstname,String Lastname)
{
Customer customer=new Customer(firstname, Lastname);
customers[numberOfCustomers]=customer;
numberOfCustomers++;


}

public static int getNumberOfCustomers() {
return numberOfCustomers;
}

public Customer getCustomers(int int_index) {
return customers[int_index];
}



}

package exercise2;

public class CheckingAccount extends Account {

private double overdraftProtection;

public CheckingAccount(double balance) {
super(balance);

}

public CheckingAccount(double balance, double overdraftProtection) {
super(balance);
this.overdraftProtection = overdraftProtection;
}

@Override
public boolean withdraw(double amt) {
if (balance >= amt) {
balance -= amt;
return true;
} else {
if (amt - balance <= overdraftProtection) {
balance -= amt;

return true;
} else {
return false;
}

}
}
}


package exercise2;

public class Customer {

private Account[] accounts= new Account[7];
private String firstname, Lastname;
private static int numberOfAccounts;

public Customer(String firstname, String Lastname) {
this.firstname = firstname;
this.Lastname = Lastname;
}



public void addAccount(Account account) {
accounts[numberOfAccounts] = account;
numberOfAccounts++;
}

public Account getAccounts(int in_index) {
return accounts[in_index];
}

public static int getNumberOfAccounts() {
return numberOfAccounts;
}

public String getLastname() {
return Lastname;
}

public String getFirstname() {
return firstname;
}
}


package exercise2;

public class SavingsAccount extends Account {

private double interestRate;

public SavingsAccount(double balance, double interestRate) {
super(balance);


}
}


package exercise2;

import java.text.NumberFormat;

public class TextBanking {

public static void main(String[] args) {
NumberFormat currency_format = NumberFormat.getCurrencyInstance();
Bank bank = new Bank();
Customer customer;

// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomers(0); //该客户两个卡
customer.addAccount(new SavingsAccount(500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00, 400.00));

bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomers(1); //该客户一个卡
customer.addAccount(new CheckingAccount(200.00));

bank.addCustomer("Tim", "Soley");
customer = bank.getCustomers(2);
customer.addAccount(new SavingsAccount(1500.00, 0.05));
customer.addAccount(new CheckingAccount(200.00)); //该客户两个卡

bank.addCustomer("Maria", "Soley");
customer = bank.getCustomers(3);
// Maria and Tim have a shared checking account
customer.addAccount(bank.getCustomers(2).getAccounts(1));//该客户分享上一客户的
//第二个卡
customer.addAccount(new SavingsAccount(150.00, 0.05));//该客户自己又单独有一个
//一共两个

// Generate a report
System.out.println("\t\t\tCUSTOMERS REPORT");
System.out.println("\t\t\t================");

for (int cust_idx = 0; cust_idx < bank.getNumberOfCustomers(); cust_idx++) {
{
customer = bank.getCustomers(cust_idx);

System.out.println();
System.out.println("Customer: "
+ customer.getLastname() + ", "
+ customer.getFirstname());

for (int acct_idx = 0; acct_idx < customer.getNumberOfAccounts(); acct_idx++) {
{
Account account = customer.getAccounts(acct_idx);
String account_type = "Savings Account";
if (account instanceof SavingsAccount) {
System.out.println(account_type + ":current balance is" + currency_format.format(account.getBalance()));

} else {
if (account instanceof CheckingAccount) {
System.out.println("Checking Account" + ":current balance is" + currency_format.format(account.getBalance()));
}
}


}
}
// 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.
***/
}
}
}
}

...全文
57 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
QEC568320642 2011-10-12
  • 打赏
  • 举报
回复
不好意思我还是一个初学者呵呵

那再问你们一个问题上面打出的是人民币符号怎么显示美元符号呢
菖蒲老先生 2011-10-12
  • 打赏
  • 举报
回复
TextBanking里
customer.addAccount(bank.getCustomers(2).getAccounts(1));//该客户分享上一客户的

改成

customer.addAccount(bank.getCustomers(2).getAccounts(4));//该客户分享上一客户的

你写的太难懂了,自己看看吧。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧