数组输入时的Atm机器逻辑错误

时间:2017-11-15 18:04:08

标签: java arrays loops

我创建了一个关于ATM机的应用程序,其数组帐户大小为10但是由于一些奇怪的原因,输入只接受0(第一个id),而不接受其他9个ID,它总是显示无效的ID错误消息。我已经坚持了几个小时寻找错误。

与检查id有关的那些将是这两个;

public static boolean hasID(Account acc[], int id){ //this is to check whether the id exist or not.
    for(int i=0; i<acc.length; i++){
        if(id == acc[i].getID()){
            return true;
        }
    }
    return false;
}

public static int gID(Account[] acc){  //this is the login method to initiate the hasID method.
    Scanner sc = new Scanner(System.in);
    int id=0;
    boolean valid = false;
    while(!valid){
        System.out.println("Enter ID: ");
        id = sc.nextInt();

        if(!hasID(acc, id)){
            System.out.println("YOUR ID IS INVALID.");
        } else{
            valid = true;
        }
    }
    return id;
}

public Account(int mID, double mBalance/*, double mInterestRate*/){
    this.id = getID();
    this.balance = getBalance();
    //this.interestRate = getInterestRate();
}

public static Account getAccount(Account acc[], int id){
    for(int i = 0; i<acc.length; i++){
        if(id == acc[i].getID()){
            return acc[i];
        }
    }
    return null;
}

这是我的主要内容:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Account acc[] = new Account[10];
    for(int i = 0; i<acc.length; i++){
        acc[i] = new Account(i, 100.0);
    }

    int a = 0;
    int id = gID(acc);

    while(a != 4){
        Account ac = getAccount(acc,id);
        System.out.println("1: Check Balance");
        System.out.println("2: Withdraw");
        System.out.println("3: Deposit");
        System.out.println("4: Exit");

        System.out.println("Enter your choice: ");
        a = sc.nextInt();
        switch(a){
        case 1:
            System.out.println("Your Balance is: RM " + ac.getBalance());
            break;

        case 2:
            System.out.println("Amount to withdraw: RM ");
            ac.withdraw(sc.nextDouble());
            break;

        case 3:
            System.out.println("Amount to deposit: RM ");
            ac.deposit(sc.nextDouble());
            break;

        case 4:
            id = gID(acc);
            a = 0;
            break;

        default:
            System.out.println("Invalid input!");
        }
    }

}

编辑:帐户构造函数,不使用insterestrate。 id和balance的setter和getter:

public int getID(){
    return id;
}

public void setID(int mID){
    id = mID;
}

public double getBalance(){
    return balance;
}

public void setBalance(double mBalance){
    balance = mBalance;
}

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

public Account(int mID, double mBalance/*, double mInterestRate*/){
    this.id = getID();
    this.balance = getBalance();
    //this.interestRate = getInterestRate();
}

你不应该使用

 public Account(int mID, double mBalance/*, double mInterestRate*/){
    this.id = mID;
    this.balance = mBalance;
    //this.interestRate = getInterestRate();
 } 

getters和setter是FIELDS ENCAPSULATION的一部分,但为了更好的用户体验,我可以建议您使用以下代码

public Account(/*you dont need a parameter*/){
   this.id = setIdByScannedValue();
   this.balance = setBalanceByScannedValue();
   //this.interestRate = setInterestRateByScannedValue();
}

private void setIdByScannedValue(){
    Scanner sc = new Scanner(System.in);
    boolean provided = false;
    while(!provided)
    try{
        System.out.print("provide id: ");
        this.id = sc.nextInt();
    }catch(NumberFormatException e){
        System.out.println("you must provide an integer id format !!!");
    }finally{
        provided = true;
    }
}

我会让你做其他方法,这样你就可以通过自己的方式学习这个想法,我将确保你理解我的代码。