如何永久保存用户输入?

时间:2021-11-23 21:31:16

So I have created an Account class and i wanted to save the user details permenantly for future use. For example, should the user create an account, i would like him/her to be able to use his info including balance in the future. Please help me!

所以我创建了一个Account类,我想永久保存用户详细信息以备将来使用。例如,如果用户创建帐户,我希望他/她能够使用他的信息,包括将来的余额。请帮我!

package mini_project;

import java.util.Random;
import javax.swing.JOptionPane;


public class Account {

private String firstName, surname;
private short transactionNumber;
String input;
private int startingAmount;

public Account(String first, String sur, int amount){

    firstName = first;
    surname = sur;
    startingAmount = amount;
}

public void setFirstName (){

    input = JOptionPane.showInputDialog("What Is Your First  Name?").toUpperCase();
    firstName = input;
}

public String getFirstName (){

    return firstName;
}

public void setSurname (){

input = JOptionPane.showInputDialog("What Is Your  Surname?").toUpperCase   ();
    surname = input;
}

public String getSurname (){

    return surname;
}

public void setTransactionNumber (){

    Random rand = new Random ();
    int randomNumber = rand.nextInt(1000)+1;
    transactionNumber = (short) randomNumber;
}

public short getTransactionNumber(){

    return transactionNumber;
}

public void setShares(){

    input = JOptionPane.showInputDialog("How Many Shares Do You Currently Own?");
    startingAmount = Integer.parseInt(input);
}

public int getShares(){

    return startingAmount;
}
}

2 个解决方案

#1


0  

You could always make the account class implement Serializable. This will allow you to save and import the object's instance, in this case individual account objects. These instances can be saved into a text file. They can also be encrypted if your program requires more security.

您始终可以使帐户类实现Serializable。这将允许您保存和导入对象的实例,在这种情况下是单个帐户对象。这些实例可以保存到文本文件中。如果您的程序需要更高的安全性,它们也可以加密。

#2


-1  

If you want to save the data permanently, You need to use database. You can make your instance variable static. It will save permanently unless you close the program.

如果要永久保存数据,则需要使用数据库。您可以将实例变量设置为静态。除非您关闭程序,否则它将永久保存。

private static String firstName, surname;
private static short transactionNumber;
private static int startingAmount;

Then get the setters and getters. But remove the static word in the functions of setters and getters. You only need static in an instance variable.

然后得到安装者和吸气剂。但是删除setter和getters函数中的静态单词。您只需要在实例变量中使用static。

#1


0  

You could always make the account class implement Serializable. This will allow you to save and import the object's instance, in this case individual account objects. These instances can be saved into a text file. They can also be encrypted if your program requires more security.

您始终可以使帐户类实现Serializable。这将允许您保存和导入对象的实例,在这种情况下是单个帐户对象。这些实例可以保存到文本文件中。如果您的程序需要更高的安全性,它们也可以加密。

#2


-1  

If you want to save the data permanently, You need to use database. You can make your instance variable static. It will save permanently unless you close the program.

如果要永久保存数据,则需要使用数据库。您可以将实例变量设置为静态。除非您关闭程序,否则它将永久保存。

private static String firstName, surname;
private static short transactionNumber;
private static int startingAmount;

Then get the setters and getters. But remove the static word in the functions of setters and getters. You only need static in an instance variable.

然后得到安装者和吸气剂。但是删除setter和getters函数中的静态单词。您只需要在实例变量中使用static。