我如何更改此ATM代码以便它

时间:2011-01-18 14:03:06

标签: java

import java.util.Scanner;
import userAccountInformation.csv;

public class AtmMachine {

 private double availableBalance;
 private double totalBalance;
 Scanner input = new Scanner(System.in);

 public int userAccount() {
  System.out.print("Please enter your account number: ");
  int account;
  account = input.nextInt();
  validateInput();
  return account;
 }

 public int userPin() {
  System.out.print("Please enter your pin number: ");
  int pin;
  pin =input.nextInt();
  validateInput();
  return pin;
 }

public void startAtm() {
 userAccount();
 userPin();
 drawMainMenu();
}

public void drawMainMenu() {
 int selection;

 System.out.println("\nATM main menu:");
 System.out.println("1 - View account balance");
 System.out.println("2 - Withdraw funds");
 System.out.println("3 - Add funds");
 System.out.println("4 - Terminate transaction");
 System.out.print("Choice: ");
 selection =input.nextInt();

 switch(selection) {

 case 1:
 viewAccountInfo();
 break;
 case 2:
 withdraw();
 break;
 case 3:
 addFunds();
 break;
 case 4:
 System.out.println("Thank you for using this ATM!!! goodbye");
 }
}

public void viewAccountInfo() {
 System.out.println("Account Information:");
 System.out.println("\t--Total balance: $"+totalBalance);
 System.out.println("\t--Available balance: $"+availableBalance);
 drawMainMenu();
}

public void deposit(int depAmount) {
 System.out.println("\n***Please insert your money now...***");
 totalBalance =totalBalance +depAmount;
 availableBalance =availableBalance +depAmount;
}

public void checkNotSufficientFunds(int withdrawAmount) {
 if(totalBalance -withdrawAmount < 0)
 System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
 else {
  totalBalance =totalBalance -withdrawAmount;
  availableBalance =availableBalance -withdrawAmount;
  System.out.println("\n***Please take your money now...***");
 }
}

public void addFunds() {

 int addSelection;

 System.out.println("Deposit funds:");
 System.out.println("1 - $20");
 System.out.println("2 - $40");
 System.out.println("3 - $60");
 System.out.println("4 - $100");
 System.out.println("5 - Back to main menu");
 System.out.print("Choice: ");
 addSelection =input.nextInt();

 switch(addSelection) {

 case 1:
 deposit(20);
 drawMainMenu();
 break;
 case 2:
 deposit(40);
 drawMainMenu();
 break;
 case 3:
 deposit(60);
 drawMainMenu();
 break;
 case 4:
 deposit(100);
 drawMainMenu();
 break;
 case 5:
 drawMainMenu();
 break;
 }
}

public void withdraw() {

 int withdrawSelection;

 System.out.println("Withdraw money:");
 System.out.println("1 - $20");
 System.out.println("2 - $40");
 System.out.println("3 - $60");
 System.out.println("4 - $100");
 System.out.println("5 - Back to main menu");
 System.out.print("Choice: ");
 withdrawSelection =input.nextInt();

 switch(withdrawSelection) {
 case 1:
 checkNotSufficientFunds(20);
 drawMainMenu();
 break;
 case 2:
 checkNotSufficientFunds(40);
 drawMainMenu();
 break;
 case 3:
 checkNotSufficientFunds(60);
 drawMainMenu();
 break;
 case 4:
 checkNotSufficientFunds(100);
 drawMainMenu();
 break;
 case 5:
 drawMainMenu();
 break;
 }
}

public static void main(String args[]) {

 AtmMachine myAtm = new AtmMachine();
 myAtm.startAtm();
 }
}

validateInput() {

}

但有人可以帮助我编写validateInput()方法,以便它: 1.检查帐号和密码,并根据用户的文本文件进行验证。允许三次尝试。

除了帮助我改变它以便:

  1. 如果要存放多个检查,请将值保存在数组中,然后更新文本文件。
  2. 存款,转帐和取款会修改文本文件。
  3. 如果用户不是您银行的客户,则任何提款都需要额外支付2.00美元。
  4. 根据文本文件检查余额。
  5. * 我遇到问题的地方: *将一个文本文件合并到代码中,以便我可以检查它并在需要时更新它/编写validateInput()方法。

    我认为这需要大量的工作,所以任何帮助都值得赞赏...

1 个答案:

答案 0 :(得分:2)

有很多方法可以将文件读入Java。我更喜欢使用以下内容:

阅读文件:

ArrayList<Account> accounts = new ArrayList<Account>();
try {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new FileReader("C:\\readFile.txt"));
    String line;
    while ((line = br.readLine()) != null) {
        accounts.Add(new Account(line));
    } 
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
} finally {
  if (br != null) {
    try {
      br.close();
    } catch (Exception ex) {
      e.printStackTrace();
    }
  }
}

您必须决定格式,然后使用tokenizer或其他解析器从您读过的字符串中提取信息。我建议您在项目中设置类(例如Account类 - 属性:AccountNumber,Pin ,平衡等)。请记住添加一个Account(String)构造函数和一个toString()方法,以便从字符串中轻松创建一个帐户,并将该帐户转换为字符串,以便存储在文本文件中。

要将帐户写回您的文件(在每笔交易结束时或程序关闭时,您的选择),请执行以下操作:

BufferedWriter bufferedWriter = null;

    try {
        //Construct the BufferedWriter object
        bw = new BufferedWriter(new FileWriter(filename));

        //Start writing to the output stream
        for (int i=0; i<accounts.size(); i++) {
          bw.write(accounts.get(i).toString());
          bw.newLine();
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        //Close the BufferedWriter
        try {
            if (bw!= null) {
                bw.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
相关问题