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.

199 lines
5.5 KiB

package com.car.model;
import java.util.ArrayList;
import java.util.List;
/**
* 汽车类 - 主类,包含多个组件的组合关系
*/
public class Car {
private String brand;
private String model;
private String year;
// 组合关系组件
private SteeringWheel steeringWheel; // 方向盘
private List<Seat> seats; // 座椅列表
private Engine engine; // 引擎
private Transmission transmission; // 变速箱
private ElectronicSystem electronicSystem; // 电子系统
private List<Wheel> wheels; // 车轮列表
/**
* 构造方法
* @param brand 品牌
* @param model 型号
* @param year 年份
*/
public Car(String brand, String model, String year) {
this.brand = brand;
this.model = model;
this.year = year;
// 初始化基础组件
this.steeringWheel = new SteeringWheel("Leather", "380mm", true);
this.seats = new ArrayList<>();
this.engine = new Engine("V6", 300, 3.5, 6);
this.transmission = new Transmission("Automatic", 8, true);
this.electronicSystem = new ElectronicSystem("v1.0");
this.wheels = new ArrayList<>();
// 初始化座椅
seats.add(new Seat("Driver", "Leather", true, true));
seats.add(new Seat("Passenger", "Leather", true, true));
// 初始化车轮
for (int i = 0; i < 4; i++) {
wheels.add(new Wheel("18 inch", "All-season", 32));
}
}
/**
* 启动汽车
*/
public void start() {
System.out.println(brand + " " + model + " starting...");
System.out.println("Engine started: " + engine.getType());
System.out.println("Electronic systems initialized.");
}
/**
* 停止汽车
*/
public void stop() {
System.out.println(brand + " " + model + " stopping...");
System.out.println("Engine stopped.");
}
/**
* 更换方向盘
* @param newSteeringWheel 新的方向盘
*/
public void replaceSteeringWheel(SteeringWheel newSteeringWheel) {
this.steeringWheel = newSteeringWheel;
System.out.println("Steering wheel replaced.");
}
/**
* 更换座椅
* @param index 座椅索引
* @param newSeat 新的座椅
* @return 是否更换成功
*/
public boolean replaceSeat(int index, Seat newSeat) {
if (index >= 0 && index < seats.size()) {
seats.set(index, newSeat);
System.out.println("Seat at position " + index + " replaced.");
return true;
}
return false;
}
/**
* 更换车轮
* @param index 车轮索引
* @param newWheel 新的车轮
* @return 是否更换成功
*/
public boolean replaceWheel(int index, Wheel newWheel) {
if (index >= 0 && index < wheels.size()) {
wheels.set(index, newWheel);
System.out.println("Wheel at position " + index + " replaced.");
return true;
}
return false;
}
/**
* 运行汽车诊断
* @return 诊断结果
*/
public boolean runDiagnostics() {
System.out.println("Running diagnostics for " + brand + " " + model + "...");
boolean engineOk = true; // 简化处理
boolean electronicOk = electronicSystem.runDiagnostics();
// 检查轮胎气压
boolean wheelsOk = true;
for (int i = 0; i < wheels.size(); i++) {
if (!wheels.get(i).checkPressure()) {
System.out.println("Wheel " + i + " pressure issue detected!");
wheelsOk = false;
}
}
boolean overallStatus = engineOk && electronicOk && wheelsOk;
System.out.println("Diagnostics completed. Overall status: " + (overallStatus ? "OK" : "Issues detected"));
return overallStatus;
}
// getter and setter methods
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public SteeringWheel getSteeringWheel() {
return steeringWheel;
}
public void setSteeringWheel(SteeringWheel steeringWheel) {
this.steeringWheel = steeringWheel;
}
public List<Seat> getSeats() {
return new ArrayList<>(seats);
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public Transmission getTransmission() {
return transmission;
}
public void setTransmission(Transmission transmission) {
this.transmission = transmission;
}
public ElectronicSystem getElectronicSystem() {
return electronicSystem;
}
public void setElectronicSystem(ElectronicSystem electronicSystem) {
this.electronicSystem = electronicSystem;
}
public List<Wheel> getWheels() {
return new ArrayList<>(wheels);
}
@Override
public String toString() {
return "Car [brand=" + brand + ", model=" + model + ", year=" + year + ", engine=" + engine + ", transmission=" + transmission + "]";
}
}