首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OOP ATM应用

OOP ATM应用
EN

Code Review用户
提问于 2018-01-14 21:55:59
回答 1查看 17.7K关注 0票数 2

作为我研究的一部分,我需要收集同伴反馈作为我面向对象的ATM应用程序的一部分。我只使用过类(继承等)这不需要GUI。你能不能给我在编码方面的反馈,以及我如何改进它?

要求:

  • 使用Java的面向对象应用程序
  • 能够处理存款/客户帐户(使用继承)的标准ATM和ATM机模型的应用程序
  • ATM将显示余额(在标准ATM中显示)
  • 每天最多可提款300 on (视余额而定)。
  • PIN的更改(与此做斗争,所以没有实现它)
  • 存款自动取款机应允许每日存款高达100 of。
  • 如果余额不足,两台自动取款机都会阻止取款。
  • 客户可在正确组合帐户/个人识别码后获得服务。
  • 在连续三次尝试错误的帐户/PIN组合后,帐户将被禁用。

customer.java (ArrayList)

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.List;

public class Customer {
private String acctNo;
private String PIN;
private double balance;
private int attempt;

public Customer(String acctNo, String PIN, double balance, int attempt) {
    super();
    this.acctNo = acctNo;
    this.PIN = PIN;
    this.balance = balance;
    this.attempt = attempt;
}
public int getAttempt() {
    return attempt;
}
public void setAttempt(int attempt) {
    this.attempt = attempt;
}
public String getAcctNo() {
    return acctNo;
}
public void setAcctNo(String acctNo) {
    this.acctNo = acctNo;
}
public String getPIN() {
    return PIN;
}
public void setPIN(String PIN) {
    this.PIN = PIN;
}
public double getBalance() {
    return balance;
}
public void setBalance(double balance) {
    this.balance = balance;
}

存款自动取款机

代码语言:javascript
复制
   public class DepositAtm extends StandardAtm { //subclass //variables used 
    from superclass: StandardATM

