如何在Java中将多个数据类型赋值给单个变量?

huangapple go评论82阅读模式
英文:

How to assign multiple data types to a single variable in java?

问题

现在我正在创建一个银行应用程序,我不知道如何将例如 string name; double balance; 等分配给正确的 int PIN;。将会有许多带有不同PIN和不同值的帐户。我尝试创建多个对象:

perInfo card1 = new perInfo();
card1.PIN = 1994;
card1.balance = 24.68;
card1.isValid = true;

perInfo karta2 = new perInfo();
card2.PIN = 2002;
card2.balance = 522.2;
card2.isValid = false;

但我认为这样做太麻烦了,而且会影响应用程序的性能。我还尝试过创建一个列表:

public bApp(int pin, double balance) {
    this.pin = pin;
    this.balance = balance;
}

List<bApp> pass = new ArrayList<>();
pass.add(new bApp(1994, 568.45));
pass.add(new bApp(2002, 13866.24));

但这没有起作用,因为我无法调用PIN来检查用户是否提供了正确的PIN。数组也不适用于这种情况。

英文:

Right now I'm creating a bankApp and I don't have an idea how can I assign e.g. string name; double balance; etc. to a right one int PIN;. There is gonna be many accounts with different PINs and different values assigned to it . I tried making many objects :

    perInfo card1 = new perInfo();
    card1.PIN = 1994;
    card1.balance = 24.68;
    card1.isValid = true;

    perInfo karta2 = new perInfo();
    card2.PIN = 2002;
    card2.balance = 522.2;
    card2.isValid = false;

but I think it's too much work to do and it'll worsen performance of the app. I also tried making a list

    public bApp(int pin, double balance){
    this.pin = pin;
    this.balance = balance;}

    List&lt;bApp&gt; pass = new ArrayList&lt;&gt;();
        pass.add(new bApp(1994, 568.45));
        pass.add(new bApp(2002, 13866.24));

but it didn't work ,because I couldn't call the PIN to check that if user has provided the right one PIN . Arrays are also not suited for this.

答案1

得分: 0

你正在寻找的数据结构是一个映射(Map)。看一下 HashMap,你会想要以你用来查找的任何值作为键。

例如,如果你想通过 PIN 码查找用户:

Map<Integer, bApp> passes = new HashMap<>();
passes.put(1994, new bApp(1994, 568.45));
passes.put(2002, new bApp(2002, 13866.24));
英文:

The data structure you are looking for is a Map. Take a look at HashMap and you'll want to key off of whatever value you are using to lookup.

For example if you wanted to lookup a user by pin:

Map&lt;Integer, bApp&gt; passes = new HashMap&lt;&gt;();
passes.put(1994, new bApp(1994, 568.45));
passes.put(2002, new bApp(2002, 13866.24));

答案2

得分: 0

你可以使用HashMap来实现这个,将pin作为键存储在HashMap中,并将对象存储在其中。这将允许您仅使用pin来访问每张卡。然而,这将不允许重复的pin。我建议您通过唯一的ID来引用每个账户,并在对象内部检查pin。

HashMap<Integer, Account> accounts = new HashMap<Integer, Account>();
accounts.put(12345678, new Account(1994, 568.4));

然后,您可以使用唯一的ID获取账户,并检查pin是否正确。

Account acc = accounts.get(uniqueID);
if(acc.pin == enteredPin){
    //无论您需要做什么
}
英文:

You could use a HashMap for this and use the pin as a key and store the object in the HashMap. This would allow you to access each card using only the pin. However, this would not allow for duplicate pins. I would recommend that you reference each account by a unique ID and check the pin within the object itself.

HashMap&lt;Integer, Account&gt; accounts = new HashMap&lt;Integer, Account&gt;();
accounts.put(12345678, new Account(1994, 568.4));

You can then get the account using the unique ID and check if the pin is correct.

Account acc = accounts.get(uniqueID);
if(acc.pin == enteredPin){
    //Whatever you need to do
}

答案3

得分: 0

我认为只需要一个包含不同卡片的数组,每个 PIN 设置为数组地址会更简单:

bApp[] cards = new bApp[NUMBER_OF_CARDS];
cards[0] = new bApp(0, 568.45);
// ...
cards[2002] = new bApp(2002, 13866.24);
英文:

I think it would be simpler to just have an array of the different cards, with each pin being set to the array address:

bApp[] cards = new bApp[NUMBER_OF_CARDS];
cards[0] = new bApp(0, 568.45);
// ...
cards[2002] = new bApp(2002, 13866.24);

huangapple
  • 本文由 发表于 2020年8月27日 06:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63606575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定