用java编写以下内容 生产制造企业开发一个零部件的库存管理系统。目前企业生产两种产品:零部件(part) 和组件 (asscembly)。 (1) 定义零部件类, (2) 增加目录类,属性包括: 零部件编号、零部件名称及零部件价格;
时间: 2023-06-26 20:08:15 浏览: 91
基于java企业库存管理系统设计与实现.docx
以下是Java代码实现:
零部件类:
```java
public class Part {
private int partNumber;
private String partName;
private double partPrice;
public Part(int partNumber, String partName, double partPrice) {
this.partNumber = partNumber;
this.partName = partName;
this.partPrice = partPrice;
}
public int getPartNumber() {
return partNumber;
}
public void setPartNumber(int partNumber) {
this.partNumber = partNumber;
}
public String getPartName() {
return partName;
}
public void setPartName(String partName) {
this.partName = partName;
}
public double getPartPrice() {
return partPrice;
}
public void setPartPrice(double partPrice) {
this.partPrice = partPrice;
}
}
```
目录类:
```java
import java.util.ArrayList;
import java.util.List;
public class Catalog {
private List<Part> parts = new ArrayList<>();
public void addPart(Part part) {
parts.add(part);
}
public Part findPart(int partNumber) {
for (Part part : parts) {
if (part.getPartNumber() == partNumber) {
return part;
}
}
return null;
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) {
Catalog catalog = new Catalog();
Part part1 = new Part(1, "part1", 10.0);
Part part2 = new Part(2, "part2", 20.0);
Part part3 = new Part(3, "part3", 30.0);
catalog.addPart(part1);
catalog.addPart(part2);
catalog.addPart(part3);
Part foundPart = catalog.findPart(2);
if (foundPart != null) {
System.out.println("Part found: " + foundPart.getPartName());
} else {
System.out.println("Part not found");
}
}
}
```
输出结果:
```
Part found: part2
```
阅读全文