为每个客户创建一个唯一对象

时间:2014-04-29 18:21:15

标签: java

这是一项硬件分配

DisplayMenu.java

Customer.java

Account.java

因此,问题要求为客户创建一个菜单并为其创建一个具有唯一ID的帐户。一旦我们创建了一个客户对象,我们就为该客户创建了一个帐户对象。我总是可以说客户c1 =新客户( )但这是手动的,因为我无法控制将要创建的对象的数量。

我的问题是当选项菜单是do while循环时,如何创建客户和帐户类型的唯一对象。

有人可以对此有所了解吗

import java.util.*;



public class UserController{
    public static void main(String args[]){
        Scanner sn = new Scanner(System.in);
        int option;
        do {  
            option=0;
            System.out.println("Please Select from menu below");
            System.out.println("1.Create Personal Customer");
            System.out.println("2.Create Commericial Customer");
            System.out.println("3.Record Transaction");
            System.out.println("4.Make Withdrawl");   
            System.out.println("5.Display Customer");
            System.out.println("6.Display Customer Summary");
            System.out.println("7.Display Grand Summary");
            System.out.println("8.Exit");
            System.out.println("Please Enter a option");
            try{
                option=sn.nextInt();
            } catch(Exception e){
                System.out.println("Please Enter a number");
                option = 9;
            }
            //      System.out.println(option+"value is");
            switch(option){
                case 1: System.out.println("Enter the name of the Customer");
                    String Cname = sn.nextLine();
                    personalCustomer p1 = new personalCustomer(Cname);
                    break;
                case 2:break;
                case 3:break;
                case 4:break;
                case 5:break;
                case 6:break;
                case 7:break;
                case 8:break;
                default:
                    System.out.println("Invalid Option Please choose a valid option");
                    break;
            }
        } while (option!=8);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试使用List

进行试验
List<Customer> customers = new List<Customer>();

do {
    customers.add(new Customer(/* Possible args */));
} while (/* condition to keep making customers */);

然后您可以使用List.get

访问它们
customers.get(0);
customers.get(1);
/* etc */

java.util.Collections Library

中的许多选项

您也可以自己编写,以便更好地了解如何动态存储对象。这是一个基本的例子。

  1. 您将对象存储在数组中:T[] list
  2. 您可以将它们添加到下一个可用空间,该空间由整数int nextIndex跟踪,该整数从0开始,每次添加时都会递增。
  3. 当您添加内容时,检查是否已达到存储列表的当前容量,如果有,请创建一个旧列表大小的两倍的新列表(T[] newList),复制值,然后然后用这个新列表替换旧列表。
  4. T是一个generic,用于概括将要提供的输入。

    class AutoExpandingList<T> {
        T[] list;
        int nextIndex;
        public AutoExpandingList() {
            this.list = new T[10];
            this.nextIndex = 0;
        }
        public void add(T item) {
            if (nextIndex >= list.length) {
                expandList();
            }
            list[nextIndex] = item;
            nextIndex++;
        }
        public void expandList() {
            T[] newList = new T[list.length*2];
            for (int i = 0; i < list.length; i++) {
                newList[i] = list[i];
            }
            this.list = newList;
        }
        public T get(int index) {
            return list[index];
        }
        // You could implement other methods here:
        //     remove(int index)
        //     insert(int index, T item)
        //     sort()
        //     etc...
    }
    

    您现在可以使用:

    AutoExpandingList<Integer> numbers = new AutoExpandingList<Integer>();
    numbers.add(1);
    numbers.add(5);
    
    AutoExpandingList<String> strings = new AutoExpandingList<String>();
    strings.add("one");
    strings.add("five");
    
    AutoExpandingList<Customer> costumers = new AutoExpandingList<Costumer>();
    costumers.add(new Customer(1));
    costumers.add(new Customer(5));
    

    对于您提供的代码,您只需实现其中一种动态存储方法,然后使用它来存储您的客户:

    public class UserController{
        public static void main(String args[]){
            // ...
            List<Customer> customers = new List<Customer>();
            do {
                // ...
                switch(option){
                    case 1:
                        // ...
                        customers.add(new personalCustomer(Cname));
                        break;
                    // ...
                }
            } while (option!=8);
        }
    }
    

    修改

    在回复您的评论时,我建议您检查整个列表

    public Customer searchByName(List<Customer> list, String name) {
        for (int i = 0; i < list.size(); i++) {
            if ( list.get(i).name.equals(name) ) {
                return list.get(i);
            }
        }
        return null;
    }
    

    遍历每个Customer,检查每个客户名称,然后从返回的对象中获取Customer信息。