我试图存储用户账户信息到一个单独的文本文件,我已经多次尝试后完成...序列化对象被覆盖,不添加到
但是每一次我创建覆盖的新用户最后一个放入文件中。 另外,存储的唯一东西是字符串,我有一个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%肯定他们不是? –