如何编辑此方法以使用客户数组?

时间:2019-05-01 17:14:32

标签: java arrays indexing

我应该创建一个接受客户ID的方法,然后在找到该ID的情况下返回数组中的索引。使用该方法时,只能输入整数ID。

我使用了迭代的二进制搜索,以便能够搜索我的客户数组列表。但是问题在于,将Customer数组与Integer进行比较时,它被操作数类型卡住了。我试图将方法的类型更改为static,CustomerList,Customer等。但这根本不影响它。

代码中的

cl是我在课程开始时创建的公共字段。

作为->公共客户[] cl;

**public class Person {
    private String firstName;
    private String lastName;
    private String address;
    private String city;
    private String state;
    private String zipCode;    

    public Person(String firstName, String lastName, String address, String city, String state, String zipCode) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;    
    }

    protected Person() {

    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Override
    public String toString() {
        return "FirstName: " + firstName + "\nLastName: " + lastName + "\nAddress: " + address + "\nCity: " + city + "\nState: " + state + "\nZipCode: " + zipCode;
    }

    public String toCSV() {
        return this.firstName + "," + this.lastName + "," + this.address + "," + this.city
                + "," + this.state + "," + this.zipCode;
    }

    public void copy(Person p) {       
        firstName = p.firstName;
        lastName = p.lastName;
        address = p.address;
        city = p.city;
        state = p.state;
        zipCode = p.zipCode;
    }

    public void copy(String firstName, String lastName, String address, String city, String state, String zipCode) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.city = city;
        this.zipCode = zipCode;
    }

    @Override
    public Person clone() {
        Person p = new Person(this.firstName, this.lastName, this.address, this.city, this.state, this.zipCode);
        return p;
    }
}**

**public class Customer extends Person{
    private int customerID;
    private double grossSales;

    public Customer(int customerID, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
        super(firstName, lastName, address, city, state, zipCode);
        this.customerID = customerID;
        this.grossSales = grossSales;
    }

    public Customer(String s, int customerID, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
        super(firstName, lastName, address, city, state, zipCode);
        copyCSV(s);
    }

    protected Customer() {

    }

    public int getCustomerID() {
        return customerID;
    }

    public void setCustomerID(int customerID) {
        this.customerID = customerID;
    }

    public double getGrossSales() {
        return grossSales;
    }

    public void setGrossSales(double grossSales) {
        this.grossSales = grossSales;
    }

    @Override
    public String toString() {
        return "CustomerID: " + customerID + "\nGrossSales: " + grossSales + super.toString();
    }

    public String toCSV() {
        return this.customerID + "," + this.grossSales + "," + super.toCSV();
    }

    public void copy(Customer c) {
        super.copy(c);
        customerID = c.customerID;
        grossSales = c.grossSales;
    }

    public void copy(int customerId, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
        super.copy(firstName, lastName, address, city, state, zipCode);
        this.customerID = customerId;
        this.grossSales = grossSales;
    }

    public Customer clone() {
        Customer c = new Customer(this.customerID, this.grossSales, this.getFirstName(), this.getLastName(), this.getAddress(), this.getCity(), this.getState(), this.getZipCode());
        return c;    
    }

    public int compareTo(Customer c) {
        int returnValue = 0;

        if (this.customerID > c.customerID) {
            returnValue = -1;
        } else if (this.customerID < c.customerID) {
            returnValue = 1;
        } else {
            returnValue = 0;
        }

        return returnValue;
    }

    public void copyCSV(String s) {
        List<String> list = new ArrayList<>();

        String[] a = s.split(",");

        list = Arrays.asList(a);

        this.copy(Integer.parseInt(list.get(0)), Double.parseDouble(list.get(1)), list.get(2), 
                list.get(3), list.get(4), list.get(5), list.get(6), list.get(7));
    }

}**

public int indexOf(Integer id) {
        int min = 0;
        int max = cl.length - 1; 

        while (min <= max) {
            int mid = (min + max) / 2;
            if (cl[mid] < id) {
                min = mid + 1;
            } else if (cl[mid] > id) {
                max = mid - 1;
            } else {
                return mid;   // target found
            }
        }

        return -(min + 1);    // target not found
    }

应该接受一个customerID,如果找到,则返回数组中的索引,否则返回插入点。

基本上,我们有一个CSV文件,其中包含客户列表,并且为每个客户分配了一个ID。我们有人员类别和客户类别,我将它们包括在其中并设置所有变量。以及toString和toCSV方法。在最后一个类CustomerList中,有几种方法。尤其是,这个应该采用一个整数,例如1、2、3、4等。如果有人分配了该ID号,它将返回该人在创建的数组中的索引。

但是它被卡在if和else if比较语句中。

1 个答案:

答案 0 :(得分:0)

假设一个客户类别,例如:

 public class Customer {

private int id;
private String name;

public Customer() {
}

public Customer(int id, String name) {
    this.id = id;
    this.name = name;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}
}

您可以通过传递以下两种方式来以静态方法返回客户数组中ID的索引:

    public static int indexOf(Customer[] arr, Customer cust) {
    int min = 0;
    int max = arr.length - 1; 

    while (min <= max) {
        int mid = (min + max) / 2;
        if (arr[mid].getId() < cust.getId()) {
            min = mid + 1;
        } else if (arr[mid].getId() > cust.getId()) {
            max = mid - 1;
        } else {
            return mid;   // target found
        }
    }

    return -(min + 1);    // target not found
}

您还可以将其转换为非静态方法,并为Customer []对象创建一个类。或者,您可以像这样在您的主要(或任何其他方法)中使用它:

    Customer cl1 = new Customer(1234,"John");
    Customer cl2 = new Customer(1235,"Khal");
    Customer cl3 = new Customer(1236,"John");
    Customer[] clArray = {cl1,cl2,cl3};

    Customer testCustomer = new Customer(1236,"AnyName");
    int index = indexOf(clArray, testCustomer);
    System.out.println(index); //Prints 2, which is the location of 1236 in the array

客户当前正在存储名称和ID,可以更改以存储所需的任何值,您只需要编辑构造函数和私有类变量即可。

如果您打算保留信息并经常更改它,我也建议研究ArrayList。数组不是最好的处理方法。

编辑: 根据您的评论,这是CustomerArray类的样子:

public class CustomerArray {

private Customer[] cl;

public CustomerArray() {
}

public CustomerArray(Customer...customers ) {
    this.cl = customers;
}

public Customer[] getCl() {
    return cl;
}

public void setCl(Customer[] cl) {
    this.cl = cl;
}


    public int indexOf(int id) {
    int min = 0;
    int max = cl.length - 1; 

    while (min <= max) {
        int mid = (min + max) / 2;
        if (cl[mid].getId() < id) {
            min = mid + 1;
        } else if (cl[mid].getId() > id) {
            max = mid - 1;
        } else {
            return mid;   // target found
        }
    }

    return -(min + 1);    // target not found
}
}

然后您就可以在main方法(或任何其他方法)中使用它了,

    Customer cl1 = new Customer(1234,"John");
    Customer cl2 = new Customer(1235,"Khal");
    Customer cl3 = new Customer(1236,"John");
    CustomerArray clArray =  new CustomerArray(cl1,cl2,cl3);

    Customer testCustomer = new Customer(1236,"AnyName");
    int index = clArray.indexOf(testCustomer.getId());

    System.out.println(index);

因此,基本上,您要声明一个拥有所有客户的CustomerArray。