当前位置: 首页 > 知识库问答 >
问题:

我如何完成这个java程序[关闭]

柴磊
2023-03-14
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)

共有1个答案

马阳晖
2023-03-14

您需要告诉我们这个“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>
 类似资料: