How to get an instance of a class? [closed]

2019-09-02 17:19发布

问题:

I have the following code

class driver{
        static BankAccount GetAccount(Customer customer, char c) {
            BankAccount accSrc = customer.S;
            // savings account
            if (c =='S') {
                accSrc = customer.S;
            // loan account
            } else if (c =='L') {
                accSrc = customer.L;
            // checking account
            } else if (c =='C') {
                accSrc = customer.C;
            // auto loan account
            } else if (c =='A') {
                accSrc = customer.A;
            }
            return accSrc;
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Customer forrest = new Customer("Forrest Gump", 1, "42 New Street, New York, New York"); // me
            Customer random = new Customer("Random Name", 2, "44 New Street, New York, New York"); // imaginary partner
            //try{
                String input = JOptionPane.showInputDialog("Please enter your transaction information: ");
                Scanner s = new Scanner(input);
                int id  = Integer.parseInt(s.next());
                char action = Character.toUpperCase((s.next().charAt(0)));
                char accSrc = ' ';
                char accDest = ' ';
                double amount = 0;

                if(action == 'T'){
                    amount = s.nextDouble();
                    accSrc = s.next().charAt(0);
                    accDest = s.next().charAt(0);
                }else if(action == 'G' || action == 'I'){
                    accSrc = s.next().charAt(0);
                }else{
                    //if D,W
                    amount = s.nextDouble();
                    accSrc = s.next().charAt(0);
                }

            //}catch (IOException e){

            //}
                if(id==1){
                    return forrest;
                }else if(id == 2){
                    return random;
                }
                BankAccount src = GetAccount(forrest,  accSrc);
                System.out.print(src.getOwner().name);
                if(action == 'T'){
                    BankAccount dst = GetAccount(forrest, accDest);
                    src.transfer(amount, dst);
.

    ..
    }

    class Customer{
        protected String name;
        protected int id;
        protected String address;
        protected BankAccount C = new BankAccount(id, this, 0);
        protected BankAccount S = new BankAccount(id, this, 0);
        protected BankAccount A = new BankAccount(id, this, 0);
        protected BankAccount L = new BankAccount(id, this, 0);
    ...
    }

currently im hard coding BankAccount src = GetAccount(forrest, accSrc); how can I proceed to writing code so it returns an instance of the customers given an id number (say 1 given returns forrest, 2 given returns random) ?

回答1:

Put your classes in a Map e.g.

 Map<Integer, Customer> classMap = new HashMap<Integer, Customer>();
 Customer forrest =
        new Customer("Forrest Stallings", 1, "42 New Street, New York, New York"); 
 classMap.put(1, forrest );

 Customer random = 
        new Customer("Random Name", 2, "44 New Street, New York, New York");
 classMap.put(2, random );

Then simply get your class as:

Customer forrest= classMap.get(1); 
Customer random = classMap.get(2);


回答2:

You need to store your stuff in a List and then retrieve a list item by its index.



回答3:

better yet

Customer customers[] = { forrest, random };

I can call the instance with customer[id]



标签: java class