要求如下:题目四:实验设备管理系统设计
实验设备信息包括:设备编号,设备种类(如:微机、打印机、扫描仪等等),设备名称,设备价格,设备购入日期,是否报废,报废日期等。
主要功能:
1、能够完成对设备的录入和修改
2、对设备进行分类统计
3、设备的破损耗费和遗损处理
4、设备的查询
要求:存储数据方式不限,系统以菜单方式工作
非常感谢
这个实验设备管理系统需要用JAVA来实现,可以使用面向对象的编程思想。首先需要设计一个设备类,包含设备编号、设备种类、设备名称、设备价格、设备购入日期、是否报废、报废日期等属性。然后可以设计一个设备管理类,包含对设备的录入、查询、删除、修改等功能。具体的代码实现可以参考下面的示例:
设备类:
public class Device {
private String id; //设备编号
private String type; //设备种类
private String name; //设备名称
private double price; //设备价格
private Date purchaseDate; //设备购入日期
private boolean isScrapped; //是否报废
private Date scrapDate; //报废日期
//构造函数
public Device(String id, String type, String name, double price, Date purchaseDate, boolean isScrapped, Date scrapDate) {
this.id = id;
this.type = type;
this.name = name;
this.price = price;
this.purchaseDate = purchaseDate;
this.isScrapped = isScrapped;
this.scrapDate = scrapDate;
}
//getter和setter方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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;
}
public Date getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public boolean isScrapped() {
return isScrapped;
}
public void setScrapped(boolean scrapped) {
isScrapped = scrapped;
}
public Date getScrapDate() {
return scrapDate;
}
public void setScrapDate(Date scrapDate) {
this.scrapDate = scrapDate;
}
}
设备管理类:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DeviceManager {
private List<Device> deviceList; //设备列表
//构造函数
public DeviceManager() {
deviceList = new ArrayList<>();
}
//添加设备
public void addDevice(Device device) {
deviceList.add(device);
}
//删除设备
public void deleteDevice(String id) {
for (Device device : deviceList) {
if (device.getId().equals(id)) {
deviceList.remove(device);
break;
}
}
}
//修改设备信息
public void modifyDevice(String id, String type, String name, double price, Date purchaseDate, boolean isScrapped, Date scrapDate) {
for (Device device : deviceList) {
if (device.getId().equals(id)) {
device.setType(type);
device.setName(name);
device.setPrice(price);
device.setPurchaseDate(purchaseDate);
device.setScrapped(isScrapped);
device.setScrapDate(scrapDate);
break;
}
}
}
//查询设备
public List<Device> queryDevice(String type) {
List<Device> resultList = new ArrayList<>();
for (Device device : deviceList) {
if (device.getType().equals(type)) {
resultList.add(device);
}
}
return resultList;
}
}
使用示例:
import java.util.Date;
import java.util.List;
public class Main {
public static void main(String[] args) {
//创建设备管理对象
DeviceManager deviceManager = new DeviceManager();
//添加设备
Device device1 = new Device("001", "微机", "联想电脑", 3999.0, new Date(), false, null);
Device device2 = new Device("002", "打印机", "惠普打印机", 999.0, new Date(), false, null);
deviceManager.addDevice(device1);
deviceManager.addDevice(device2);
//查询设备
List<Device> result = deviceManager.queryDevice("微机");
for (Device device : result) {
System.out.println(device.getName());
}
//修改设备信息
deviceManager.modifyDevice("002", "打印机", "佳能打印机", 899.0, new Date(), false, null);
//删除设备
deviceManager.deleteDevice("001");
}
}
建议使用文件进行存储,使用数据库的还得安装数据库,建表什么的。java实现的实验室设备管理系统程序如下:
DeviceInfo 设备信息类:
/**
* 设备信息类
* @author Administrator
*
*/
public class DeviceInfo {
private String id; //设备编号
private String type; //设备种类
private String name; //设备名称
private double price; //设备价格
private String purchaseDate; //设备购入日期
private boolean isScrapped; //是否报废
private String scrapDate; //报废日期
//构造函数
public DeviceInfo() {};
public DeviceInfo(String id, String type, String name, double price, String purchaseDate, boolean isScrapped, String scrapDate) {
this.id = id;
this.type = type;
this.name = name;
this.price = price;
this.purchaseDate = purchaseDate;
this.isScrapped = isScrapped;
this.scrapDate = scrapDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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;
}
public String getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(String purchaseDate) {
this.purchaseDate = purchaseDate;
}
public boolean isScrapped() {
return isScrapped;
}
public void setScrapped(boolean scrapped) {
isScrapped = scrapped;
}
public String getScrapDate() {
return scrapDate;
}
public void setScrapDate(String scrapDate) {
this.scrapDate = scrapDate;
}
}
新建设备管理类:
import java.io.BufferedWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 管理设备类
* @author Administrator
*
*/
public class ManagerDeviceInfo {
private List<DeviceInfo> deviceList; //目前的设备信息
private boolean stored = false;//当前的设备信息是否需要重新保存更新
private String dataFilePath = "D:\\device_info.txt";//默认的存储设备数据文件
//构造函数
public ManagerDeviceInfo() {
deviceList = new ArrayList<DeviceInfo>();
loadDeviceInfo();
}
//添加设备信息
public boolean addDeviceInfo(DeviceInfo device) {
deviceList.add(device);
stored = true;
return true;
}
//修改设备信息
public boolean modifyDeviceInfo(DeviceInfo device) {
boolean success = false;
for (int i = 0;i<deviceList.size();i++) {
DeviceInfo d = deviceList.get(i);
if (d.getId().equals(device.getId())) {
d.setType(device.getType());
d.setName(device.getName());
d.setPrice(device.getPrice());
d.setPurchaseDate(device.getPurchaseDate());
d.setScrapped(device.isScrapped());
d.setScrapDate(device.getScrapDate());
deviceList.set(i, d);
stored = true;
success = true;
break;
}
}
return success;
}
//根据设备的任意属性查询设备:支持模糊查询
public List<DeviceInfo> searchDeviceInfo(String key) {
List<DeviceInfo> resultList = new ArrayList<DeviceInfo>();
for (DeviceInfo d : deviceList) {
if (d.getName().contains(key)||d.getId().contains(key)
||String.valueOf(d.getPrice()).contains(String.valueOf(key))
||d.getPurchaseDate().contains(key)
||d.getScrapDate().contains(key)
||d.isScrapped()== (key=="是"?true:false)
||d.getType().contains(key)) {
resultList.add(d);
}
}
return resultList;
}
//根据设备的id查询设备
public DeviceInfo searchDeviceInfoById(String id) {
for (DeviceInfo d : deviceList) {
if (d.getId().equals(id)){
return d;
}
}
return null;
}
//设备的破损耗费统计
public double getScrappedMoney() {
double money = 0;
for (DeviceInfo d : deviceList) {
if (d.isScrapped()) {
money+=d.getPrice();
}
}
return money;
}
//根据设备类型进行分类统计数量
public int getCountByType(String type) {
int count = 0;
for (DeviceInfo d : deviceList) {
if (d.getType().equals(type)) {
count+=1;
}
}
return count;
}
//分类统计所有类别的设备数量
public Map<String,Integer> getCountByAllType() {
Map<String,Integer> countsMap = new HashMap();
for (DeviceInfo d : deviceList) {
countsMap.put(d.getType(), countsMap.getOrDefault(d.getType(), 0)+1);
}
return countsMap;
}
//从数据文件中加载设备信息
public boolean loadDeviceInfo() {
boolean loaded = false;
try {
Path path = Paths.get(dataFilePath);
List<String> lines = Files.readAllLines(path);
if(lines!=null && !lines.isEmpty()) {
for(String line:lines) {
String[] str_info = line.split(",");
String id = str_info[0];
String type = str_info[1];
String name = str_info[2];
double price = Double.parseDouble(str_info[3]);
String purchaseDate = str_info[4];
boolean isScrapped = Boolean.parseBoolean(str_info[5]);
String scrapDate = str_info[6];
DeviceInfo deviceInfo = new DeviceInfo(id, type, name, price, purchaseDate, isScrapped, scrapDate);
this.deviceList.add(deviceInfo);
}
}
loaded = true;
} catch (Exception e) {
// TODO: handle exception
}
return loaded;
}
//将设备信息更新到数据文件中去
public boolean storeDeviceInfo() {
boolean store = true;
try {
if (this.stored&&this.deviceList.size()>0) {
Path path = Paths.get(dataFilePath);
// 使用newBufferedWriter创建文件并写文件
// 这里使用了try-with-resources方法来关闭流,不用手动关闭
try (BufferedWriter writer =
Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
for(DeviceInfo deviceInfo:deviceList) {
String line =deviceInfo.getId()+","+deviceInfo.getType()+","+deviceInfo.getName()+","+deviceInfo.getPrice()+
","+deviceInfo.getPurchaseDate()+","+deviceInfo.isScrapped()+","+deviceInfo.getScrapDate();
writer.write(line);
writer.write("\n");
}
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
store = false;
}
return store;
}
//输出设备信息
public void printDeviceInfo(DeviceInfo deviceInfo) {
System.out.println("该设备信息如下:");
System.out.println("设备编号:"+deviceInfo.getId());
System.out.println("设备类型:"+deviceInfo.getType());
System.out.println("设备名称:"+deviceInfo.getName());
System.out.println("设备价格:"+deviceInfo.getPrice());
System.out.println("设备购入日期:"+deviceInfo.getPurchaseDate());
System.out.println("是否报废:"+(deviceInfo.isScrapped()?"是":"否"));
System.out.println("废日期:"+deviceInfo.getId());
}
}
在主程序中实现菜单和功能选择:
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.BiConsumer;
public class Test {
public static void main(String[] args) {
int select = 1;
Scanner scanner = new Scanner(System.in);
ManagerDeviceInfo managerDeviceInfo= new ManagerDeviceInfo();
while (select != 0) {
System.out.println("**********欢迎使用实验设备管理系统**********");
System.out.println("1 录入设备信息");
System.out.println("2 修改设备信息");
System.out.println("3 设备分类统计");
System.out.println("4 设备破损和遗失处理");
System.out.println("5 设备查询");
System.out.println("6 退出系统");
System.out.print("请选择需要使用的功能:");
select = Integer.parseInt(scanner.nextLine().trim());
// 选择执行不同的功能
switch (select) {
case 1://录入设备信息
System.out.println("请按次序输入设备的信息,英文逗号隔开(设备编号,设备种类,设备名称,设备价格,设备购入日期,是否报废,报废日期):");
String line = scanner.nextLine().trim();
String[] device_info = line.split(",");
try {
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setId(device_info[0]);
deviceInfo.setType(device_info[1]);
deviceInfo.setName(device_info[2]);
deviceInfo.setPrice(Double.parseDouble(device_info[3]));
deviceInfo.setPurchaseDate(device_info[4]);
deviceInfo.setScrapped(device_info[5].equals("是"));
deviceInfo.setScrapDate(device_info[6]);
DeviceInfo infoList = managerDeviceInfo.searchDeviceInfoById(device_info[0]);
if(infoList!=null) {
System.out.println("该设备编号已存在,请检查您输入的设备编号是否有误!");
}else {
managerDeviceInfo.addDeviceInfo(deviceInfo);
}
}catch (Exception e) {
// TODO: handle exception
System.out.println("设备信息录入失败,数据格式输入有误!");
}
break;
case 2://修改设备信息
System.out.println("请输入要修改的设备编号:");
String id = scanner.nextLine().trim();
DeviceInfo deviceInfo = managerDeviceInfo.searchDeviceInfoById(id);
if(deviceInfo==null) {
System.out.println("该设备不存在,请检查您输入的设备编号是否有误!");
}else {
managerDeviceInfo.printDeviceInfo(deviceInfo);
System.out.println("按次序输入修改后的信息,英文逗号隔开(设备种类,设备名称,设备价格,设备购入日期,是否报废,报废日期):");
line = scanner.nextLine().trim();
device_info = line.split(",");
try {
deviceInfo.setType(device_info[0]);
deviceInfo.setName(device_info[1]);
deviceInfo.setPrice(Double.parseDouble(device_info[2]));
deviceInfo.setPurchaseDate(device_info[3]);
deviceInfo.setScrapped(device_info[4].equals("是"));
deviceInfo.setScrapDate(device_info[5]);
managerDeviceInfo.modifyDeviceInfo(deviceInfo);
}catch (Exception e) {
// TODO: handle exception
System.out.println("设备信息修改失败,数据格式输入有误!");
}
}
break;
case 3://设备分类统计
System.out.println("请输入要统计的类别名称(若要统计全部类别则不输入):");
String type = scanner.nextLine().trim();
if (type.equals("")){
Map<String, Integer> mapCount = managerDeviceInfo.getCountByAllType();
if (!mapCount.isEmpty()) {
System.out.println("当前系统中设备类别统计如下:");
Iterator<String> types = mapCount.keySet().iterator();
while(types.hasNext()) {
String t = types.next();
System.out.println(t+":"+mapCount.get(t));
}
}else {
System.out.println("当前系统中没有任何设备信息,请先录入设备信息!");
}
}else {
int count = managerDeviceInfo.getCountByType(type);
System.out.println(type+":"+count);
}
break;
case 4://设备破损和遗失处理
double money = managerDeviceInfo.getScrappedMoney();
System.out.println("当前报废的设备金额为:"+money);
break;
case 5://设备查询
System.out.println("请输入要查询的设备属性(可以是编号、类型、名称等任意属性,支持模糊查询):");
line = scanner.nextLine().trim();
List<DeviceInfo> result = managerDeviceInfo.searchDeviceInfo(line);
if(result.isEmpty()) {
System.out.println("抱歉,没有查询到符合条件的设备信息!");
}else {
System.out.println("有查询到符合条件的设备信息如下:");
for(DeviceInfo dInfo:result) {
managerDeviceInfo.printDeviceInfo(dInfo);
System.out.println();
}
}
break;
case 6:
//退出系统前进行数据保存
managerDeviceInfo.storeDeviceInfo();
System.out.println("感谢使用本系统!");
select = 0;
break;
default:
System.out.println("请输入正确的功能选项!");
break;
}
}
}
}
运行效果:
Java Swing 课程设计 ---- 实验室设备管理系统
可以借鉴下,非常详细
https://blog.csdn.net/rain67/article/details/125278016
【课程设计】2小时学会JavaSwing课程设计-万能模板-图书管理系统-[你的课程我设计]
这里有个万能模板,你直接套就行了