【问题标题】:ATM Machine customers.txt fileATM机customers.txt文件
【发布时间】:2015-11-26 01:37:02
【问题描述】:

我是堆栈溢出的新用户.. 好吧,我正在做一个ATM Java 程序.. 我的问题是,我很难弄清楚如何制作一个 文件阅读器(我的是customers.txt)在我的ATMSimulator.java..

这是我的ATM.java

/*
An ATM that accesses a bank.
*/

public class ATM
{
public static final int CHECKING = 1;
public static final int SAVINGS = 2;

private int state;
private int customerNumber;
private Customer currentCustomer;
private BankAccount currentAccount;
private Bank theBank;

public static final int START = 1;
public static final int PIN = 2;
public static final int ACCOUNT = 3;
public static final int TRANSACT = 4;

/*
    Construct an ATM for a given bank.
    @param aBank the bank to which this ATM connects
*/

public ATM (Bank aBank)
{
    theBank = aBank;
    reset();
}

/*
    Resets the ATM to the initial state
*/

public void reset()
{
    customerNumber = -1;
    currentAccount = null;
    state = START;
}

/*
    Sets the current customer number and sets state to PIN.
    (Precondition: state is START)
    @param number the customer number
*/

public void setCustomerNumber (int number)
{
    assert state == START;
    customerNumber = number;
    state = PIN;
}

/*
    Finds customer in bank. If found, sets state to ACCOUNT, else to START
    (Precondition: state is PIN)
    @param pin the PIN of the current customer
*/

public void selectCustomer (int pin)
{
    assert state == PIN;
    currentCustomer = theBank.findCustomer (customerNumber, pin);

    if (currentCustomer == null)
        state = START;
    else
        state = ACCOUNT;
}

/*
    Sets current account to checking or savings. Sets state to TRANSACT.
    (Precondition: state is ACCOUNT or TRANSACT)
    @param account one of CHECKING or SAVINGS
*/

public void selectAccount (int account)
{
    assert state == ACCOUNT || state == TRANSACT;

    if (account == CHECKING )
        currentAccount = currentCustomer.getCheckingAccount ();
    else
        currentAccount = currentCustomer.getSavingsAccount();
    state = TRANSACT;
}

/*
    Withdraws amount from current account.
    (Precondition state is TRANSACT)
    @param value the amount to withdraw
*/

public void withdraw (double value)
{
    assert state == TRANSACT;
    currentAccount.withdraw(value);
}

/*
    Deposits amount to current account.
    (Prcondition: state is TRANSACT)
    @param value the amount to deposit
*/

public void deposit (double value)
{
    assert state == TRANSACT;
    currentAccount.deposit (value);
}

/*
    Gets the balance of the current account.
    (Precondition: state is TRANSACT)
    @return the balance
*/

public double getBalance()
{
    assert state == TRANSACT;
    return currentAccount.getBalance();
}

/*
    Moves back to previous state
*/

public void back ()
{
    if (state == TRANSACT)
        state = ACCOUNT;
    else if (state == ACCOUNT)
        state = PIN;
    else if (state == PIN)
        state = START;
}

/*
    Gets the current state of this ATM.
    @return the currnt state
*/

public int getState ()
{
    return state;
}
}

这是我的Customer.java

/*
A bank customer with a checking and a savings account.
*/

public class Customer
{
private int customerNumber;
private int pin;
private BankAccount checkingAccount;
private BankAccount savingsAccount;

/*
    Construct a customer with a given number and PIN.
    @param aNumber the customer number
    @param aPin the personal identification number
*/

public Customer (int aNumber, int aPin)
{
    customerNumber = aNumber;
    pin = aPin;
    checkingAccount = new BankAccount();
    savingsAccount = new BankAccount();
}

/*
    Test if this customer matches a customer number and PIN
    @param aNumber a customer number
    @param aPin a personal identification number
    @return true if the customer number and PIN match
*/

public boolean match (int aNumber, int aPin)
{
    return customerNumber == aNumber && pin == aPin;
}

/*
    Gets the checking account of this customer
    @return the checking account
*/

public BankAccount getCheckingAccount()
{
    return checkingAccount;
}

/*
    Get the savings account of this customer
    @return checking account
*/

public BankAccount getSavingsAccount()
{
    return savingsAccount;
}
}

这是我的Bank.java

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/*
A bank contains customer with bank accounts
*/

public class Bank
{
private ArrayList<Customer> customers;

/*
    Construct a bank with no customers
*/

public Bank()
{
    customers = new ArrayList<Customer> ();
}

/*
    Reads the customer numbers and pins
    and initializes the bank accounts
    @param filename the name of the customer file
*/

public void readCustomers(String filename)
    throws IOException
{
    Scanner in = new Scanner (new File(filename));

    while (in.hasNext())
    {
        int number = in.nextInt();
        int pin = in.nextInt();
        Customer c = new Customer (number, pin);
        addCustomer (c);
    }
    in.close();
}

/*
    Adds a customer to the bank.
    @param c the customer to add
*/

public void addCustomer (Customer c)
{
    customers.add(c);
}

/*
    Find a customer in the bank.
    @param aNumber a customer number
    @param aPin a personal identification number
    @return the matching customer, or null if no customer matches
*/

public Customer findCustomer (int aNumber, int aPin)
{
    for (Customer c : customers)
    {
        if (c.match(aNumber, aPin))
            return c;
    }
    return null;
}
}

这是我的BankAccount.java

