ssc85
2017-10-08 12:21新手在线等~Exception in thread ~
40package banking;
public class Account {
protected double balance;
public Account(double balance) {
super();
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public boolean deposit(double amt) {
balance+=amt;
return true;
}
public boolean withdraw(double amt) {
if(amt<=balance) {
balance -= amt;
}else {
return false;
}
return true;
}
}
package banking;
public class SavingAccount extends Account {
private double interestRate;
public SavingAccount(double balance, double interestRate) {
super(balance);
this.interestRate = interestRate;
}
public double getInterestRate() {
return interestRate;
}
}
package banking;
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;
}
public boolean withdraw(double amt) {
if(amt<=balance) {
balance -= amt;
return true;
}else {
if(overdraftProtection>= amt - balance) {
overdraftProtection -= amt - balance;
balance = 0;
}else {
return false;
}
}
return true;
}
}
package banking;
public class Customer {
private String lastName;
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
private String firstName;
Account[] accounts;
int numberOfAccounts;
public Customer(String L,String F) {
lastName = L;
firstName =F;
}
public Customer() {
accounts = new Account[2];
}
public void addAccount(Account account) {
accounts[numberOfAccounts++]=account;
}
public Account getAccounts(int index) {
return accounts [index];
}
public int getNumberOfAccounts() {
return numberOfAccounts;
}
}
package banking;
public class Bank {
private Customer[] customers;
private int numberOfCustomers;
public Bank() {
customers = new Customer[5];
}
public Customer getCustomers(int index) {
return customers[index];
}
public int getNumberOfCustomers() {
return numberOfCustomers;
}
public void addCustomer(String L,String F) {
Customer customer = new Customer(L,F);
customers[numberOfCustomers]=customer;
numberOfCustomers++;
}
}
package banking;
public class TestBanking {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer;
bank.addCustomer("Jane", "Smith");
customer = bank.getCustomers(0);
customer.addAccount(new SavingAccount(500,0.05));
customer.addAccount(new CheckingAccount(200,400));
bank.addCustomer("oven", "Bryant");
customer = bank.getCustomers(1);
customer.addAccount(new CheckingAccount(200));
bank.addCustomer("Tim", "Soley");
customer=bank.getCustomers(2);
customer.addAccount(new SavingAccount(1500,0.05));
customer.addAccount(new CheckingAccount(200));
bank.addCustomer("Maria", "Soley");
customer = bank.getCustomers(3);
customer.addAccount(bank.getCustomers(2).getAccounts(1));
customer.addAccount(new SavingAccount(150,0.05));
for(int i = 0;i<bank.getNumberOfCustomers();i++) {
customer = bank.getCustomers(i);
System.out.println();
System.out.println("Customer "+customer.getLastName()+customer.getFirstName());
}
}
}
错误是Exception in thread "main" java.lang.NullPointerException
at banking.Customer.addAccount(Customer.java:28)
at banking.TestBanking.main(TestBanking.java:13)
不明白哪里错了,刚刚自学JAVA的小萌新,希望能够详细解答,并给出修改方案~
- 点赞
- 回答
- 收藏
- 复制链接分享
5条回答
为你推荐
- Java TCP文件服务器,下载到的文件和原文件大小不一致
- tcp
- socket
- java
- 0个回答
- myeclipse7.0 配置SSH问题?
- spring
- 0个回答
- 偶是Spring新手,做试验的时候遇到了点问题(关于加载配置文件),请大虾们不吝赐教!
- spring
- 0个回答
- 急!!!使用shiro+ssm时自定义Realm项目运行报错
- java
- 3个回答
- 哪位大神帮我处理一下这个错啊,再下新手
- exception
- arraylist
- stringbuilder
- 8个回答