Java代码实现以下功能(1) 定义零部件类, (2) 增加目录类,属性包括: 零部件编号、零部件名称及零部件价格;
时间: 2023-09-05 07:05:25 浏览: 41
### 回答1:
以下是 Java 代码实现:
```java
// 零部件类
public class Component {
private int id; // 零部件编号
private String name; // 零部件名称
private double price; // 零部件价格
public Component(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
}
// 目录类
public class Catalog {
private ArrayList<Component> components; // 零部件列表
public Catalog() {
this.components = new ArrayList<>();
}
public void addComponent(Component component) {
components.add(component);
}
public ArrayList<Component> getComponents() {
return components;
}
public Component getComponentById(int id) {
for (Component component : components) {
if (component.getId() == id) {
return component;
}
}
return null;
}
}
```
在上面的代码中,我们定义了一个 `Component` 类来表示零部件,其包含编号、名称和价格三个属性。然后,我们定义了一个 `Catalog` 类来表示零部件的目录,其包含一个零部件列表,可以添加零部件并通过编号查找零部件。
### 回答2:
Java代码实现以下功能:
1. 首先,我们需要定义一个零部件类,代码如下:
```java
public class Component {
private String id;
private String name;
private double price;
public Component(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
// Getters and setters for id, name, and price
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
2. 接下来,我们需要增加一个目录类,该类包含零部件的编号、名称和价格。代码如下:
```java
import java.util.ArrayList;
import java.util.List;
public class Catalog {
private List<Component> components;
public Catalog() {
components = new ArrayList<>();
}
public void addComponent(Component component) {
components.add(component);
}
public void removeComponent(Component component) {
components.remove(component);
}
public List<Component> getComponents() {
return components;
}
}
```
使用以上代码,我们可以创建一个零部件对象,并将其添加到目录中。示例代码如下:
```java
public class Main {
public static void main(String[] args) {
Catalog catalog = new Catalog();
// 创建零部件对象
Component component1 = new Component("001", "零部件1", 10.99);
Component component2 = new Component("002", "零部件2", 15.99);
// 将零部件添加到目录中
catalog.addComponent(component1);
catalog.addComponent(component2);
// 获取目录中所有的零部件
List<Component> components = catalog.getComponents();
// 打印每个零部件的编号、名称和价格
for (Component component : components) {
System.out.println("零部件编号:" + component.getId());
System.out.println("零部件名称:" + component.getName());
System.out.println("零部件价格:" + component.getPrice());
System.out.println();
}
}
}
```
以上代码实现了定义一个零部件类以及增加一个目录类,并可以添加零部件到目录中,并显示每个零部件的编号、名称和价格。
### 回答3:
Java代码实现以下功能:
(1) 定义零部件类:
```java
public class Component {
private int id; // 零部件编号
private String name; // 零部件名称
private double price; // 零部件价格
// 构造方法
public Component(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
// 获取零部件编号
public int getId() {
return id;
}
// 设置零部件编号
public void setId(int id) {
this.id = id;
}
// 获取零部件名称
public String getName() {
return name;
}
// 设置零部件名称
public void setName(String name) {
this.name = name;
}
// 获取零部件价格
public double getPrice() {
return price;
}
// 设置零部件价格
public void setPrice(double price) {
this.price = price;
}
}
```
(2) 增加目录类,属性包括:零部件编号、零部件名称及零部件价格:
```java
import java.util.ArrayList;
import java.util.List;
public class Catalog {
private List<Component> components;
// 构造方法
public Catalog() {
components = new ArrayList<>();
}
// 增加零部件
public void addComponent(Component component) {
components.add(component);
}
// 获取全部零部件
public List<Component> getAllComponents() {
return components;
}
// 获取零部件数量
public int getComponentCount() {
return components.size();
}
}
```
以上代码实现了零部件类和目录类。零部件类具有零部件编号、零部件名称和零部件价格三个属性,并提供了相应的 getter 和 setter 方法。目录类通过 ArrayList 维护了一组零部件,可以通过 addComponent 方法添加零部件,通过 getAllComponents 方法获取全部零部件,通过 getComponentCount 方法获取零部件数量。