/**
A bank account has a balance that can be changed by deposits and withdrawals
*/
public class BankAccount
{
private int accountNumber;
private double balance;

/**
    Counstruct a bank account with a zero balance.
    @param anAccountNumber the account number for this account
*/

public BankAccount(){}


public BankAccount(int anAccountNumber)
{
    accountNumber = anAccountNumber;
    balance = 0;
}

/**
    Construct a bank account with a given balance.
    @param anAccountNumber the account number for this account
    @param initialBalance the initial balance
*/
public BankAccount(int anAccountNumber, double initialBalance)
{
    accountNumber = anAccountNumber;
    balance = initialBalance;
}

/**
    Gets the account number of this bank account.
    @return the account number
*/
public int getAccountNumber()
{
    return accountNumber;
}

/**
    Deposits money into bank account
    @param amount the amount to deposit
*/
public void deposit (double amount)
{
    double newBalance = balance + amount;
    balance = newBalance;
}

/**
    Withdraws money from the bank account.
    @param amount the amount to withdraw
*/
public void withdraw(double amount)
{
    double newBalance = balance - amount;
    balance = newBalance;
}

/**
    Gets the current balance of the bank account.
    @return the current balance
*/
public double getBalance()
{
    return balance;
}
}

我在ATMSimulator.java 中遇到了customers.txt 的问题:

import java.io.IOException;
import java.util.Scanner;

/*
    A text based simulation of an automatic teller machine.
*/

public class ATMSimulator
{
    public static void main (String [] args)
{
    ATM theATM;

    try
    {
        Bank theBank = new Bank ();
        theBank.readCustomers("customers.txt");
        theATM = new ATM (theBank);
    }

    catch (IOException e)
    {
        System.out.println ("Error opening accounts file.");
        return;
    }

    Scanner in= new Scanner (System.in);

    while (true)
    {
        int state = theATM.getState();
        if (state == ATM.START)
        {
            System.out.print ("Enter customer number: ");
            int number = in.nextInt();
            theATM.setCustomerNumber(number);
        }

        else if (state == ATM.PIN)
        {
            System.out.println ("Enter PIN: ");
            int pin = in.nextInt();
            theATM.setCustomerNumber (pin);
        }

        else if (state == ATM.ACCOUNT )
        {
            System.out.print ("A = Checking, B = Savings, C = Quit: ");
            String command = in.next();

            if (command.equalsIgnoreCase ("A"))
                theATM.selectAccount (ATM.CHECKING);
            else if (command.equalsIgnoreCase ("B"))
                theATM.selectAccount (ATM.SAVINGS);
            else if (command.equalsIgnoreCase ("C"))
                theATM.reset();
            else
                System.out.println ("Illegal input!");
        }

        else if (state == ATM.TRANSACT)
        {
            System.out.println ("Balance = " + theATM.getBalance());
            System.out.print ("A = Deposit, B = Withdrawal, C = Cancel: ");
            String command = in.next();

            if (command.equalsIgnoreCase ("A"))
            {
                System.out.print ("Amount: ");
                double amount = in.nextDouble();
                theATM.deposit (amount);
                theATM.back();
            }

        else if (command.equalsIgnoreCase ("B"))
        {
            System.out.print ("Amount: ");
            double amount = in.nextDouble();
            theATM.withdraw (amount);
            theATM.back();
        }

        else if (command.equalsIgnoreCase ("C"))
            theATM.back();

        else
            System.out.println ("Illegal input!");
        }
    }
}
}

我创建了customers.txt 文件,但输出错误..请帮助我

【问题讨论】:

  • 错误是什么?
  • 它将打印“打开帐户文件时出错”。如果我创建 customers.txt 文件,它将显示 java.util.InputMismatchException
  • 如果你把这个错误作为问题的重点,那会很有帮助。

标签: java filereader


【解决方案1】:

我会解决你的问题而不是sh..大量的代码。

您以这种方式有效地从文件中读取。

try( BufferedReader br = new BufferedReader(new FileReader("filename.txt"))){  
   String line;
   while((line = br.readLine()) != null){

   }
}catch(IOException e}{
     e.printStackTrace();
}

如果你想从控制台读取:

try( BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){  
   String line;
   while((line = br.readLine()) != null){

   }
}catch(IOException e}{
     e.printStackTrace();
}

要在控制台中指示输入结束,请使用ctrl + z

【讨论】:

  • 对不起先生,我不明白.. 对于我的情况,如何制作“filename.txt”?
  • @Lufiii 请显示您获得的异常堆栈并标记您在代码中获得它的位置,例如通过注释 //EXCEPTION HERE。这会有很大帮助。
  • 在 java.util.Scanner.throwFor(Scanner.java:864) 在 java.util.Scanner.next(Scanner.java:1485) 的线程“主”java.util.InputMismatchException 中的异常java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Bank.readCustomers(Bank.java:37) at ATMSimulator.main(ATMSimulator.java:17) )
  • 对不起先生,我是java新手
【解决方案2】:

嘿,您在方法 readCustomer 中传递的文本文件的路径不是正确的路径。所以你可以做的就是在路径中放入src和包名如下:“src/packageName/customer.txt”

所以当你在 main 中调用它时

public static void main (String [] args)
{
    ATM theATM;

    try
    {
        Bank theBank = new Bank ();
        theBank.readCustomers("src/packageName/customer.txt");
        theATM = new ATM (theBank);
    }

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    相关资源
    最近更新 更多