0
回答
有没有高手指导写一个Junit test case,很简单的银行baking系统,谢谢。
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
package comp311.banking;

import java.text.NumberFormat;

import comp311.banking.exceptions.InsufficientFundsException;
import comp311.banking.exceptions.NegativeAmountException;

public abstract class BankAccount {
	private static long lastAccountNo = 789000;
	protected long accNo;
	protected double balance;
	protected Owner owner;
	
	public BankAccount(Owner owner) {
		this.owner = owner;
		this.accNo = ++lastAccountNo;
		balance = 0.0;
	}
	
	public double deposit(double amount) throws NegativeAmountException {
		if (amount < 0.01 ) {
			throw new NegativeAmountException("Deposit must be at least $0.01");
		}
		balance += amount;
		return balance;
	}
	
	public double withdraw(double amount)throws NegativeAmountException, InsufficientFundsException {
		if (amount < 0.01 ) {
			throw new NegativeAmountException("Withdraw must be at least $0.01");
		}
		if (amount > balance ) {
			throw new InsufficientFundsException("Withdrawal that results in negative balance not allowed");
		} else { 	  
			balance -= amount;
		}
		return balance;
	}
	
	public double transfer( BankAccount ba, double amount) throws NegativeAmountException, InsufficientFundsException {
		withdraw(amount);
		ba.deposit(amount);
		return balance;
	}

	public long getAccNo() {
		return accNo;
	}

	public double getBalance() {
		return balance;
	}
	
	public String getFormattedBalance() {
		NumberFormat formatter = NumberFormat.getCurrencyInstance();
		return formatter.format(balance);
		
	}

	public Owner getOwner() {
		return owner;
	}
	
	public String toString() {
		String acctInfo =  "Account Number: " + getAccNo() + " ";
		acctInfo += "   Owner: " + owner.getName() + "  ";
		acctInfo += "   Balance: " + getFormattedBalance();
		return acctInfo;
	}
}

package comp311.banking;

public class BankMain {

	public static void main(String[] args) {
		SavingsAccount sa = null;
		ChequingAccount ca = null;
		try {
			Owner owner = new Owner("A Person");
			sa = new SavingsAccount(owner);
			ca = new ChequingAccount(owner);
			sa.deposit(500.0);
			ca.deposit(100.0);
			sa.withdraw(25.0);
			ca.withdraw(25.0);
			ca.transfer(sa, 50.0);
			ca.withdraw(44.0);
			System.out.println(owner);
			ca.withdraw(100.0);
			System.out.println(sa);
			System.out.println(ca);

			Owner owner2 = new Owner("Person's wife");
			SavingsAccount sa2 = new SavingsAccount(owner2);
			sa2.deposit(1000.0);
			sa2.transfer(sa, 200.0);
			System.out.println(owner);
			System.out.println(owner2);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



package comp311.banking;

import comp311.banking.Owner;
import comp311.banking.exceptions.AccountOwnerException;
import comp311.banking.exceptions.InsufficientFundsException;
import comp311.banking.exceptions.NegativeAmountException;

public class ChequingAccount extends BankAccount {

	public ChequingAccount(Owner owner) throws AccountOwnerException {
		super(owner);
		owner.setCa(this);
	}

	public String toString() {
		return "Chequing " + super.toString();
	}
	
	public double withdraw( double amount) throws NegativeAmountException, InsufficientFundsException {
		if (amount < 0.01 ) {
			throw new NegativeAmountException("Withdraw must be at least $0.01");
		}
		if (amount > balance ) { 
			SavingsAccount sa = owner.getSa();
			if ( sa == null ) { 
				throw new InsufficientFundsException();
			} else 
				sa.transfer( this, amount - balance);
			}
		balance -= amount;
		return balance;
	}
}



package comp311.banking;

import comp311.banking.SavingsAccount;
import comp311.banking.exceptions.AccountOwnerException;
import comp311.banking.exceptions.CustomerNameException;

public class Owner {
	String name;
	ChequingAccount ca;
	SavingsAccount sa;

	public String getName() {
		return name;
	}

	public void setName(String name) throws CustomerNameException {
		if (name == null || name.length() < 1) {
			throw new CustomerNameException("Account owner must have a name");
		}
		this.name = name;
	}

	public ChequingAccount getCa() {
		return ca;
	}

	public void setCa(ChequingAccount ca) throws AccountOwnerException {
		if (ca.getOwner() != null && ca.getOwner().getCa() != null ) {
			throw new AccountOwnerException("Attempt to change ownership of account " + ca.getAccNo());
		}
		this.ca = ca;
	}

	public SavingsAccount getSa() {
		return sa;
	}

	public void setSa(SavingsAccount sa)  throws AccountOwnerException {
		if (sa.getOwner() != null && sa.getOwner().getSa() != null ) {
			throw new AccountOwnerException("Attempt to change ownership of account " + sa.getAccNo());
		}
		this.sa = sa;
	}

	public Owner(String name) throws CustomerNameException {
		if (name == null || name.length() < 1) {
			throw new CustomerNameException("Account owner must have a name");
		}
		this.name = name;
		ca = null;
		sa = null;
	}

	public String toString() {
		String ownerInfo = "Bank customer: " + getName();
		if (getSa() != null) {
			ownerInfo += "\n     Savings account:  " + getSa().getAccNo() + "  "
					+ getSa().getFormattedBalance();
		}
		if (getCa() != null) {
			ownerInfo += "\n     Chequing account: " + getCa().getAccNo() + "  "
					+ getCa().getFormattedBalance();
		}
		return ownerInfo;
	}

}



package comp311.banking;

import comp311.banking.Owner;
import comp311.banking.exceptions.AccountOwnerException;

public class SavingsAccount extends BankAccount {

	public SavingsAccount(Owner owner) throws AccountOwnerException {
		super(owner);
		owner.setSa(this);
	}

	public String toString() {
		return "Savings  " + super.toString();
	}
}



test class:

AccountOwnerException,CustomerNameException,InsufficientFundsException,NegativeAmountException
谢谢。



举报
lukeyang
发帖于3年前 0回/386阅
顶部