我試圖存儲用戶賬戶信息到一個單獨的文本文件,我已經多次嘗試後完成...序列化對象被覆蓋,不添加到
但是每一次我創建覆蓋的新用戶最後一個放入文件中。 另外,存儲的唯一東西是字符串,我有一個PIN號碼和一個賬戶餘額,我以後需要的是非常重要的。
這是我到目前爲止,我認爲問題是每次我運行代碼Object
命名用戶正在創建,並且因爲它不是動態的,它只是每次都覆蓋自己。
在序列化任何東西時我都是noob,所以如果你能用最簡單的話來解釋這個問題,那就太棒了!
的目標是讓我能夠存儲每一個新的用戶帳戶,然後回來在稍後將它們顯示給用戶,如果帳戶信息的引腳數量和用戶名
public class ATM implements Serializable {
public static void main(String[] args) {
// variables
String dash = "-------------------\n";
int accounts = 0;
// Scanner
Scanner scanner = new Scanner(System.in);
// Welcome screen
System.out.print(dash);
System.out.print("Welcome to the Bank\n");
System.out.print(dash);
System.out.println("Do you have an account with us? (y/n) ");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("y")) {
} else {
// new user is created
Bank bank = new Bank();
accounts++;
System.out
.println("Enter your full name below (e.g. John M. Smith): ");
String name = scanner.nextLine();
System.out.println("Create a username: ");
String userName = scanner.nextLine();
System.out.println("Enter your starting deposit amount: ");
int balance = scanner.nextInt();
System.out.print(dash);
System.out.print("Generating your information...\n");
System.out.print(dash);
int pin = bank.PIN();
String accountNum = bank.accountNum();
String id = name + accountNum;
User user = new User(name, userName, pin, accountNum, balance);
// new user gets added to the array list
Bank.users.add(user);
String test = "Test989898998";
System.out.println(user);
try {
FileOutputStream fileOut = new FileOutputStream("users.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(Bank.users);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
}
}
匹配銀行類
public class Bank implements Serializable {
//Generate a random 16 digit bank account number
public String accountNum() {
int max = 9999;
int min = 1000;
int a1 = (int) (Math.random() * (max - min) + min);
int a2 = (int) (Math.random() * (max - min) + min);
int a3 = (int) (Math.random() * (max - min) + min);
int a4 = (int) (Math.random() * (max - min) + min);
String accountNum = a1 + "-" + a2 + "-" + a3 + "-" + a4;
return accountNum;
}
//Generate a random 4 digit PIN
public int PIN() {
int max = 9999;
int min = 1000;
int PIN = (int) (Math.random() * (max - min) + min);
return PIN;
}
//array list for users
static ArrayList<User> users = new ArrayList<User>() {
};
}
用戶類別
public class User implements Serializable{
String name;
String userName;
String accountNum;
int pin;
int balance;
public User (String name, String userName, int pin, String accountNum, int balance) {
this.name = name;
this.userName = userName;
this.accountNum = accountNum;
this.pin = pin;
this.balance = balance;
}
public String toString() {
return "Name: " + this.name + "\n\nUsername: " + this.userName + " | " + "Pin: " + this.pin + "\n\n"
+ "Account Number: " + this.accountNum + "\n\nAccount Balance: $" + this.balance +
"\n\nNever share your login information with anyone!";
}
}
發佈你'User'和'Bank'類。 –
@ PM77-1我把它們放在那裏爲雅 – 23k
我明白了。用戶類的構造函數似乎沒問題。 PIN和帳號應該已經分配並保存爲「用戶」對象的一部分。你100%肯定他們不是? –