(简单的放置问题)如何使我的ATM程序不能低于0美元?

时间:2015-05-14 02:01:48

标签: java

这是我的Java 1决赛。如果余额低于0美元(并且没有创建对象动作 - 这将被添加到arraylist行动中),我希望它反对说资金不足。

我相信我会在2个地方使用条件。一个在Account类的getDouble()方法中,以及在Action类的对象Action()中。也许在主要的方法中......我也不知道我会放置什么条件,并且以最简单的方式不会弄乱我的程序..

我还有一个单独的问题......

如果我做了一个很大的数字,比如7或8位数,它会返回一个不同的值,比如200E7JK(例子)。我认为双重达到某种最大值。我该如何解决这个问题?

主要课程:

公共类ATM_Larrabee {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    ArrayList<Action> actions = new ArrayList();



    Account acc = new Account("N/A", "N/A", 0.0);

    acc.displayWelcome();

    acc.createUsernamePassword();



    boolean options = true;

    while(options){

        acc.disconnect();

        System.out.println("\nWhat action would you like to take?");
        System.out.println("[ Account Balance: $" + Account.balance + " ]");
        System.out.println("\n- Type 'deposit' to make a deposit into your account.");
        System.out.println("- Type 'withdrawal' to make a withdrawal from your account.");
        System.out.println("- Type 'history' to see your history of transactions.");
        System.out.println("- Type 'exit' if you are finished.\n");

        String input = in.nextLine();



        if("exit".equals(input)){
            break;
        }



        if("history".equals(input)){

            Action actCopy = new Action("N/A", 0);

            System.out.println("\nBelow is a history of all transactions made:\n");

            for(int x = 0; x < actions.size(); x++){

                actCopy = actions.get(x);

                System.out.println(actCopy.getType() + ": $" + actCopy.getAmount());   
            }
        }



        if("deposit".equals(input) || "withdrawal".equals(input)){

            double actionAmount;
            actionAmount = acc.getDouble();

            Action act = new Action(input, actionAmount);

            actions.add(act);

            System.out.println("\nAction accepted.");
        }
    }     



    System.out.println("\nYour Account Balance Is: $" + Account.balance);
    System.out.println("\nThank you for using Really Boring Bank!");
}
}

帐户类:

public class Account {

Scanner in = new Scanner(System.in);
static String username = "n/a";
static String password = "n/a";
static double balance = 0.0;



public Account(String u, String p, double b){

    username = u;

    password = p;

    balance = b;
}



public void displayWelcome(){

    String[] welcome = new String[3];

    welcome[0] = "Welcome to Really Boring Banking!";
    welcome[1] = "Hi there!  Welcome to Really Boring Banking!";
    welcome[2] = "Hello!  Welcome to Really Boring Banking!";

    Random rand = new Random();
    int r = rand.nextInt(3);

    if(r == 0){
        System.out.println(welcome[0]);
    }
    if(r == 1){
        System.out.println(welcome[1]);
    }
    if(r == 2){
        System.out.println(welcome[2]);
    }
}



public void createUsernamePassword(){

    System.out.println("\nAs a new member, you will first need to create a username and password.");
    System.out.println("\n[ Enter a username for your account: ]");

    String input = in.nextLine();
    setUsername(input);

    System.out.println("\n[ Enter a password for your account: ]");

    input = in.nextLine();
    setPassword(input);
}



public void disconnect(){

    Random rand = new Random();
    int r = rand.nextInt(5);

    if(r == 1){

        System.out.println("\n[ Connection to server lost.  Please log back into your account. ]");

        boolean loggedIn = false;
        boolean correctUsername = false;
        boolean correctPassword = false;

        while(!loggedIn){

            while(!correctUsername){

                System.out.println("\n[ Enter username: ]");

                String input = in.nextLine();

                if(Account.username.equals(input)){
                    correctUsername = true;
                    System.out.println("\nUsername Accepted");
                }
                else{
                    System.out.println("[ Incorrect username.  Please try again. ] ");
                }
            }    

            while(!correctPassword){

                System.out.println("\n[ Enter password: ]");
                String input = in.nextLine();

                if(Account.password.equals(input)){
                    correctPassword = true;
                    System.out.println("\nPassword Accepted");
                    loggedIn = true;
                    System.out.println("\nSuccessfully logged back into account: "+ Account.username + ".");
                }
                else{
                    System.out.println("[ Incorrect password.  Please try again. ] ");
                }
            }    
        }  
    }
}



    public double getDouble(){

    boolean validInput = false;

    double doub = 0.0;

    while (!validInput) {
        System.out.println("\n[ Please enter an amount: ]\n");

        String input = in.nextLine();

        try {

            doub = Double.parseDouble(input);

            validInput = true;

        } catch (NumberFormatException e) {

            validInput = false;
        }
    }
    return doub;
    }



public String getUsername(){
    return Account.username;
}
public void setUsername(String u){
    username = u;
}

public String getPassword(){
    return Account.password;
}
public void setPassword(String p){
    password = p;
}

public double getBalance(){
    return Account.balance;
}
public void setSeed(double b){
    balance = b;
}
}

动作类:

public class Action {

double amount = 0;
String type;


// object for withdrawals and deposits (saved into an arraylist elsewhere)

public Action(String t, double a){

    type = t;

    amount = a;

    if("deposit".equals(type)){
        type = "deposit";
        Account.balance = Account.balance + a;
    }

    if("withdrawal".equals(type)){
        type = "withdrawal";
        Account.balance = Account.balance - a;
    }        
}


// gets and sets

public String getType(){
    return this.type;
}
public void setType(String t){
    type = t;
}    

public double getAmount(){
    return this.amount;
}
public void setAmount(double a){
    amount = a;
} 

}

1 个答案:

答案 0 :(得分:0)

balance中制作static(和其他字段)Account会使所有内容都变为全局(并且意味着您只能拥有一个Account)。但是,在你的问题的背景下,

Account.balance = Account.balance - a;

需要进行测试以确保a不大于Account.balance

if (Account.balance >= a) {
    // Account.balance = Account.balance - a;
    Account.balance -= a;
} else {
    System.err.printf("%d is greater than %d and would be an overdraft%n",
            a, Account.balance);
}

从长远来看,虽然您需要Account的实例(因此请将它们作为实例字段并创建Account的实例)。