- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是堆栈溢出的新用户..好吧,我正在做一个ATM
Java程序..我的问题是,我很难弄清楚如何在我的ATMSimulator.java
中创建一个文件阅读器(我的是customers.txt
)..
这是我的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
文件,但输出错误..请帮助我
最佳答案
我会解决你的问题,而不是大量的代码。
您可以通过这种方式有效地读取文件。
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
。
关于java - ATM机customers.txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33929274/
我正在制作一个 ATM 程序来学习 java,使用 cmd 来编译和运行它。该程序编译没有错误,但在运行时仅打印 at ATM.(init)(ATM.java:6)。 如有任何帮助,我们将不胜感激!
减少很多,是不对称的。我试图证明它,但它不起作用这么好。 给定两种语言 A 和 B,其中 A 定义为 A={w| |w| is even} , i.e. `w` has an even length
这是我的源代码: import java.text.SimpleDateFormat; import java.util.*; import java.io.*; public class ATM {
我在世界范围内如何ATM-systems被架构。银行在全局范围内设计一个一致的系统一定非常困难。他们是为此使用最终一致性还是使用出色的 ACID 系统? 我可以有一天在我的银行所在的瑞典使用 ATM,
我正在努力获得 old piece of code从 2003 年开始工作。我正在尝试复制 ATM 样式的十进制文本框。这段代码声称对某人有用,但我在实现它时遇到了困难。 也许有人有更好的方法来实现这
我正在做一个自动取款机程序,我很难弄清楚如何让它实际存款和取款。余额一开始自动为 0 美元,但我无法输入任何内容来实际添加或减去它,我做错了什么? public class ATM { static
我的任务是创建一个 ATM 类型的程序。下面是我的主要功能(不包括存款、取款和查询余额功能)。当我运行此代码时,程序会重复循环 pin 函数,即使我输入 0 或 1234 也是如此。它会重复指示用户输
我们想编写一个基于 Web 的应用程序来监控银行中的 ATM 机,以具有以下功能: 各终端显示位置 显示一般状态 颜色编码或简单的终端 图标(ATM 向上/向下/需要注意, 低现金等) 有一个设施可以
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我创建了一个关于 ATM 机的应用程序,其数组帐户大小为 10,但由于某些奇怪的原因,输入仅接受 0(第一个 id),但不接受其他 id 的其他 9 个 id,它总是显示无效 ID 错误消息。我花了几
我对 Python 和编码还很陌生。我正在尝试创建一个 ATM 类型的程序,让您可以存款和取款。我输入了我收到的错误消息。老实说,我只是不知道从这里该去哪里。 此外,如何通过 % 调用确保提款金额可以
Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if
我试图创建一个程序来显示推算金额以及 ATM 可以分发的 10 美元和 1 美元纸币的数量,但它不会显示正确数量的 1 美元纸币。 int amount = int.Parse(txtAmount.T
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关于您编写的代码问题的问题必须在问题本身中描述具体问题 — 并且包括有效代码 以重现它。参见 SSC
我在 iPhone 应用程序中有一个文本字段,我想实现类似于 atms 输入的输入,您所要做的就是输入数字(不需要输入小数点字符)。我希望能够只为这个文本字段使用数字键盘。例如,文本字段最初显示: 0
import java.util.Scanner; import userAccountInformation.csv; public class AtmMachine { private doub
我制作了一个提款机(ATM)程序,但我不知道出了什么问题。一切正常,除了交易是一行零。我一定是搞砸了。我认为这与“showTransactions”方法有关。感谢您的帮助! import java.u
嘿伙计们,我正在构建一个 ATM 程序,并且一切正常 我有它弹出的菜单,您可以选择一个选项,它会运行该功能,但是,我一生都不能 设定平衡并让它留下来直到它改变一旦两个选项(存款、取款)之一发生变化,我
现在我正在开发一个 ATM 项目。用户将输入姓名、余额、交易类型和交易金额。交易类型为提款(W)、存款(D)和R(报告),我使用的是switch,有 3 个案例 W、D、R。如果您出现以下情况,程序将
我是一名优秀的程序员,十分优秀!