以下是我的程序的方法:
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消息对话框,显示"错误",然后显示"用户名/密码不匹配或不存在"。我已经检查了文本文件,所有信息都用"_"完美地隔开。另一种可能性是,我的方法与我在默认构造函数中输入的方法没有变化。我承认我是新手,所以这可能是可能的。
任何帮助都将不胜感激。
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));
尝试使用此代码来获取文本文件的路径。因为您的方法看起来是有序的,但可能您的程序在查找文本文件时遇到了问题。希望这能奏效。