Java ATM项目逻辑错误

时间:2013-10-27 07:25:52

标签: java function loops logic switch-statement

所以这是我的源代码:

import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
public class ATM {

String names []= {"Nikhil", "Furrqaan", "Raj", "Saron", "Abishiek", "Aditya"};
static int pin_no;static int card_no;
static int card[]={1806978,1244668,5769124,7658301,6571354,5155499};
static int pin[]={4444, 3333, 2222, 1111, 6666, 7988};
int balance []={17867, 23345, 24670, 37532, 43637, 37356};
int account[]={219075286,156482798,456482748,465295772,665793758,565738957};
int f,ch,z;
static int r = 0;
static int opt=0;
Scanner s=new Scanner(System.in);
public static void main(String args []) {
    int pincode=0;      
    int repeat=0;   
    ATM k=new ATM();
    do{
    k.info();
    k.login();
    for(int choice=0;choice<=5;choice++)
    if(card_no==card[choice] && pin_no==pin[choice]){
    k.menu(choice);
    break;
    }else{
    System.out.println("Wrong card no. or pincode");
    break;
    }
    k.End(repeat);
    }while(k.End(repeat)==0);

}

   void info(){
   Calendar calender = Calendar.getInstance();
   SimpleDateFormat dateformatter = new SimpleDateFormat(" dd EEEEEEEEE',' yyyy ");
   SimpleDateFormat timeformatter = new SimpleDateFormat(" hh:mm:ss a");
   System.out.println("");
   System.out.println("                  ****************************************************************       ");
   System.out.println("                  ***                         HSBC's  ATM                     ****       ");
   System.out.println("                  ****************************************************************       ");
   System.out.println("                  Date - "+ dateformatter.format(calender.getTime()));
   System.out.println("                  Time - "+timeformatter.format(calender.getTime()));
   System.out.println("                  ****************************************************************       ");
   System.out.println("");
   }

