我必须创建一个迷你ATM代码,我无法弄清楚从哪里开始? [英] I have to create a mini ATM code, and I can't figure out where to start?

查看:74
本文介绍了我必须创建一个迷你ATM代码,我无法弄清楚从哪里开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正计划制作5个预设帐户并为帐户添加资金,我对方法不熟悉所以我无法弄清楚如何使用方法验证用户输入(我必须使用方法)



我尝试过:



import java.util。* ;

公共类银行{



public static void main(String [] args){

// TODO自动生成方法存根

扫描仪输入=新扫描仪(System.in);

int counter = 0;

int dig = 0 ;

int pin = 0;

System.out.println(欢迎来到ATM);

if(counter == 0 )

{

System.out.println(输入您帐号的最后4位数字);

dig = input.nextInt ();

验证(挖掘,计数器);

}

否则if(counter == 1)

{

System.out.println(输入引脚);

pin = input.nextInt();

}

validation(挖掘,计数器);

}



静态布尔验证(int dig,int counter){

/ / TODO自动生成的方法stub

int acc [] = {1234,2345,3456,4567,5678};

for(int x = 0; x< 5 ; x ++)

{

if(acc [x] == dig)

{

counter + = 1 ;

main(null);

返回true;

}

其他

{

System.out.println(输入无效);

main(柜台);

}

}

返回0;



}



}

At the moment I'm planning on making 5 preset accounts and add money to the account, im new to methods so i can't really figure out how to validate the user inputs using methods( and i have to use methods)

What I have tried:

import java.util.*;
public class Bank {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner (System.in);
int counter=0;
int dig=0;
int pin=0;
System.out.println("Welcome to the ATM");
if(counter==0)
{
System.out.println("Enter the last 4 digits of your account number");
dig=input.nextInt();
validation(dig,counter);
}
else if(counter==1)
{
System.out.println("Enter the pin");
pin=input.nextInt();
}
validation(dig,counter);
}

static boolean validation(int dig,int counter) {
// TODO Auto-generated method stub
int acc[]={1234,2345,3456,4567,5678};
for(int x=0;x<5;x++)
{
if(acc[x]==dig)
{
counter+=1;
main(null);
return true;
}
else
{
System.out.println("Invalid Input");
main(counter);
}
}
return 0;

}

}

推荐答案

使用方法在所有情况下,做任何事都是一样的。定义方法名称,输入参数和输出参数。因此,要验证帐号,您可以使用以下内容:

Using methods to do anything is much the same in all cases. Define the method name, the input parameters and the output parameters. So to validate an account number you could have something like:
bool isValidAccount(int accountNumber) {
    bool answer = false; // default condition
    // do whatever is necessary to check the account
    // and set answer = true if it is valid

    return answer;
}

int account = // get the account number
if (isValidAccount(account) {
    // yes, it is valid
}
else {
    // not valid
}


在你做任何事情之前,澄清所有要求。在你知道要求重写和重构之前跳进编码部分。你似乎并不确定要求和限制
Before you do anything, clarify all of the requirements. Jumping into the coding portion before you know is asking for re-write and refactoring. It does not seem that you are sure on the requirements and limitations
Q' Do you have to use only one class, or are you allowed multiple?
A' No i don't think we can use multiple classes



一旦满足所有要求,就可以对需要的操作进行高级概述/计划


Once you have all of the requirements, then put together a high level outline/plan on the operations that are going to be needed

1 Display "Welcome" screen

2 User Information
-2A AccountNumber, PIN, Balance
-2B Get user credentials: Account number and PIN
-2C Authenticate user

3 Banking Functions
-3A Balance
-3B Withdrawal
-3C Deposit

此列表可能不完整,或者可能有太多根据您的要求。

如果需要,每个主要项目可以成为一个单独的类。在大多数情况下,下面的每个项目都将通过一种方法完成

#2A是例外,而且只是数据。应该是一个单独的课程。



现在为您的实际代码。我的建议是同时验证帐号和PIN码;使用类似于用户名和方法的方法密码。这有两个安全原因



首先是猜测和暴力保护。这些统计数据基于一个4位数的Acct和4位数的引脚

1-单独的验证只有20,000种组合(2 * 10 ^ 4)

2-精梳一个身份验证将是100,000,000个组合(1 * 10 ^ 8)



第二个来自此 - 如果您有2个单独的反馈消息,那个人试图获得一旦他们得到正确的帐号或正确的密码,就会知道,然后将1:20,000的一半组合起来。如果您返回帐号和/或PIN码不正确的含糊不清的消息,则它们保持在1:100,000,000循环中,该循环具有(可以说)5,000x的安全性。

This list may not be complete for you, or it may have too much for your requirements.
Each one of the main items could become a separate class if needed. For the most part, each item underneath would be accomplished via a method
#2A is the exception, and is just the data. Should probably be a separate class.

Now for your actual code. My recommendation would be to authenticate both the Account Number and the PIN simultaneously; using a method similar to a username & password. There are 2 security reasons for this

The first is guessing and brute force protection. These stats are based on a 4 digit Acct and 4 digit pin
1- Separate "validations" would only have 20,000 combinations (2*10^4)
2- Combing into one authentication would be 100,000,000 combinations (1*10^8)

The second is derived from this- if you have 2 separate feedback messages, the person trying to get in would know once they got a correct Acct or a correct PIN, and then have half of the 1:20,000 combination. If you returned an ambiguous message of "Account number and/or PIN number are incorrect" they stay on the 1:100,000,000 loop which has (arguably) 5,000x the security.


这篇关于我必须创建一个迷你ATM代码,我无法弄清楚从哪里开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