我想编写一个跟踪银行帐户的Java程序
现在我有以下简单程序:
public class account
{
private double balance;
private String owner;
public account(double x, String s) { balance=x; owner=s; }
public String owner() { return owner; }
public void withdraw(double a) { balance -= a; }
public void deposit(double a) { balance += a; }
public void printbalance() { System.out.println(balance); }
// main for testing:
public static void main(String[] argv)
{
account a1 = new account(2000,"you boss");
account a2 = new account(1000,"me nerd");
a1.deposit(400);
a2.withdraw(300000); // not enough money!
a2.withdraw(-500000); // trying to cheat!
a1.printbalance();
a2.printbalance();
}//main
} // account
我想使用aspectj将以下内容添加到该程序中:
1-我想防止帐户提取更多的当前余额并提取负数。
2-我也希望它能防止存入负数。
3-我需要添加一个图形界面,(按钮)
4-添加需要在客户进行交易之前输入的密码或密码
。5-跟踪帐户中进行的所有交易(取款和存款),并在需要时打印报告。
感谢您的帮助。谢谢。
privileged aspect newAccount
{
//withdraw (prevent withdraw negative numbers and number greater than the //current balance)
void around(account a, double x) : execution(void account.withdraw(double)) && target(a) && args(x){
if(x > a.balance){
System.out.println("not enough money!");
return;
}else if(x < 0){
System.out.println("trying to cheat!");
return;
}
proceed(a, x);
}
//Deposit: prevent deposit negative number
void around(double x) : execution(void account.deposit(double)) && args(x){
if(x < 0){
System.out.println("trying to deposit negtive money!");
return;
}
proceed(x);
}
after() : execution(public static void *.main(String[])){
account.a3 = new account(3000,"he nerd");
a3.deposit(-100);
a3.printbalance();
}
//To Do: pin secret password
//To Do: Transaction Record
}
答案 0 :(得分:1)
我可以看到您仍在学习Java,因为您不了解诸如此类的基本编程约定。
您还从特权方面使用直接字段访问,而不是仅为类的字段创建公共getter方法并使用它们。 toString
方法也很有用,因为这样您就可以轻松地打印对象而无需访问getter和构造自己的输出。
此外,在main
方法之后运行的建议是一个不错的实验,但没有太大意义。由于帐户所有者与应用程序中的帐户所有者之一具有相同的名称,因此看起来您好像要入侵该帐户。我在这里注释了代码,以解释为什么它不能那样工作。
我还重构了您的应用程序类和方面,使其看起来像这样,而无需更改功能:
package de.scrum_master.app;
public class Account {
private String owner;
private double balance;
public Account(String owner, double balance) {
this.owner = owner;
this.balance = balance;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
public String getOwner() {
return owner;
}
public double getBalance() {
return balance;
}
@Override
public String toString() {
return "Account[owner=" + owner + ", balance=" + balance + "]";
}
public static void main(String[] argv) {
Account bossAccount = new Account("Boss", 2000);
Account nerdAccount = new Account("Nerd", 1000);
bossAccount.deposit(400);
nerdAccount.withdraw(200);
bossAccount.withdraw(300000); // Cannot withdraw more than account balance
nerdAccount.withdraw(-500000); // Cannot withdraw a negative amount
bossAccount.deposit(-123456); // Cannot deposit a negative amount
System.out.println(bossAccount);
System.out.println(nerdAccount);
}
}
package de.scrum_master.aspect;
import de.scrum_master.app.Account;
public aspect AccountAspect {
// Withdrawal
void around(Account account, double amount) :
execution(void Account.withdraw(double)) &&
target(account) &&
args(amount)
{
if (amount > account.getBalance()) {
System.out.println("Cannot withdraw more than account balance");
return;
}
if (amount < 0) {
System.out.println("Cannot withdraw a negative amount");
return;
}
proceed(account, amount);
}
// Deposit
void around(double amount) :
execution(void Account.deposit(double)) &&
args(amount)
{
if (amount < 0) {
System.out.println("Cannot deposit a negative amount");
return;
}
proceed(amount);
}
// This does not make any sense because
// 1. it happens after the application ends (after leaving main method)
// 2. Even though the account owner is the same as in the main method,
// it does not mean that by creating a new object with the same name
// the "Nerd" can manipulate the original account balance. You have to
// intercept the original Account object and manipulate it directly.
after() : execution(public static void *.main(String[])) {
System.out.println("--- after end of main program ---");
Account account = new Account("Nerd", 3000);
account.deposit(-100);
System.out.println(account);
}
// TODO: PIN secret password
// TODO: transaction record
}
控制台日志将为:
Cannot withdraw more than account balance
Cannot withdraw a negative amount
Cannot deposit a negative amount
Account[owner=Boss, balance=2400.0]
Account[owner=Nerd, balance=800.0]
--- after end of main program ---
Cannot deposit a negative amount
Account[owner=Nerd, balance=3000.0]
我不会为您做家庭作业,但会给您一些提示:
PIN(秘密密码):Account
类需要一个字段pin
,该字段可以在构造函数中设置,并且不应具有公共getter方法,以避免任何人都可以访问PIN码。如果分配要求您不编辑基类而是通过AOP解决问题,则可以使用类型间定义(ITD)来添加私有字段和公共设置器,甚至可以向该类添加其他构造函数。接下来,您将添加一条建议,要求用户是否首次尝试访问某个帐户的任何交易方法(例如,deposit
和withdraw
),请在控制台上输入PIN。正确输入PIN后,他将能够继续操作,否则将出现错误消息,并且交易将被禁止。该方面本身可以为所有Account
对象(可能要使用Set<Account>
)保留一个缓存(临时存储),这些对象在运行会话期间已成功进行身份验证,从而避免了用户再次输入同一帐户的PIN码。
每个帐户的交易记录:同样,您可以使用ITD来将List<TransactionRecord>
之类的字段添加到Account
,用一个空列表初始化它,然后添加一个每次存款或取款的交易记录。您也可以简化概念证明,而不是创建TransactionRecord
辅助类,而仅使用List<Double>
进行交易,记录正的存款金额和负的存款金额。具有诸如“存款123.45”或“提款67.89”之类的元素的List<String>
也是可行的选择。重要的是,您的老师可以看到正确的方面逻辑。