请用JAVA写一个设备管理系统
时间: 2024-05-02 16:17:27 浏览: 17
好的,下面是一个简单的设备管理系统的JAVA代码。该系统可以添加、删除、修改和查询设备信息。
```java
import java.util.ArrayList;
public class DeviceManagementSystem {
private ArrayList<Device> devices;
public DeviceManagementSystem() {
devices = new ArrayList<>();
}
public void addDevice(Device device) {
devices.add(device);
}
public void deleteDevice(Device device) {
devices.remove(device);
}
public void updateDevice(Device oldDevice, Device newDevice) {
int index = devices.indexOf(oldDevice);
devices.set(index, newDevice);
}
public Device getDeviceById(int id) {
for (Device device : devices) {
if (device.getId() == id) {
return device;
}
}
return null;
}
public ArrayList<Device> getAllDevices() {
return devices;
}
}
public class Device {
private int id;
private String name;
private String type;
private String location;
public Device(int id, String name, String type, String location) {
this.id = id;
this.name = name;
this.type = type;
this.location = location;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getLocation() {
return location;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setLocation(String location) {
this.location = location;
}
}
```
使用方法:
```java
public class TestDeviceManagementSystem {
public static void main(String[] args) {
DeviceManagementSystem system = new DeviceManagementSystem();
// 添加设备
system.addDevice(new Device(1, "电脑", "IT设备", "办公室A"));
system.addDevice(new Device(2, "打印机", "办公设备", "办公室B"));
// 查询设备
Device device = system.getDeviceById(1);
System.out.println(device.getName()); // 输出:电脑
// 修改设备
device.setName("笔记本电脑");
system.updateDevice(new Device(1, "", "", ""), device);
// 删除设备
system.deleteDevice(device);
// 获取所有设备
ArrayList<Device> devices = system.getAllDevices();
for (Device d : devices) {
System.out.println(d.getName());
}
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)