You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于手机号检测CabCustomer列表重复项,返回布尔值列表

需求实现:根据CabCustomer列表生成新客户判断布尔列表

需求说明

针对存储CabCustomer对象的ArrayList,生成与列表元素一一对应的布尔值列表:

  • 若当前客户的phone字段是首次出现(未在之前的列表元素中出现过),对应位置返回true(表示是新客户)
  • 若当前客户的phone字段已在之前的元素中存在(重复),对应位置返回false(表示不是新客户)

现有代码框架

CabCustomer类

package cabServiceProgram;

class CabCustomer
{
private int custId, distance;
private String customerName, pickupLocation, dropLocation, phone;

public CabCustomer()
{
    this.custId         = 0;
    this.distance       = 0;
    this.customerName   = null;
    this.pickupLocation = null;
    this.dropLocation   = null;
    this.phone          = null;
}

public CabCustomer(int custId, String customerName,String pickupLocation,String dropLocation, int distance,
        String phone)
{
    this.custId         = custId;
    this.distance       = distance;
    this.customerName   = customerName;
    this.pickupLocation = pickupLocation;
    this.dropLocation   = dropLocation;
    this.phone          = phone;
}
public int getCustId()
{
    return custId;
}
public void setCustId(int custId)
{
    this.custId = custId;
}
public int getDistance()
{
    return distance;
}
public void setDistance(int distance)
{
    this.distance = distance;
}
public String getCustomerName()
{
    return customerName;
}
public void setCustomerName(String customerName)
{
    this.customerName = customerName;
}
public String getPickupLocation()
{
    return pickupLocation;
}
public void setPickupLocation(String pickupLocation)
{
    this.pickupLocation = pickupLocation;
}
public String getDropLocation()
{
    return dropLocation;
}
public void setDropLocation(String dropLocation)
{
    this.dropLocation = dropLocation;
}
public String getPhone()
{
    return phone;
}
public void setPhone(String phone)
{
    this.phone = phone;
}
}

CabCustomerService类

package cabServiceProgram;

import java.util.ArrayList;

class CabCustomerService
{
    private ArrayList<CabCustomer> customerList = new ArrayList<CabCustomer>();

    public void addCabCustomer(CabCustomer customer)
    {
        customerList.add(customer);
    }
}

CabCustomerServiceTester类

package cabServiceProgram;

public class CabCustomerServiceTester
{
public static void main(String[] args)
{
    CabCustomer cb=new CabCustomer(101, "David", "Jersey", "New York", 1, "123");
    CabCustomer cb1=new CabCustomer(102, "John", "Houston", "Boston", 3, "000");
    CabCustomer cb2=new CabCustomer(103, "Connor","California", "Alaska", 4, "123");
    CabCustomer cb3=new CabCustomer(104, "Mark", "Los Angeles", "Nevada", 5, "321");
    CabCustomerService ccs= new CabCustomerService();
    ccs.addCabCustomer(cb);
    ccs.addCabCustomer(cb1);
    ccs.addCabCustomer(cb2);
    ccs.addCabCustomer(cb3);
}
}

解决方案实现

我们可以在CabCustomerService类中添加一个方法,通过HashSet跟踪已出现的手机号,遍历客户列表生成对应的布尔值列表:

修改后的CabCustomerService类

package cabServiceProgram;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

class CabCustomerService
{
    private ArrayList<CabCustomer> customerList = new ArrayList<CabCustomer>();

    public void addCabCustomer(CabCustomer customer)
    {
        customerList.add(customer);
    }

    // 生成新客户判断布尔列表的方法
    public List<Boolean> generateNewCustomerFlags() {
        List<Boolean> flags = new ArrayList<>();
        HashSet<String> seenPhones = new HashSet<>();

        for (CabCustomer customer : customerList) {
            String phone = customer.getPhone();
            // 判断当前手机号是否已存在,不存在则标记为true并加入集合
            if (!seenPhones.contains(phone)) {
                flags.add(true);
                seenPhones.add(phone);
            } else {
                flags.add(false);
            }
        }
        return flags;
    }
}

修改后的测试类(添加结果输出)

package cabServiceProgram;

import java.util.List;

public class CabCustomerServiceTester
{
public static void main(String[] args)
{
    CabCustomer cb=new CabCustomer(101, "David", "Jersey", "New York", 1, "123");
    CabCustomer cb1=new CabCustomer(102, "John", "Houston", "Boston", 3, "000");
    CabCustomer cb2=new CabCustomer(103, "Connor","California", "Alaska", 4, "123");
    CabCustomer cb3=new CabCustomer(104, "Mark", "Los Angeles", "Nevada", 5, "321");
    CabCustomerService ccs= new CabCustomerService();
    ccs.addCabCustomer(cb);
    ccs.addCabCustomer(cb1);
    ccs.addCabCustomer(cb2);
    ccs.addCabCustomer(cb3);

    // 调用方法并输出结果
    List<Boolean> newCustomerFlags = ccs.generateNewCustomerFlags();
    System.out.println("新客户判断结果列表:");
    for (boolean flag : newCustomerFlags) {
        System.out.print(flag + " ");
    }
    // 输出结果应为:true true false true
}
}

内容的提问来源于stack exchange,提问作者Vikas Tiwari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 04:50:24