import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
public class ATM {
String names []= {"Nikhil", "Sam", "Raj", "Saron", "Abi", "Adi"};
int card[]={1806978,1244668,5769124,7658301,6571354,5155499};
int pin[]={4444, 3333, 2222, 1111, 6666, 7988};
int balance []={10000, 23345, 24673, 37532, 43637, 37356};
Scanner s=new Scanner(System.in);
int f,ch, z=0;
static int opt=0;
public static void main(String args []) {
int enter=0;
ATM k=new ATM();
k.info();
k.login(enter);
int temp=k.login(enter);
if(enter==1){
k.menu(temp);
}else{
System.out.println("Wrong card no. or pincode");
}
}
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("");
}
int login(int enter){
System.out.println(" >> Enter card number - ");
int card_no=s.nextInt();
System.out.println(" >> Enter pin number - ");
int pin_no= s.nextInt();
for (int i=0;i<=5;i++){
if (card [i]==card_no && pin[i]==pin_no)
enter=1;
z=i;
}
return z;
}
void menu (int temp){
System.out.println(" Welcome "+names[temp]);
System.out.println(" Your Card no: is " + card[temp]);
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(temp);
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(temp);
}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...");
}
}
}
无论我输入什么作为密码或卡号(即使它是正确的)我收到消息“错误卡号或密码”并且它还要求我两次卡号和密码。任何有关改进代码的建议都将不胜感激。
答案 0 :(得分:0)
这里有很多问题:
int enter=0;
的{{1}}值,因此它将始终为0,而您的enter
条件将始终为false。if
方法返回1
或0
。k.login(enter);
但是您要与逻辑错误的int temp=k.login(enter);
进行比较,您需要将其与enter
进行比较。同时显示temp
方法的代码,基于此我将修改答案。
答案 1 :(得分:0)
问题不在于登录方法中的for循环,您传递的输入值是问题。 在登录方法中设置的输入值设置为本地值,因此输入始终为0.请检查并纠正它应该正常工作