我写了下面的关于银行账号的程序但运行有错求帮助:
package exercisel.banking;
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(amt<=balance)
{
balance-=amt;
return true;
}
else
return false;
}}package exercisel.banking;
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 customer_index) {
return customers[customer_index];
}
}
package exercisel.banking;public class CheckingAccount extends Account { private double overdraftProtection; public CheckingAccount(double balance) {
super(balance); } public CheckingAccount(double balance, double protect) {
super(balance);
this.overdraftProtection = protect;
} @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 exercisel.banking;
public class Customer {
private Account account;
private String firstname,Lastname; public Customer(String firstname, String Lastname) {
this.firstname = firstname;
this.Lastname = Lastname;
} public Account getAccount() {
return account;
}
public void setAccount(Account account)
{
this.account=account; } public String getLastname() {
return Lastname;
} public String getFirstname() {
return firstname;
}
}package exercisel.banking;public class SavingsAccount extends Account{ private double interestRate; public SavingsAccount(double balance, double interestRate) {
super(balance); }
}package exercisel.banking;public class TestBanking { public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account; //
// Create bank customers and their accounts
// System.out.println("Creating the customer Jane Smith.");
bank.addCustomer("Jane", "Simms");
//code
customer = bank.getCustomers(0);
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
//code
SavingsAccount savingsaccount = new SavingsAccount(500.00, 0.03);
System.out.println("Creating the customer Owen Bryant.");
//code
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomers(1);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
//code
CheckingAccount checkingaccount1 = new CheckingAccount(500.00);
System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomers(2);
System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
//code
CheckingAccount checkingaccount2 = new CheckingAccount(500.00, 500.00);
System.out.println("Creating the customer Maria Soley.");
//code
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomers(3);
System.out.println("Maria shares her Checking Account with her husband Tim.");
customer.setAccount(bank.getCustomers(2).getAccount()); System.out.println(); //
// Demonstrate behavior of various account types
// // Test a standard Savings Account
System.out.println("Retrieving the customer Jane Smith with her savings account.");
customer = bank.getCustomers(0);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account w/o overdraft protection
System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
customer = bank.getCustomers(1);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
customer = bank.getCustomers(2);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
customer = bank.getCustomers(3);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Deposit 150.00: " + account.deposit(150.00));
System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); }
}
请帮我写明原因谢谢
package exercisel.banking;
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(amt<=balance)
{
balance-=amt;
return true;
}
else
return false;
}}package exercisel.banking;
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 customer_index) {
return customers[customer_index];
}
}
package exercisel.banking;public class CheckingAccount extends Account { private double overdraftProtection; public CheckingAccount(double balance) {
super(balance); } public CheckingAccount(double balance, double protect) {
super(balance);
this.overdraftProtection = protect;
} @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 exercisel.banking;
public class Customer {
private Account account;
private String firstname,Lastname; public Customer(String firstname, String Lastname) {
this.firstname = firstname;
this.Lastname = Lastname;
} public Account getAccount() {
return account;
}
public void setAccount(Account account)
{
this.account=account; } public String getLastname() {
return Lastname;
} public String getFirstname() {
return firstname;
}
}package exercisel.banking;public class SavingsAccount extends Account{ private double interestRate; public SavingsAccount(double balance, double interestRate) {
super(balance); }
}package exercisel.banking;public class TestBanking { public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
Account account; //
// Create bank customers and their accounts
// System.out.println("Creating the customer Jane Smith.");
bank.addCustomer("Jane", "Simms");
//code
customer = bank.getCustomers(0);
System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
//code
SavingsAccount savingsaccount = new SavingsAccount(500.00, 0.03);
System.out.println("Creating the customer Owen Bryant.");
//code
bank.addCustomer("Owen", "Bryant");
customer = bank.getCustomers(1);
System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
//code
CheckingAccount checkingaccount1 = new CheckingAccount(500.00);
System.out.println("Creating the customer Tim Soley.");
bank.addCustomer("Tim", "Soley");
customer = bank.getCustomers(2);
System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
//code
CheckingAccount checkingaccount2 = new CheckingAccount(500.00, 500.00);
System.out.println("Creating the customer Maria Soley.");
//code
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomers(3);
System.out.println("Maria shares her Checking Account with her husband Tim.");
customer.setAccount(bank.getCustomers(2).getAccount()); System.out.println(); //
// Demonstrate behavior of various account types
// // Test a standard Savings Account
System.out.println("Retrieving the customer Jane Smith with her savings account.");
customer = bank.getCustomers(0);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account w/o overdraft protection
System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
customer = bank.getCustomers(1);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
customer = bank.getCustomers(2);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Withdraw 150.00: " + account.withdraw(150.00));
System.out.println("Deposit 22.50: " + account.deposit(22.50));
System.out.println("Withdraw 47.62: " + account.withdraw(47.62));
System.out.println("Withdraw 400.00: " + account.withdraw(400.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); System.out.println(); // Test a Checking Account with overdraft protection
System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
customer = bank.getCustomers(3);
account = customer.getAccount();
// Perform some account transactions
System.out.println("Deposit 150.00: " + account.deposit(150.00));
System.out.println("Withdraw 750.00: " + account.withdraw(750.00));
// Print out the final account balance
System.out.println("Customer [" + customer.getLastname()
+ ", " + customer.getFirstname()
+ "] has a balance of " + account.getBalance()); }
}
请帮我写明原因谢谢
解决方案 »
- 一个关于java流程控制的例子,但无法达到预定目的,始终无法执行大部分代码
- JAVA俄罗斯方块
- swt 中CTabFolder问题
- 关于calendar的问题?
- 怎样让JEditorPane中的textarea自动换行?
- 百思不得其解:环境配置好后,java可运行,javac不能运行,注销系统后再登录才可以运行
- 有了解ZK框架的吗,一个可以像开发桌面程序那样开发ajax应用的框架
- 我是初学者“请问JBuilder下怎么导入自己的图象和图标啊”?
- 请高手指教下java在editplus里的用法!waiting!!!
- JNI可以调MFC写的DLL吗??
- java crossword编程
- 有网络编程经验的大牛和架构师们进一下 谢谢
public Customer getCustomers(int customer_index) {
Account a=new Account(100);
customers[customer_index].setAccount(a);
return customers[customer_index];
}
因为你的custom中account是没赋值的
在你的代码中没看到这个。就算是有这个,也不对呀。Account是Customer对象的一个属性。你在对Customer对象初始化的时候,并没有对其初始化。但你也可以在使用时对其初始化。但你也没有这样的操作(这个就是5楼给你的建议了)。所以Accout将返回其默认是null