描述说明
采用了集合、泛型、反射等内容…
代码实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
class Device implements Comparable {
private int ID;
private String name;
private String brand;
private String type;
public Device() {
}
public Device(int id, String name, String brand, String type) {
this.ID = id;
this.name = name;
this.brand = brand;
this.type = type;
}
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 String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public int compareTo(Object obj) {
Device d = (Device) obj;
if (this.ID - d.getID() > 0) {
return 1;
}
if (this.ID - d.getID() == 0) {
return this.name.compareTo(d.getName());
}
return -1;
}
}
interface DeviceOperation {
int getCounts(); // 获取设备数量
void select(int deviceID); // 选择控制设备
void add(Device device); // 添加一个设备
void listAll(); // 查看所有设备
void search(int deviceID); // 根据设备ID查询设备
void search(String name); // 根据设备类型查询设备
void delete(int deviceID); // 根据设备ID删除记录
}
class AccomplishOperation implements DeviceOperation {
TreeSet<Device> devices = new TreeSet<>();
@Override
public int getCounts() {
return devices.size();
}
@Override
public void select(int deviceID) {
for (Device d : devices) {
if (d.getID() == deviceID) {
System.out.println("已选中型号为" + d.getType() + "的" + d.getBrand() + d.getName());
Class c = d.getClass();
Scanner br = new Scanner(System.in);
while (true) {
if (c == Fridge.class) {
Fridge fridge = (Fridge) d;
fridge.show();
System.out.print("请输入您的选择:");
int choice = br.nextInt();
if (choice == 0) {
break;
} else if (choice == 1) {
fridge.open();
} else if (choice == 2) {
fridge.close();
}
} else if (c == Micro.class) {
Micro micro = (Micro) d;
micro.show();
System.out.print("请输入您的选择:");
int choice = br.nextInt();
if (choice == 0) {
break;
} else if (choice == 1) {
micro.open();
} else if (choice == 2) {
micro.close();
}
} else if (c == Lamp.class) {
Lamp lamp = (Lamp) d;
lamp.show();
System.out.print("请输入您的选择:");
int choice = br.nextInt();
if (choice == 0) {
break;
} else if (choice == 1) {
lamp.open();
} else if (choice == 2) {
lamp.close();
}
} else if (c == Fan.class) {
Fan fan = (Fan) d;
fan.show();
System.out.print("请输入您的选择:");
int choice = br.nextInt();
if (choice == 0) {
break;
} else if (choice == 1) {
fan.open();
} else if (choice == 2) {
fan.close();
}
} else if (c == DoorLock.class) {
DoorLock doorLock = (DoorLock) d;
doorLock.show();
System.out.print("请输入您的选择:");
int choice = br.nextInt();
if (choice == 0) {
break;
} else if (choice == 1) {
doorLock.open();
} else if (choice == 2) {
doorLock.close();
}
}
}
break;
}
}
}
@Override
public void add(Device device) {
devices.add(device);
System.out.println("添加成功!");
}
@Override
public void listAll() {
if (devices.size() == 0) {
System.out.println("没有设备!");
} else {
for (Device d : devices) {
System.out.println("设备ID:" + d.getID() + ",类型:" + d.getName() + ",品牌:" + d.getBrand() + ",型号:" + d.getType());
}
}
}
@Override
public void search(int deviceID) {
int flag = 0;
for (Device d : devices) {
if (d.getID() == deviceID) {
flag = 1;
System.out.println("设备ID:" + d.getID() + ",类型:" + d.getName() + ",品牌:" + d.getBrand() + ",型号:" + d.getType());
break;
}
}
if (flag == 0)
System.out.println("输入的ID无效!");
}
@Override
public void search(String name) {
int flag = 0;
for (Device d : devices) {
if (d.getName().equals(name)) {
flag = 1;
System.out.println("设备ID:" + d.getID() + ",类型:" + d.getName() + ",品牌:" + d.getBrand() + ",型号:" + d.getType());
}
}
if (flag == 0)
System.out.println("不存在该种设备!");
}
@Override
public void delete(int deviceID) {
int flag = 0;
for (Device d : devices) {
if (d.getID() == deviceID) {
flag = 1;
devices.remove(d);
System.out.println("删除成功!");
break;
}
}
if (flag == 0)
System.out.println("不存在该种设备!");
}
}
interface EleUse {
void show();
void open();
void close();
}
class Fridge extends Device implements EleUse {
public Fridge(int id, String name, String brand, String type) {
super(id, name, brand, type);
}
@Override
public void show() {
System.out.println("******功能选择******");
System.out.println("1-打开冰箱 2-关闭冰箱 0-退出");
}
@Override
public void open() {
System.out.println("已打开冰箱");
}
@Override
public void close() {
System.out.println("已关闭冰箱");
}
}
class Micro extends Device implements EleUse {
public Micro(int id, String name, String brand, String type) {
super(id, name, brand, type);
}
@Override
public void show() {
System.out.println("******功能选择******");
System.out.println("1-启动微波炉 2-关闭微波炉 0-退出");
}
@Override
public void open() {
System.out.println("已打开微波炉");
}
@Override
public void close() {
System.out.println("已关闭微波炉");
}
}
class Lamp extends Device implements EleUse {
public Lamp(int id, String name, String brand, String type) {
super(id, name, brand, type);
}
@Override
public void show() {
System.out.println("******功能选择******");
System.out.println("1-打开电灯 2-关闭电灯 0-退出");
}
@Override
public void open() {
System.out.println("已打开灯");
}
@Override
public void close() {
System.out.println("已关灯");
}
}
class Fan extends Device implements EleUse {
public Fan(int id, String name, String brand, String type) {
super(id, name, brand, type);
}
@Override
public void show() {
System.out.println("******功能选择******");
System.out.println("1-打开风扇 2-关闭风扇 0-退出");
}
@Override
public void open() {
System.out.println("已打开风扇");
}
@Override
public void close() {
System.out.println("已关闭风扇");
}
}
class DoorLock extends Device implements EleUse {
public DoorLock(int id, String name, String brand, String type) {
super(id, name, brand, type);
}
@Override
public void show() {
System.out.println("******功能选择******");
System.out.println("1-打开门锁 2-上锁 0-退出");
}
@Override
public void open() {
System.out.println("已打开门锁");
}
@Override
public void close() {
System.out.println("已关闭门锁");
}
}
public class SmartHomeSys {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
AccomplishOperation accomplishOperation = new AccomplishOperation();
while (true) {
System.out.print("********智能家居控制系统**********\n");
System.out.print("1 选择设备\n");
System.out.print("2 添加设备\n");
System.out.print("3 根据设备ID查询设备\n");
System.out.print("4 根据设备类型查询设备\n");
System.out.print("5 根据设备ID删除记录\n");
System.out.print("0 退出系统\n\n");
accomplishOperation.listAll();
System.out.println("当前在线设备数量为:" + accomplishOperation.getCounts());
System.out.println();
System.out.print("请输入你的操作选择:");
int choice = Integer.parseInt(br.readLine());
if (choice == 0) {
System.exit(0);
} else if (choice == 1) {
System.out.print("请输入设备ID:");
int deviceID = Integer.parseInt(br.readLine());
accomplishOperation.select(deviceID);
} else if (choice == 2) {
System.out.print("********设备类型**********\n");
System.out.print("1 电冰箱\n");
System.out.print("2 微波炉\n");
System.out.print("3 智能电灯\n");
System.out.print("4 电风扇\n");
System.out.print("5 门锁\n");
System.out.print("请输入设备类型:");
int leixing = Integer.parseInt(br.readLine());
switch (leixing) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.print("请输入设备ID:");
int id = Integer.parseInt(br.readLine());
System.out.print("请输入设备品牌:");
String brand = br.readLine();
System.out.print("请输入设备型号:");
String type = br.readLine();
if (leixing == 1) {
Device fridge = new Fridge(id, "电冰箱", brand, type);
accomplishOperation.add(fridge);
} else if (leixing == 2) {
Device micro = new Micro(id, "微波炉", brand, type);
accomplishOperation.add(micro);
} else if (leixing == 3) {
Device lamp = new Lamp(id, "智能电灯", brand, type);
accomplishOperation.add(lamp);
} else if (leixing == 4) {
Device fan = new Fan(id, "电风扇", brand, type);
accomplishOperation.add(fan);
} else {
Device doorLock = new DoorLock(id, "门锁", brand, type);
accomplishOperation.add(doorLock);
}
break;
default:
System.out.println("请在可选择范围内选择!");
break;
}
} else if (choice == 3) {
System.out.print("请输入设备ID:");
int deviceID = Integer.parseInt(br.readLine());
accomplishOperation.search(deviceID);
} else if (choice == 4) {
System.out.print("请输入设备类型(电冰箱、微波炉、微波炉、智能电灯、风扇、门锁):");
String name = br.readLine();
accomplishOperation.search(name);
} else if (choice == 5) {
System.out.print("请输入设备ID:");
int deviceID = Integer.parseInt(br.readLine());
accomplishOperation.delete(deviceID);
} else {
System.out.print("请在可选择范围内选择!");
}
}
}
}