You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Car/MaintenanceSystem.java

244 lines
7.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.automobile.service;
import com.automobile.model.Car;
import com.automobile.parts.Engine;
import com.automobile.parts.Piston;
import com.automobile.parts.SteeringWheel;
import com.automobile.parts.Seat;
import com.automobile.parts.Transmission;
import com.automobile.parts.Wheel;
import com.automobile.system.ElectronicSystem;
import com.automobile.system.ElectronicControlUnit;
import java.util.ArrayList;
import java.util.List;
/**
* 汽车维修系统类
* 展示组合关系中部件的更换和维修
*/
public class MaintenanceSystem {
private String garageName;
private List<ServiceRecord> serviceHistory;
/**
* 构造方法
* @param garageName 修理厂名称
*/
public MaintenanceSystem(String garageName) {
this.garageName = garageName;
this.serviceHistory = new ArrayList<>();
}
/**
* 执行常规保养
* @param car 要保养的汽车
*/
public void performRegularMaintenance(Car car) {
System.out.println("\n===== 开始常规保养 =====");
System.out.println("车辆: " + car.getBrand() + " " + car.getModel());
// 检查和更换机油
System.out.println("更换引擎机油...");
// 检查轮胎
System.out.println("检查轮胎...");
for (int i = 0; i < car.getWheels().size(); i++) {
Wheel wheel = car.getWheels().get(i);
wheel.checkStatus();
wheel.adjustPressure(2.5); // 调整到标准胎压
}
// 检查电子系统
System.out.println("执行电子系统诊断...");
car.getElectronicSystem().runDiagnostics();
// 记录保养
ServiceRecord record = new ServiceRecord(
car.getVin(),
"常规保养",
"更换机油、检查轮胎、电子系统诊断"
);
serviceHistory.add(record);
System.out.println("===== 常规保养完成 =====\n");
}
/**
* 更换方向盘
* @param car 汽车
* @param newType 新方向盘类型
*/
public void replaceSteeringWheel(Car car, String newType) {
System.out.println("\n===== 更换方向盘 =====");
System.out.println("当前方向盘: " + car.getSteeringWheel().getType());
// 在实际应用中,这里应该创建一个新的方向盘对象并替换
// 由于Java的封装性我们通过setter方法来修改
car.getSteeringWheel().setType(newType);
System.out.println("新方向盘: " + car.getSteeringWheel().getType());
// 记录维修
ServiceRecord record = new ServiceRecord(
car.getVin(),
"更换方向盘",
"更换为: " + newType
);
serviceHistory.add(record);
System.out.println("===== 方向盘更换完成 =====\n");
}
/**
* 更换座椅
* @param car 汽车
* @param seatIndex 要更换的座椅索引
* @param newMaterial 新材料
* @param heated 是否加热
* @param ventilated 是否通风
*/
public void replaceSeat(Car car, int seatIndex, String newMaterial, boolean heated, boolean ventilated) {
if (seatIndex >= 0 && seatIndex < car.getSeats().size()) {
System.out.println("\n===== 更换座椅 =====");
Seat oldSeat = car.getSeats().get(seatIndex);
System.out.println("当前座椅: " + oldSeat.getType() + ", " + oldSeat.getMaterial());
// 创建新座椅并替换
Seat newSeat = new Seat(oldSeat.getType());
newSeat.setMaterial(newMaterial);
newSeat.setHeated(heated);
newSeat.setVentilated(ventilated);
// 替换座椅
car.getSeats().set(seatIndex, newSeat);
System.out.println("新座椅: " + newSeat.getType() + ", " + newSeat.getMaterial() +
", 加热: " + heated + ", 通风: " + ventilated);
// 记录维修
ServiceRecord record = new ServiceRecord(
car.getVin(),
"更换座椅",
"座椅类型: " + oldSeat.getType() + ", 新材料: " + newMaterial
);
serviceHistory.add(record);
System.out.println("===== 座椅更换完成 =====\n");
} else {
System.out.println("无效的座椅索引");
}
}
/**
* 维修引擎
* @param car 汽车
* @param issue 问题描述
*/
public void repairEngine(Car car, String issue) {
System.out.println("\n===== 维修引擎 =====");
System.out.println("引擎类型: " + car.getEngine().getType());
System.out.println("问题: " + issue);
// 检查活塞状态
System.out.println("检查活塞...");
for (Piston piston : car.getEngine().getPistons()) {
System.out.println(piston.getStatus());
}
// 模拟修复过程
System.out.println("执行引擎修复...");
System.out.println("修复完成");
// 记录维修
ServiceRecord record = new ServiceRecord(
car.getVin(),
"引擎维修",
issue
);
serviceHistory.add(record);
System.out.println("===== 引擎维修完成 =====\n");
}
/**
* 重置电子控制单元
* @param car 汽车
* @param ecuType ECU类型
*/
public void resetECU(Car car, String ecuType) {
System.out.println("\n===== 重置ECU =====");
for (ElectronicControlUnit ecu : car.getElectronicSystem().getEcus()) {
if (ecu.getType().contains(ecuType)) {
System.out.println("重置 " + ecu.getAbbreviation() + "...");
ecu.shutdown();
ecu.initialize();
ecu.clearFault();
System.out.println("重置完成");
// 记录维修
ServiceRecord record = new ServiceRecord(
car.getVin(),
"重置ECU",
"ECU类型: " + ecu.getType()
);
serviceHistory.add(record);
System.out.println("===== ECU重置完成 =====\n");
return;
}
}
System.out.println("未找到类型为 " + ecuType + " 的ECU");
}
/**
* 显示维修历史
* @param vin 车辆识别号
*/
public void showServiceHistory(String vin) {
System.out.println("\n===== 维修历史 (" + vin + ") =====");
boolean found = false;
for (ServiceRecord record : serviceHistory) {
if (record.getVin().equals(vin)) {
System.out.println(record.toString());
found = true;
}
}
if (!found) {
System.out.println("未找到该车辆的维修记录");
}
System.out.println("========================\n");
}
/**
* 内部类:维修记录
*/
private static class ServiceRecord {
private String vin;
private String serviceType;
private String description;
private long timestamp;
public ServiceRecord(String vin, String serviceType, String description) {
this.vin = vin;
this.serviceType = serviceType;
this.description = description;
this.timestamp = System.currentTimeMillis();
}
public String getVin() {
return vin;
}
@Override
public String toString() {
return "时间: " + new java.util.Date(timestamp) +
", 类型: " + serviceType + ", 描述: " + description;
}
}
}