package ATM;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ATM {
Users user; //User object as class member
private String code_idtf; //Used to receive the entered account and password, and compare with the original account and password.
private String password_idtf;
String str = "------------------";
BufferedReader br ;
BufferedWriter bw ;
//Constructor, the parameters are Users user, BufferedReader br, BufferedWriter bw.
public ATM(Users user, BufferedReader br, BufferedWriter bw) throws IOException {
this.br = br;
this.bw = bw;
this.user = user;
}
//Password input interface
public void enter_passwaord() throws Exception {
String St = str + "Welcome to use the simulated ATM system" + str;
bw.write(St);
bw.flush();
for (int counter = 0; counter < 3;) {
bw.write("\r\n" + "Please enter your credit card number:"+"\r\n");
bw.flush();
code_idtf = br.readLine(); // Get the entered card number
bw.write("The account number is:" + code_idtf + "\r\n" + "please enter your password:"+"\r\n");
bw.flush();
password_idtf = br.readLine(); // Get the entered password
bw.write("password is:" + password_idtf + "\r\n");
bw.flush();
if (user.code.equals(code_idtf) && user.password.equals(password_idtf)) {
select();
} else {
bw.write("Password or card number is wrong, please re-enter, you still have" + (3 - counter) + "Second Chance");
bw.flush();
counter++;
}
}
}
//Select interface
public void select() throws IOException {
bw.write(str + "Welcome to use Angel to simulate ATM program" + str + "\r\n");
bw.flush();
bw.write("1.>Withdraw funds." + "\r\n" + "2.>Deposit." + "\r\n" + "3.>Query information." + "\r\n" + "4.>Password settings." + "\r\n" + "5.>Exit the system."
+ "\r\n");
bw.flush();
String num;
bw.write("Please select the item you want to operate (1-5):");
bw.flush();
num = br.readLine(); // num is an integer converted from ASICC code
switch (num) {
case "1":
draw_money();
break;
case "2":
deposit();
break;
case "3":
query();
break;
case "4":
change_password();
break;
case "5":
Exit_Sys();
break;
}
}
//Withdraw
public void draw_money() throws IOException {
bw.write("Withdrawal"+ "\r\n"+"Please enter the amount you want to withdraw:");
bw.flush();
int num = Integer.parseInt(br.readLine());
if (user.balance <= num) {
bw.write(num+"\r\n");
bw.flush();
user.balance = user.balance - num;
} else {
bw.write("Your balance is insufficient");
bw.flush();
}
select();
}
//deposit
public void deposit() throws NumberFormatException, IOException {
bw.write("deposit"+"\r\n"+"Please put in your deposit banknote:");
bw.flush();
int num = Integer.parseInt(br.readLine());
bw.write(num+"\r\n");
bw.flush();
int money = num;
user.balance += money;
select();
}
//Inquire
public void query() throws IOException {
bw.write("Check balances"+"\r\n"+"Your balance is" + user.balance+ "\r\n");
bw.flush();
select();
}
//change Password
public void change_password() throws IOException {
bw.write("change Password"+"\r\n"+"Please enter the old password:"+ "\r\n");
bw.flush();
String pwd = br.readLine();
if (pwd.equals(user.password)) {
bw.write("Please enter a new password:"+ "\r\n");
bw.flush();
String pwd1 = br.readLine();
bw.write("Please enter the new password again:"+ "\r\n");
bw.flush();
String pwd2 = br.readLine();
if (!pwd1.equals(pwd2)) {
bw.write("The two entries are inconsistent, please enter again."+ "\r\n");
bw.flush();
change_password();
} else {
user.password = pwd1;
bw.write("The password has been successfully modified, please use the new password."+ "\r\n");
bw.flush();
}
select();
}
}
//drop out
public void Exit_Sys() throws IOException {
bw.write("Exit\r");
bw.flush();
bw.close();
br.close();
System.exit(1);
}
//main function
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String st1 = "Enter.txt";
String st2 = "Output.txt";
FileReader fr = new FileReader(st1);
FileWriter fw = new FileWriter(st2, true); // Take append content to the end of the file
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
Users userone = new Users("Zhang San", "123456789", "415861149", 4000);
ATM atm = new ATM(userone, br, bw);
atm.enter_passwaord();
}
}
用户:
package ATM;
public class Users {
private String name; //username
String code; //card number
String password; //password
int balance ; //Balance
//Constructor
public Users(String name,String code,String password,int balance){
this.code=code;
this.name=name;
this.password=password;
this.balance=balance;
}
}
而此错误显示为:
C:\javaprograms>javac -d . ATM.java
C:\javaprograms>java ATM.ATM
Exception in thread "main" java.io.FileNotFoundException: Enter.txt (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at java.base/java.io.FileReader.<init>(Unknown Source)
at ATM.ATM.main(ATM.java:164)
您需要告诉我们这个“Atm”包是从哪里来的,没有“基本Atm java程序”这样的东西。
假设您正在使用这段代码,那么您需要在运行程序之前创建两个空的.txt文件。一个名为“enter.txt”,另一个名为“output.txt”。
更新:再次阅读网页,有代码包括照顾文件问题。开始“或者,直接使用代码创建一个txt文件。代码直接创建文件,文件仍然在src文件所在的目录中。”
javac -d . Users.java
javac -d . ATM.java
java ATM.ATM
<use 'create new text document' in windows explorer to create enter.txt and output.txt in the same folder as ATM.java>
你能帮忙找出哪一部分错了吗,多谢。:)
我有这个,它将某个类解析为另一个类: 我的意思是,我可以使用类似这样的东西来完成这件事吗?
想改进这个问题吗 通过编辑此帖子,更新问题,使其只关注一个问题。 我尝试了一个简单的Java代码,但我不太理解程序的流程。请解释一下。 答案是123
问题内容: 有什么方法可以简单地等待所有线程处理完成?例如,假设我有: 如何更改此方法,以便该方法在注释处暂停直到所有线程的方法退出?谢谢! 问题答案: 你将所有线程放入数组中,全部启动,然后进行循环 每个连接将阻塞,直到相应的线程完成为止。线程的完成顺序可能不同于你加入线程的顺序,但这不是问题:退出循环时,所有线程均已完成。
写一个函数均衡器(w1,w2),它包含两个不同长度的单词w1和w2。函数应该返回重复的较小单词,直到它达到长单词的长度
我需要找到并修复代码块中的所有错误。我已经修复了几个,但是我不知道如何定位其余的或者从哪里开始。 代码图像