    private Customer cust1;
    private double balance;

//constructor
public DepositAtm(Customer customer) 
    {
    super(customer);
    this.cust1 = customer;
    this.balance = balance;
    }


public void depositCash(int amount) 
{
    if (amount > 0 && amount < 100 && cust1.getBalance() >= amount) 
    { 
            if (cust1.getBalance() >= amount)//compare balance to the amount to deposit + amount = balance(amount) + amount;
            { 

                cust1.setBalance(cust1.getBalance()+amount); //set the new balance
                System.out.println("Deposit Complete. New Balance:  £"+ cust1.getBalance());

            }
    } else 
    {
        System.out.println("Transaction cancelled due to insufficient funds. Please retry:"); 

}
}

标准ATM

代码语言:javascript
复制
import java.util.ArrayList; import java.util.List;

public class StandardAtm { //declaration of variables

private Customer cust1;
List cust = new ArrayList<>(); //an list for number of customers
Boolean accessed = false;


//constructor
public StandardAtm (Customer customer) {
    this.cust1 = customer;
}

public void displayBalance() 
{       
    if (accessed = true) 
    {
    System.out.println("Balance: "+cust1.getBalance()); //displays the balance
    }
}


public void accessacc(String acctNo, String PIN)  //log in method
{ 
    if (cust1.getAcctNo().equals(acctNo) && cust1.getPIN().equals(PIN)) 
        {
        System.out.println("Welcome! Your account balance is: £"+cust1.getBalance());

        } 

    else 
    {   
        //for (int attempt=1; attempt<=3;attempt++) //1. limit the number of attempts 

        /*if (attempt == 3 && cust1.getAcctNo().equals(acctNo) != cust1.getPIN().equals(PIN))
        { //1. find the opposite of equals
           System.out.println("Invalid Account Number & PIN! " + "Attempt: "+attempt);
        }*/

        if (cust1.getAcctNo().equals(acctNo) != cust1.getPIN().equals(PIN))
             cust1.setAttempt(cust1.getAttempt()+1);
        {                                                   
           System.out.println("Invalid Account Number & PIN! " + "Attempt: "+ cust1.getAttempt());

         if (cust1.getAttempt() >= 3) 
            {
                System.out.println("Number of attempts exceeded. Please contact your bank. ");


           }
        }
    }
}

public void withdrawCash(int amount) 
{ 
    if (accessed=true) 
    { 
    if (amount > 0 && amount < 301 && cust1.getBalance() >= amount)  //nested statements are required to run a number of commands
    { 
        if(amount > 0 && amount < 301) 
        { 
            if (cust1.getBalance() >= amount)  //compare balance to the amount you want to withdraw if amount to withdraw is same as balance
            {
                    cust1.setBalance(cust1.getBalance()-amount); //set the new balance
                    System.out.println("Withdrawn complete! New Balance : £"+ cust1.getBalance());
            }
        } else 
        {
        if (accessed = false) 
        System.out.println("Transaction cancelled: please enter correct amount"); 

       }
  }
}

试验传动

代码语言:javascript
复制
public class Testdrive {

public static void main(String[] args) {
    Customer cust1 = new Customer("123456", "1224", 50, 0); // New customer object

    StandardAtm SA1 = new StandardAtm(cust1);//created a standard ATM object and passed cust1 in it

    SA1.accessacc("123456", "1224");//given SA1 an account number: the printout method should not work
    //SA1.withdrawCash(100);

    DepositAtm SA2 = new DepositAtm(cust1); // created a deposit ATM object and passed cust1 through it
    SA2.depositCash(50);

}
EN

回答 1

Code Review用户

回答已采纳

发布于 2018-01-15 08:54:41

让我们按严重性顺序列出注释:

Bugs

有几个我可以看到:

  1. 这个if语句if (cust1.getAcctNo().equals(acctNo) != cust1.getPIN().equals(PIN))肯定没有做它想做的事情。此外,这是多余的,因为你已经要求相等的计算和引脚。因此,在else子句中,您知道至少有一个字段不是正确的值。
  2. 您从未将accessed变量设置为true
  3. balance实例变量在构造函数之外从未使用过DepositATM
  4. cust1实例变量被声明为私有变量。这意味着两个ATM类中的每一个都有自己的私有变量。我不明白这是为了什么,因为-
  5. 你的客户名单永远不会被填充。事实上,在当前状态下,ATM机器的实例只能处理一个客户。该列表也是名称cust,具有误导性。至少应该是类似于customers
  6. 在两个ATM类(列表和布尔)之间共享的两个变量在默认的“包私有”访问器中声明。尽管如此,它还是可以工作的(它们可以被两个ATM类访问和修改),这意味着不是ATM但在同一个包中的类也可以访问这些变量。这些变量应该声明为protected,以便它们仅由ATM层次结构树共享。
  7. withdrawCash()中嵌套的if语句是多余的。也许这一评论是试图作出解释,但既不明确也不正确。
  8. 演习指出,提款和存款限额为每天。这个问题在解决方案中没有得到解决。

设计

正如bug部分中提到的,ATM的设计需要为每个客户创建一个新的ATM实例。为了解决这个问题,让我们想象一下一个真正的ATM是如何工作的:

一台自动取款机可以为多家银行的客户提供服务。当客户输入卡和PIN号码时,ATM机识别银行并将这些细节发送给银行。银行持有其客户名单,并负责对其客户进行认证。

认证过程结束后,ATM将得到一个Customer实例。ATM将使用此实例执行余额查询和修改。

在处理结束时,当客户取回他/她的信用卡时,自动柜员机应将Customer对象发送回银行,以便它可以更新其内部记录。

将其转化为面向对象的设计。我们有以下课程:

一个与您所拥有的类非常类似的Customer类。Bank类,包含Customers的List (或任何其他收集/数据结构)。Bank公开getCustomer(int acctNo, int PIN)updateCustomer(Customer cust)方法。getCustomer也执行身份验证。ATM类在构造函数中获取Banks的D44(或任何其他收集/数据结构),并且应该能够从客户的帐户号码中识别银行。我们可以说前两位数是银行ID。

这种设计既健壮又模块化。它允许每家银行进行自己的记账、客户授权等操作。如果这看起来有点过分,那么您可以说ATM处理来自一家银行的客户(请务必在评论中提到这一点,这样就知道您没有遗漏这个收银员)。在这种情况下,ATM应该维护客户列表(可能在构造函数中获取列表)。

最佳实践

  1. 不要在代码中使用文字和常量。每个文字都应该定义为一个静态的最终变量。这样做的原因是: 1)它为文字赋予了名称和意义;2)只有一个地方指定了文字值,从而使修改该值变得更容易。示例:public static final int MAX_DAILY_WITHDRAWAL = 300;
  2. 您不必初始化构造函数中的所有实例变量。其中一些可能具有默认值。一个新的customer总是没有尝试,所以为什么每次创建新实例时都指定这个值呢?
  3. accessed变量被声明为Boolean,它是一个对象,而不是原始boolean类型。这意味着每次赋值或将变量与值进行比较时,都需要执行其他操作来访问和修改变量。将变量声明为基本boolean类型更为有效。
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/185110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档