用Java生成一个保修期搜索代码
时间: 2023-04-02 21:03:08 浏览: 118
你好,我是 C 知道。关于你的问题,我可以回答。以下是一个简单的 Java 代码示例,用于搜索保修期:
```
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class WarrantySearch {
public static void main(String[] args) {
// 创建一个保修期列表
List<Warranty> warranties = new ArrayList<>();
warranties.add(new Warranty("A001", LocalDate.of(2021, 1, 1), LocalDate.of(2022, 1, 1)));
warranties.add(new Warranty("A002", LocalDate.of(2021, 2, 1), LocalDate.of(2022, 2, 1)));
warranties.add(new Warranty("A003", LocalDate.of(2021, 3, 1), LocalDate.of(2022, 3, 1)));
warranties.add(new Warranty("A004", LocalDate.of(2021, 4, 1), LocalDate.of(2022, 4, 1)));
warranties.add(new Warranty("A005", LocalDate.of(2021, 5, 1), LocalDate.of(2022, 5, 1)));
// 搜索保修期
LocalDate searchDate = LocalDate.of(2021, 12, 1);
for (Warranty warranty : warranties) {
if (warranty.isInWarranty(searchDate)) {
System.out.println(warranty.getProductCode() + " 在保修期内");
} else {
System.out.println(warranty.getProductCode() + " 不在保修期内");
}
}
}
}
class Warranty {
private String productCode;
private LocalDate startDate;
private LocalDate endDate;
public Warranty(String productCode, LocalDate startDate, LocalDate endDate) {
this.productCode = productCode;
this.startDate = startDate;
this.endDate = endDate;
}
public String getProductCode() {
return productCode;
}
public boolean isInWarranty(LocalDate date) {
return !date.isBefore(startDate) && !date.isAfter(endDate);
}
}
```
希望这可以帮助你。
阅读全文