因此,我正在为我的Java类编写一个银行程序,我需要确保当您创建帐户时,输入的帐号不存在。
String accountNumber是私有的,因此我需要使用getter函数来检索它。
我真的不知道从哪里开始,这两种方式都是目前的代码。如有任何疑问,请发表评论。
import java.io.*;
public class Account_Main {
static Person[] personArray = new Person[10];
public static void main(String[] args) {
getSave();
personMenu();
save();
}
public static void personMenu() {
int personIndex = 1;
int yMeny = 1;
int n = personArray.length;
while(yMeny != 3) {
yMeny = Kbd.readInt("\nClick 1 to become a costumer\nClick 2 to log in\nClick 3 to exit");
switch(yMeny) {
case 1:
for (int i = 0; i < n; i++) {
if (personArray[i] == null) {
Kbd.clearScreen();
personArray[i] = new Person();
break;
}
}
break;
case 2:
try {
String s = Kbd.readString("\nPassword: ");
for (int i = 0; i < n; i++) {
if (personArray[i] != null && s.equalsIgnoreCase(personArray[i].getPassword()))
{
personIndex = i;
}
}
Kbd.clearScreen();
System.out.println("Welcome: "+personArray[personIndex].getName());
accountMeny(personIndex);
} catch(Exception e) {
System.out.println("The account which you tried to log in to doesn't exist, try again please");
}
break;
case 3:
System.out.println("Bye!");
break;
default:
Kbd.clearScreen();
System.out.println("Error, try again");
break;
}
}
}
public static void accountMenu(int personIndex) {
int mMeny = 1;
int accountIndex = -1;
int n = personArray[personIndex].accountArray.length;
while(mMeny != 3) {
mMeny = Kbd.readInt("\nClick 1 to make a bank account\nClick 2 to log in to a bank account\nClick 3 to return to previous menu");
switch(mMeny) {
case 1:
for (int i = 0; i< n; i++) {
// This is where I want to check if the accountnumber you input is valid or a duplicate or a already existing one
if (personArray[personIndex].accountArray[i] == null) {
personArray[personIndex].accountArray[i] = new Account();
break;
}
}
break;
case 2:
try{
String s = Kbd.readString("Please input your account number: ");
for (int i = 0; i< n; i++) {
if (personArray[personIndex].accountArray[i] != null && s.equals(personArray[personIndex].accountArray[i].getAccountNumber())) {
accountIndex = i;
}
}
Kbd.clearScreen();
System.out.println("Welcome: "+personArray[personIndex].accountArray[accountIndex].getAccountNumber());
choiceMenu(personIndex,accountIndex);
}catch(Exception e) {
System.out.println("The bank account you tried to log in to doesn't exist, try again!");
}
break;
case 3:
Kbd.clearScreen();
break;
default:
Kbd.clearScreen();
System.out.println("Error, try again");
break;
}
}
}
public static void choiceMenu(int personIndex,int accountIndex) {
int iMeny = 1;
while(iMeny != 4) {
System.out.println();
iMeny = Kbd.readInt("Click 1 to see your balance\nCick 2 to make a withdrawal\nClick 3 to make a deposit\nClick 4 to exit this menu");
System.out.println();
switch(iMeny) {
case 1:
personArray[personIndex].accountArray[accountIndex].balanceCheck();
break;
case 2:
personArray[personIndex].accountArray[accountIndex].withdrawal();
break;
case 3:
personArray[personIndex].accountArray[accountIndex].deposit();
break;
default:
Kbd.clearScreen();
System.out.println("Error, try again");
break;
}
System.out.println();
}
}
public static void getSave() {
try {
FileInputStream fis=new FileInputStream("savefile_1");
ObjectInputStream ois=new ObjectInputStream(fis);
personArray = (Person[]) ois.readObject();
ois.close();
} catch (Throwable t) {System.out.println("No save file availabe!"); }
}
public static void save() {
try {
FileOutputStream fos=new FileOutputStream("savefile_1");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(personArray);
oos.close();
} catch (Throwable t) {System.out.println("Error at saving"); }
}
}
这是我使用的课程
人:
public class Person implements Serializable {
private String name;
private String SSN;
private String password;
public Konto[] accountArray = new Konto[10];
public Person(){
name = Kbd.readString("Name: ");
SSN = Kbd.readString("SSN: ");
password = Kbd.readString("Password:");
Kbd.clearScreen();
}
public void showAccounts() {
int amount = 0;
for(int i = 0; i < accountArray.length; i++) {
if(accountArray[i] != null) {
amount++;
}
}
System.out.println("You have: " + amount + " acounts registred!");
}
public String getPassword() {
return password;
}
public String getName() {
return name;
}
}
帐户:
public class Account implements Serializable {
private int balance, amount;
private String AccountNumber;
public Account(){
balance = Kbd.readInt("How much money do you want to deposit?");
AccountNumber = Kbd.readString("What accountnumber do you wish to have?");
Kbd.clearScreen();
}
public void balanceCheck(){
Kbd.clearScreen();
System.out.printf("On account: %s\nThere is: %d $",AccountNumber,balance);
}
public void withdrawal(){
Kbd.clearScreen();
amount = Kbd.readInt("How much do you wish to withdraw?");
balance -= amount;
System.out.printf("You now have: %d\nYou took out: %d $",balance,amount);
}
public void deposit(){
Kbd.clearScreen();
amount = Kbd.readInt("Hur mycket vill du sätta in?");
balance += amount;
System.out.printf("You now have: %d\nYou deposited: %d $",balance,amount);
}
public String getAccountNumber(){
return AccountNumber;
}
}
这里是我开始使用的方法,但不知道如何进一步发展:
public static boolean check(int n,int personIndex) {
boolean valid= true;
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
if(personArray[personIndex].accountArray[i].getAccountNumber().equals(personArray[personIndex].accountArray[j].getAccountNumber())) {
return valid = false;
}
}
}
return valid;
}
尝试此方法进行检查:
public boolean checkAccountNumber(String s) {
int n = personArray[personIndex].accountArray.length;
for (int i = 0; i < n; i++) {
if (personArray[personIndex].accountArray[i] != null && s.equals(personArray[personIndex].accountArray[i].getAccountNumber())) {
return false;
}
return true;
}