2014-01-15 35 views
0

我试图存储用户账户信息到一个单独的文本文件,我已经多次尝试后完成...序列化对象被覆盖,不添加到

但是每一次我创建覆盖的新用户最后一个放入文件中。 另外,存储的唯一东西是字符串,我有一个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!"; 
} 

} 
+0

发布你'User'和'Bank'类。 –

+0

@ PM77-1我把它们放在那里为雅 – 23k

+0

我明白了。用户类的构造函数似乎没问题。 PIN和帐号应该已经分配并保存为“用户”对象的一部分。你100%肯定他们不是? –

回答

1

但是,每次我创建一个新用户时,它都会覆盖放入文件的最后一个用户。

这是正确的。你不是追加到文件中,而是创建一个新文件。这与对象或序列化无关,它只是您正在使用的FileOutputStream的构造函数的问题。

但是,没有使用我不会在这里引用的技巧,你不能追加到对象流,因为有一个头会混淆后续的读者。您需要阅读整个文件并再次写出。最简单的方法是序列化一个ArrayList<User>而不是User对象本身。

+0

仍然困惑,认为可以用简单的方式解释它条款对我来说? – 23k

+0

'序列化ArrayList'的哪部分你不明白? – EJP

+0

我认为在上面的代码中,我创建了一个新的person对象,并将它添加到'ArrayList'中。然后通过编写'out.writeObject(Bank.users)'''''''''''' – 23k