如何为内部类正确创建对象?

时间:2019-05-17 08:14:40

标签: java

我正在练习动态编程,我通过创建一个银行项目来做到这一点。 在这里我想有很多客户,每个客户可以有很多不同容量的礼品卡。因此,我在类客户中创建了一个类giftcard1,但是如何实例化它并添加它。这样,当我遍历该客户时,我就可以获得礼品卡的详细信息。

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

class Customer
{
    int id;
    String pwd;
    int gift;
    int pin;
    int bal=0;
    //int giftcard balance
    class giftcard1
    {
        int gift_bal;
        List<String> l2=new ArrayList<String>();
    }
}

class MyClass
{
    public static void main(String args[])
    {
       Scanner sc=new Scanner((System.in));
       List<Customer> l1=new ArrayList<Customer>();

       int choice=1;

       while(choice==1||choice==2)
       {
           System.out.println("1.Account Creation 2.Purchase");
           choice=sc.nextInt();
           cancel:
           for(int j=0;j<1;j++) {
               if (choice == 1) {
                   System.out.println("Please enter id and pwd");
                   int id1 = sc.nextInt();
                   String s = sc.next();
                   char c[] = s.toCharArray();
                   int i;
                   for (i = 0; i < s.length(); i++) {
                       c[i] = (char) (c[i] + 1);
                       //System.out.print(c[i]);
                   }
                   String s1 = new String(c);
                   System.out.println(s1);
                   Customer cu = new Customer();
                   Customer.giftcard1 g=new cu.giftcard1();
//need help above
                   cu.id = id1;
                   cu.pwd = s1;
                   l1.add(cu);

                   System.out.println("1.GiftCCard 2.TopUP 3. Transaction History 4.Block 5.Logout");
                   int ch1 = sc.nextInt();
                   while ((ch1!=0)) {
                       if (ch1 == 1) {
                           int giftcard = (int) (Math.random() * 100000);
                           int pin = (int) (Math.random() * 10000);
                           cu.gift = giftcard;
                           cu.pin = pin;
                           System.out.println("Giftcard No " + giftcard + " " + "Pin " + pin);
                       }
                       if (ch1 == 2) {
                           System.out.println("Enter the amount needed to Recharge");
                           int amt = sc.nextInt();
                           if(amt>cu.bal)
                           {
                               System.out.println("Not enough Money");
                           }
                           else
                           {
                               g.gift_bal=amt;
                               g.l2.add(amt+" is added");
                           }
                       }
                   }
               }
           }
       }
    }
}

我想为Customer对象cu创建一个类别giftcard的对象g。

2 个答案:

答案 0 :(得分:3)

首先,我建议您学习naming conventions

第二,我还建议将GiftCard类移出Customer类。

要存储n GiftCard,则应将List<GiftCard>作为班级成员添加到Customer班级。

class Customer {
    int id;
    String pwd;
    int gift;
    int pin;
    int bal=0;
    List<GiftCard> giftCards;
}

class GiftCard {
    int balance;
}

注意,您应该了解如何使用构造函数。

因此您创建并存储它:

Customer customer = ...;
....

customer.giftCards = new ArrayList<>();

GiftCard card1 = new GiftCard();
card1.balance = 100;
customer.giftCards.add(card1);

现在客户有1个GiftCardbalance 100

答案 1 :(得分:0)

以下是示例:

Customer c = ...
Customer.giftcard1 g = c.new giftcard1();

您必须通过添加public修饰符使这两个类均可见。