我们的教授为我们提供了制作ATM计划的活动,除了创建新的ATM帐户和更改PIN码之外,我已经完成了大部分功能。我只能输出已创建新帐户或PIN已更改但我不知道如何实际执行此操作。当用户想要创建一个新帐户时,他将输入他的名字,姓氏,卡号,密码和余额(所有字符串除了余额为双倍)然后程序应该记录新帐户,这样你就可以用它来查询余额,存款或取款。这是我的两个ATM功能的代码:
#A6A6A6
新帐户的课程:
public class ATM_Frame extends javax.swing.JFrame {
int f=8;
int l=8;
int n=8;
int p=8;
int a=8;
String fname[] = new String [f];
String lname[] = new String [l];
String cardno[] = new String [n];
String pinno[] = new String [p];
double balance[] = new double [a];
public ATM_Frame() {
initComponents();
btnClr.setEnabled(true);
btnNewAcc.setEnabled(false);
btnInq2.setEnabled(false);
btnDep2.setEnabled(false);
btnWdw2.setEnabled(false);
txtOutput.setEditable(false);
fname[0] = "Gudrun";
fname[1] = "Denis";
fname[2] = "Tamiko";
lname[0] = "Schlender";
lname[1] = "Petrov";
lname[2] = "Tanaka";
cardno[0] = "5023";
cardno[1] = "1821";
cardno[2] = "9931";
pinno[0] = "0000";
pinno[1] = "1111";
pinno[2] = "2222";
balance[0] = 5290;
balance[1] = 3600;
balance[2] = 10250;
}
private void btnNewAccActionPerformed(java.awt.event.ActionEvent evt) {
ButtonSound();
String fname = txtfName.getText();
String lname = txtlName.getText();
String cardno = txtcardNo.getText();
String pinno = txtpinNo.getText();
double bal = Double.parseDouble(txtBal.getText());
ATM_NewAcc a = new ATM_NewAcc(fname,lname,cardno,pinno,bal);
txtOutput.setText("" + a.toString());
}
private void btnChangePinActionPerformed(java.awt.event.ActionEvent evt) {
ButtonSound();
String fname = txtfName.getText();
String lname = txtlName.getText();
String cardno = txtcardNo.getText();
String pinno = txtpinNo.getText();
String newpin = txtNewPin.getText();
if(fname.equals("Gudrun") && lname.equals("Schlender") && cardno.equals("5023") && pinno.equals("0000"))
{
ATM_ChangePIN d = new ATM_ChangePIN(fname,lname,cardno,pinno,newpin);
txtOutput.setText("" + d.toString());
}
else if(fname.equals("Denis") && lname.equals("Petrov") && cardno.equals("1821") && pinno.equals("1111"))
{
ATM_ChangePIN d = new ATM_ChangePIN(fname,lname,cardno,pinno,newpin);
txtOutput.setText("" + d.toString());
}
else if(fname.equals("Tamiko") && lname.equals("Tanaka") && cardno.equals("9931") && pinno.equals("2222"))
{
ATM_ChangePIN d = new ATM_ChangePIN(fname,lname,cardno,pinno,newpin);
txtOutput.setText("" + d.toString());
}
else if(fname.equals("") || lname.equals("") || cardno.equals("") || pinno.equals("") && txtBal.equals(""))
{
JOptionPane.showMessageDialog(null, "Please fill out all necessary fields.", null, JOptionPane.WARNING_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "The information you entered is invalid.", null, JOptionPane.ERROR_MESSAGE);
txtfName.setText("");
txtlName.setText("");
txtcardNo.setText("");
txtpinNo.setText("");
txtNewPin.setText("");
txtBal.setText("");
}
}
更改密码的类:
public class ATM_NewAcc
{
private String fname;
private String lname;
private String cardno;
private String pinno;
private double balance;
public ATM_NewAcc()
{
fname = "";
lname = "";
cardno = "";
pinno = "";
balance = 0;
}
public ATM_NewAcc(String first, String last, String card, String pin, double bal)
{
fname = first;
lname = last;
cardno = card;
pinno = pin;
balance = bal;
}
@Override
public String toString()
{
return "NEW BANK ACCOUNT CREATED!"
+ "\nName: " + fname + " " + lname
+ "\nAccount Number: " + cardno
+ "\nPIN Code: " + pinno
+ "\nBalance: " + balance;
}
}
答案 0 :(得分:0)
根据您提供的内容,您似乎必须将输入数据放入数组fname [],lname []等。
也许有一种方法" addAccount(ATM_NewAcc acc)"或类似的东西,将ATM_NewAcc对象的数据放入数组?当用户超过8个时会发生什么? ;)
编辑:这样的事情?
public class Acc
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Acc(String name)
{
this.name = name;
}
}
public class Atm
{
private String[] users = new String[8];
public static void main(String[] args)
{
Atm atm = new Atm();
atm.users[0] = "Test1";
atm.addUser(new Acc("Test2"));
atm.printUsers();
}
private void printUsers()
{
for(String s : users)
{
if(s != null)
{
System.out.println(s);
}
}
}
private void addUser(Acc acc)
{
for(int i = 0; i < users.length; ++i)
{
if(users[i] == null)
{
users[i] = acc.getName();
return;
}
}
throw new IllegalStateException("Too many accounts");
}
}