  void login (){
   System.out.println("                  >> Enter card number - ");
   card_no=s.nextInt();
   System.out.println("                  >> Enter pin number - ");
   pin_no= s.nextInt();
  }

void menu (int choice){
    System.out.println("                  Welcome "+names[choice]);
    System.out.println("                  Your Account no: is " + account[choice]);
    System.out.println("                  ###############################################        ");
    System.out.println("                  >>> [1]. Cash Withdrawal ");
    System.out.println("");
    System.out.println("                  >>> [2]. Fast Cash ");
    System.out.println("");
    System.out.println("                  >>> [3]. Cash Deposit ");

     opt=s.nextInt();

    switch (opt){
    case 1:
        Calendar calender = Calendar.getInstance();
        SimpleDateFormat dateformatter = new SimpleDateFormat(" dd'/'MM ");
        SimpleDateFormat timeformatter = new SimpleDateFormat(" HH:mm ");
        ATM k=new ATM();
        int ammount;
        System.out.println("                  ==========================  ");
        System.out.println("                  CASH WITHDRAWAL ");
        System.out.println("                  ==========================  ");
        System.out.print("                    Enter amount to withdraw - Rs.");
        ammount=s.nextInt();
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        System.out.println("                  Please wait while we process your request...");

    if (ammount>balance[z]){
        System.out.println("                  You do not have sufficient balance in your account.");
        k.menu(choice);
        for(z=1;z<=1000000000;z++)
        System.out.print("\f");
    }else if (ammount>25000){
        System.out.println("                  The daily limit for cash withdrawal is Rs.25000 .");
        k.menu(choice);
    }else{
        balance[z]=balance[z]-ammount;

        System.out.println("");
        System.out.println("                  You have withdrawn Rs."+ammount+" from your account.");
        System.out.println("                  Please collect the cash.");
        System.out.println("                  Current Balance - Rs."+balance[z]);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
       }
        break; 
    case 2:
        int fast[]={100,500,1000,5000};
        System.out.println("                  ==========================  ");
        System.out.println("                  FAST CASH WITHDRAWAL ");
        System.out.println("                  ==========================  ");
        System.out.println("                  Please select a desired amount.");
        System.out.println("                  (1) Rs. 100 ");
        System.out.println("                  (2) Rs. 500 ");
        System.out.println("                  (3) Rs. 1000 ");
        System.out.println("                  (4) Rs. 5000 ");
        System.out.print("                     Your choice ? ");
        ch=s.nextInt();
        if (fast[ch-1]>balance[z])
        {
        System.out.println("                  You do not have sufficient balance in your account.");
        }else{
            switch(ch)
            {
            case 1:
            balance[z]=balance[z]-fast[ch-1];
            break;
            case 2:
            balance[z]=balance[z]-fast[ch-1];
            break;
            case 3:
            balance[z]=balance[z]-fast[ch-1];
            break;
            case 4:
            balance[z]=balance[z]-fast[ch-1];
            break;
            }
        }
        System.out.println("");
        System.out.println("                  You have withdrawn Rs. "+fast[ch-1]+" from your account.");
        System.out.println("                  Please collect the cash.");
        System.out.println("                  Current Balance - Rs."+balance[z]);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        break;
    case 3:
        System.out.println("                  ==========================  ");
        System.out.println("                  CASH DEPOSIT ");
        System.out.println("                  ==========================  ");
        System.out.print("                  Enter amount to deposit - Rs.");
        int deposite =s.nextInt();
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        System.out.println("                  Please wait while we process your request...");
        System.out.println("");
        System.out.println("                  You have deposited Rs."+deposite+" to your account.");
        System.out.println("                  The amount will be credited shortly.");
        System.out.println("                  Current Balance - Rs."+balance[z]+" + Rs."+deposite);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        break;
    case 4:
        System.out.println("                  ==========================  ");
        System.out.println("                  BALANCE ENQUIRY ");
        System.out.println("                  ==========================  ");
        System.out.println("                  Account Number - "+card[z]);
        System.out.println("                  Available Balance - Rs."+balance[z]);
        System.out.println("                  ::::::::::::::::::::::::::::::::::::::::::::::");
        System.out.println("                  Please collect the reciept...");
  }
}
 int End(int repeat){
    ATM k=new ATM();
    System.out.println();
    System.out.println("                      What would you like to do next?           ");
    System.out.println("                      [1]. Enter main menu.                     ");
    System.out.println("                      [2]. Exit.                                ");
    int option=s.nextInt();
    switch (option){
    case 1:
        repeat=0;
        break;
    case 2:
        repeat=1;
        break;
    default:
        System.out.println("Please check input.");
    }
    return repeat;
   }
 }  

当唯一可用的卡号和密码组合是第一个。其他所有内容打印错误信息,即&#34;错误的密码或卡号。&#34;程序的最后一个功能(void repeat)打印&#34;接下来你想做什么?&#34;部分两次。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:2)

让我们从第二个问题开始。您正在调用End方法两次。所以难怪它打印两次。

k.End(repeat);
}while(k.End(repeat)==0);

现在它只适用于第一个组合,因为你也打破了其他部分的循环。因此,它永远不会检查第二个值

for(int choice=0;choice<=5;choice++)
if(card_no==card[choice] && pin_no==pin[choice]){
k.menu(choice);
break;
}else{
System.out.println("Wrong card no. or pincode");
break;
}

答案 1 :(得分:0)

我不会立即看到这里有什么问题,而是两个更普遍的观察结果:

1)。不要写所有这些,然后开始测试。小块工作。测试login()函数,它是否可靠地返回用户输入的两个数字?一旦你有信心int测试这两个数字对你的数组的验证,等等。

2)。使用调试器。只需单步执行代码并检查一些变量,就可以快速向您显示正在发生的事情。

答案 2 :(得分:0)

在条件下注释一行代码 k.End(重复); ,并尝试一下。它只会显示一次。

//k.End(repeat);
    } while (k.End(repeat) == 0);