以下是我的程序方法:
package ItPatPackage;
//Defines a banking Client
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
public class Client
{
//attributes
private String clientName;
private String clientPass;
private int accountNum;
private double currentBal;
private double savingsBal;
private boolean verify;
//default constructor
public Client()
{
clientName = "";
clientPass = "";
accountNum = 0;
currentBal = 0.0;
savingsBal = 0.0;
verify = false;
}
//parameterised constructor
private Client(String username,String password,int accNum, double
curBal,double savBal, boolean ver)
{
clientName = username;
clientPass = password;
accountNum = accNum;
currentBal = curBal;
savingsBal = savBal;
verify = ver;
}
//get methods
public String getClientName()
{
return clientName;
}
public String getClientPass()
{
return clientPass;
}
public int getAccNum()
{
return accountNum;
}
public double getCurrentBal()
{
return currentBal;
}
public double getSavingsBal()
{
return savingsBal;
}
public boolean getClientVerify()
{
return verify;
}
//mutator methods
public void setClientName(String username)
{
clientName= username;
}
public void setClientPass(String password)
{
clientPass = password;
}
//deposit
public void depositSavings(double ds)
{
savingsBal = savingsBal + ds;
}
public void depositCurrent(double dc)
{
currentBal = currentBal + dc;
}
//withdraw
public void withdrawSavings(double ws)
{
if (ws<savingsBal){
savingsBal = savingsBal - ws;
}
else{
JOptionPane.showMessageDialog(null, "Insufficient Funds.
\nYour Savings Balance is: R" + savingsBal);
}
}
public void withdrawCurrent(double wc)
{
if (wc<currentBal){
currentBal = currentBal - wc;
}
else{
JOptionPane.showMessageDialog(null, "Insufficient Funds.
\nYour Current Balance is: R" + currentBal);
}
}
//transfer
public void transferStoC(double sc)
{
if (sc<savingsBal){
savingsBal = savingsBal - sc;
currentBal = savingsBal + sc;
}
else{
JOptionPane.showMessageDialog(null, "Insufficient Funds.
\nYour Savings Balance is: R" + savingsBal);
}
}
public void transferCtoS(double cs)
{
if (cs<savingsBal){
savingsBal = savingsBal + cs;
currentBal = savingsBal - cs;
}
else{
JOptionPane.showMessageDialog(null, "Insufficient Funds.
\nYour Current Balance is: R" + currentBal);
}
}
//: Creates New Client
public void newClient(String username,String password) throws
IOException
{
clientName = username;
clientPass = password;
accountNum = (int)(Math.random()*100000);
savingsBal = (int)((Math.random()*100000000)) / 100.0;
currentBal = (int)((Math.random()*100000000)) / 100.0;
BufferedWriter bw = new BufferedWriter (new
FileWriter("ClientDatabase.txt",true));
bw.write(clientName+"_"+clientPass+"_"+accountNum+"_"+currentBal+"_"+savingsBal+"_");
bw.newLine();
bw.close();
}
//: Verifies new Client (THIS IS WHAT DOESN'T WORK, ALONG WITH THE CODE I WILL POST AFTER THIS
public boolean clientVerify(String checkUsername, String checkPassword)
{
try
{
BufferedReader br = new BufferedReader(new FileReader("ClientDatabase.txt"));
String s;
while ((s = br.readLine()) != null){
String line = br.readLine();
String part[] = line.split("_");
if ((part[0].equals(checkUsername))|(part[1].equals(checkPassword))){
accountNum = Integer.parseInt(part[3]);
currentBal = Double.parseDouble(part[4]);
savingsBal = Double.parseDouble(part[5]);
verify = true;
}
}
}
catch(IOException | NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Error");
}
return verify;
}
}
我的登录按钮的代码如下:
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
String checkUsername = txfUsername.getText();
String checkPassword = txfPassword.getText();
Client cl1 = new Client();
boolean verify = cl1.clientVerify(checkUsername, checkPassword);
if (verify==true){
OptionsPage options = new OptionsPage();
options.setVisible(true);
this.setVisible(false);
}
else{
JOptionPane.showMessageDialog(null, "Username/Password do not match or do not exist.");
}
}
我的“clientVerify”方法遇到了问题,该方法适用于我的“登录按钮”,如上所示,我不确定哪个部分是错误的...它似乎不起作用,并且我不认为我的BufferedReader正在正确读取文本文件。 (这是我唯一能想到的可能是错误的,如果是别的话,请纠正我。) 无论用户输入是什么,所有出现的是JOptionPane消息对话框,说“错误”,然后“用户名/密码不匹配或不存在”。我检查了文本文件,信息完全由“_”分隔。 另一种可能性是我的方法没有像我在默认构造函数中输入的那样改变。我承认我是方法的新手,所以这可能是一种可能性。
非常感谢任何帮助。
答案 0 :(得分:0)
String line;
Path file = Paths.get("C:\\Users\\nik\\Documents\\Programming\\TextFiles\\Events.txt");
try {
InputStream input = new BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
尝试使用此代码尝试访问文本文件的路径。因为您的方法看起来很顺序,但是您的程序可能无法找到您的文本文件。希望这有效。