marrrolom / fin

0 stars 0 forks source link

Documentation #1

Open JhonKunuga opened 1 month ago

JhonKunuga commented 1 month ago

To tackle the task of creating a smart home automation system in Java, we'll break down the requirements step by step and provide the necessary code snippets for each part. Let's start with the class definitions and then move on to implementing the HashMap storage, iterators, sorting, and Swing GUI. : Step 1: Define the Device Class and its Subclasses // Device.java public class Device { private String deviceId; private String deviceName; private boolean isOn;

public Device(String deviceId, String deviceName) {
    this.deviceId = deviceId;
    this.deviceName = deviceName;
    this.isOn = false; // By default, devices start off
}

// Getters and setters
public String getDeviceId() {
    return deviceId;
}

public void setDeviceId(String deviceId) {
    this.deviceId = deviceId;
}

public String getDeviceName() {
    return deviceName;
}

public void setDeviceName(String deviceName) {
    this.deviceName = deviceName;
}

public boolean isOn() {
    return isOn;
}

public void setOn(boolean on) {
    isOn = on;
}

// Override toString method
@Override
public String toString() {
    return "Device{" +
            "deviceId='" + deviceId + '\'' +
            ", deviceName='" + deviceName + '\'' +
            ", isOn=" + isOn +
            '}';
}

}

Now, let's create subclasses for specific devices (Light, Thermostat, SecurityCamera) that inherit from Device and add their unique attributes:

// Light.java public class Light extends Device { private int brightness;

public Light(String deviceId, String deviceName, int brightness) {
    super(deviceId, deviceName);
    this.brightness = brightness;
}

public int getBrightness() {
    return brightness;
}

public void setBrightness(int brightness) {
    this.brightness = brightness;
}

// Override toString method
@Override
public String toString() {
    return "Light{" +
            "deviceId='" + getDeviceId() + '\'' +
            ", deviceName='" + getDeviceName() + '\'' +
            ", isOn=" + isOn() +
            ", brightness=" + brightness +
            '}';
}

}

// Thermostat.java public class Thermostat extends Device { private int temperature;

public Thermostat(String deviceId, String deviceName, int temperature) {
    super(deviceId, deviceName);
    this.temperature = temperature;
}

public int getTemperature() {
    return temperature;
}

public void setTemperature(int temperature) {
    this.temperature = temperature;
}

// Override toString method
@Override
public String toString() {
    return "Thermostat{" +
            "deviceId='" + getDeviceId() + '\'' +
            ", deviceName='" + getDeviceName() + '\'' +
            ", isOn=" + isOn() +
            ", temperature=" + temperature +
            '}';
}

}

// SecurityCamera.java public class SecurityCamera extends Device { private String resolution;

public SecurityCamera(String deviceId, String deviceName, String resolution) {
    super(deviceId, deviceName);
    this.resolution = resolution;
}

public String getResolution() {
    return resolution;
}

public void setResolution(String resolution) {
    this.resolution = resolution;
}

// Override toString method
@Override
public String toString() {
    return "SecurityCamera{" +
            "deviceId='" + getDeviceId() + '\'' +
            ", deviceName='" + getDeviceName() + '\'' +
            ", isOn=" + isOn() +
            ", resolution='" + resolution + '\'' +
            '}';
}

}

Step 2: Create Abstract Class SmartDevice

// SmartDevice.java public abstract class SmartDevice { // Add common functionality or attributes if needed for all smart devices } Step 3: Implement SmartHomeSystem to Manage Devices import java.util.HashMap; import java.util.Iterator; import java.util.Map;

public class SmartHomeSystem { private HashMap<String, Device> devices;

public SmartHomeSystem() {
    this.devices = new HashMap<>();
}

public void addDevice(Device device) {
    devices.put(device.getDeviceId(), device);
}

public void removeDevice(String deviceId) {
    devices.remove(deviceId);
}

public Device getDevice(String deviceId) {
    return devices.get(deviceId);
}

public void listDevices() {
    for (Device device : devices.values()) {
        System.out.println(device);
    }
}

// Inner class DeviceIterator implementing Iterator<Device>
public class DeviceIterator implements Iterator<Device> {
    private Iterator<Map.Entry<String, Device>> iterator;

    public DeviceIterator() {
        this.iterator = devices.entrySet().iterator();
    }

    @Override
    public boolean hasNext() {
        return iterator.hasNext();
    }

    @Override
    public Device next() {
        return iterator.next().getValue();
    }

    @Override
    public void remove() {
        iterator.remove();
    }
}

// SortByName class implementing Comparator<Device>
public static class SortByName implements Comparator<Device> {
    @Override
    public int compare(Device d1, Device d2) {
        return d1.getDeviceName().compareTo(d2.getDeviceName());
    }
}

}

Step 4: Create Swing GUI for Smart Home System import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

public class SmartHomeGUI extends JFrame { private SmartHomeSystem smartHomeSystem; private JTextArea deviceListArea;

public SmartHomeGUI() {
    smartHomeSystem = new SmartHomeSystem();

    setTitle("Smart Home Automation System");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(600, 400);
    setLayout(new BorderLayout());

    JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    JButton addDeviceButton = new JButton("Add Device");
    addDeviceButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Implement adding device functionality
            // Example: showInputDialog and add to smartHomeSystem
        }
    });

    JButton listDevicesButton = new JButton("List Devices");
    listDevicesButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            listDevices();
        }
    });

    controlPanel.add(addDeviceButton);
    controlPanel.add(listDevicesButton);

    deviceListArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(deviceListArea);

    add(controlPanel, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);

    setVisible(true);
}

private void listDevices() {
    deviceListArea.setText("");
    for (Device device : smartHomeSystem.devices.values()) {
        deviceListArea.append(device.toString() + "\n");
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new SmartHomeGUI();
        }
    });
}

}

Explanation: Device Class and Subclasses: Encapsulate attributes with private fields and provide public getters and setters. Each subclass (Light, Thermostat, SecurityCamera) inherits from Device and adds specific attributes. SmartHomeSystem Class: Manages devices using a HashMap<String, Device>, providing methods to add, remove, get, list devices, and an inner DeviceIterator class to iterate over devices. SmartHomeGUI: Implements a basic Swing GUI with buttons to add devices, list devices, and displays the list of devices in a JTextArea.

JhonKunuga commented 1 month ago

Here is Full Documentation Code IN 1 FILE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

import javax.swing.; import javax.swing.border.EmptyBorder; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.*;

public class SmartHomeAutomation {

// Device class
public static class Device {
    private String deviceId;
    private String deviceName;
    private boolean isOn;

    public Device(String deviceId, String deviceName) {
        this.deviceId = deviceId;
        this.deviceName = deviceName;
        this.isOn = false; // By default, devices start off
    }

    // Getters and setters
    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public boolean isOn() {
        return isOn;
    }

    public void setOn(boolean on) {
        isOn = on;
    }

    // Override toString method
    @Override
    public String toString() {
        return "Device{" +
                "deviceId='" + deviceId + '\'' +
                ", deviceName='" + deviceName + '\'' +
                ", isOn=" + isOn +
                '}';
    }
}

// Light subclass
public static class Light extends Device {
    private int brightness;

    public Light(String deviceId, String deviceName, int brightness) {
        super(deviceId, deviceName);
        this.brightness = brightness;
    }

    public int getBrightness() {
        return brightness;
    }

    public void setBrightness(int brightness) {
        this.brightness = brightness;
    }

    // Override toString method
    @Override
    public String toString() {
        return "Light{" +
                "deviceId='" + getDeviceId() + '\'' +
                ", deviceName='" + getDeviceName() + '\'' +
                ", isOn=" + isOn() +
                ", brightness=" + brightness +
                '}';
    }
}

// Thermostat subclass
public static class Thermostat extends Device {
    private int temperature;

    public Thermostat(String deviceId, String deviceName, int temperature) {
        super(deviceId, deviceName);
        this.temperature = temperature;
    }

    public int getTemperature() {
        return temperature;
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
    }

    // Override toString method
    @Override
    public String toString() {
        return "Thermostat{" +
                "deviceId='" + getDeviceId() + '\'' +
                ", deviceName='" + getDeviceName() + '\'' +
                ", isOn=" + isOn() +
                ", temperature=" + temperature +
                '}';
    }
}

// SecurityCamera subclass
public static class SecurityCamera extends Device {
    private String resolution;

    public SecurityCamera(String deviceId, String deviceName, String resolution) {
        super(deviceId, deviceName);
        this.resolution = resolution;
    }

    public String getResolution() {
        return resolution;
    }

    public void setResolution(String resolution) {
        this.resolution = resolution;
    }

    // Override toString method
    @Override
    public String toString() {
        return "SecurityCamera{" +
                "deviceId='" + getDeviceId() + '\'' +
                ", deviceName='" + getDeviceName() + '\'' +
                ", isOn=" + isOn() +
                ", resolution='" + resolution + '\'' +
                '}';
    }
}

// Abstract SmartDevice class (not used directly here)
public static abstract class SmartDevice {
    // Add common functionality or attributes if needed for all smart devices
}

// SmartHomeSystem class
public static class SmartHomeSystem {
    private HashMap<String, Device> devices;

    public SmartHomeSystem() {
        this.devices = new HashMap<>();
    }

    public void addDevice(Device device) {
        devices.put(device.getDeviceId(), device);
    }

    public void removeDevice(String deviceId) {
        devices.remove(deviceId);
    }

    public Device getDevice(String deviceId) {
        return devices.get(deviceId);
    }

    public void listDevices() {
        for (Device device : devices.values()) {
            System.out.println(device);
        }
    }

    // Inner class DeviceIterator implementing Iterator<Device>
    public class DeviceIterator implements Iterator<Device> {
        private Iterator<Map.Entry<String, Device>> iterator;

        public DeviceIterator() {
            this.iterator = devices.entrySet().iterator();
        }

        @Override
        public boolean hasNext() {
            return iterator.hasNext();
        }

        @Override
        public Device next() {
            return iterator.next().getValue();
        }

        @Override
        public void remove() {
            iterator.remove();
        }
    }

    // SortByName class implementing Comparator<Device>
    public static class SortByName implements Comparator<Device> {
        @Override
        public int compare(Device d1, Device d2) {
            return d1.getDeviceName().compareTo(d2.getDeviceName());
        }
    }
}

// Swing GUI for Smart Home System
public static class SmartHomeGUI extends JFrame {
    private SmartHomeSystem smartHomeSystem;
    private JTextArea deviceListArea;

    public SmartHomeGUI() {
        smartHomeSystem = new SmartHomeSystem();

        setTitle("Smart Home Automation System");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600, 400);
        setLayout(new BorderLayout());

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        JButton addDeviceButton = new JButton("Add Device");
        addDeviceButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Implement adding device functionality
                // Example: showInputDialog and add to smartHomeSystem
            }
        });

        JButton listDevicesButton = new JButton("List Devices");
        listDevicesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listDevices();
            }
        });

        controlPanel.add(addDeviceButton);
        controlPanel.add(listDevicesButton);

        deviceListArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(deviceListArea);

        add(controlPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);

        setVisible(true);
    }

    private void listDevices() {
        deviceListArea.setText("");
        for (Device device : smartHomeSystem.devices.values()) {
            deviceListArea.append(device.toString() + "\n");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SmartHomeGUI();
            }
        });
    }
}

public static void main(String[] args) {
    // Testing the SmartHomeSystem and devices
    SmartHomeSystem smartHomeSystem = new SmartHomeSystem();

    Light light = new Light("L1", "Living Room Light", 75);
    Thermostat thermostat = new Thermostat("T1", "Living Room Thermostat", 22);
    SecurityCamera camera = new SecurityCamera("C1", "Front Door Camera", "1080p");

    smartHomeSystem.addDevice(light);
    smartHomeSystem.addDevice(thermostat);
    smartHomeSystem.addDevice(camera);

    // List devices
    smartHomeSystem.listDevices();

    // Example usage of iterator
    SmartHomeSystem.DeviceIterator iterator = smartHomeSystem.new DeviceIterator();
    while (iterator.hasNext()) {
        Device device = iterator.next();
        System.out.println("Iterator device: " + device);
    }

    // Example usage of sorting by name
    ArrayList<Device> deviceList = new ArrayList<>(smartHomeSystem.devices.values());
    deviceList.sort(new SmartHomeSystem.SortByName());
    System.out.println("\nDevices sorted by name:");
    for (Device device : deviceList) {
        System.out.println(device);
    }

    // Example usage of Swing GUI
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new SmartHomeGUI();
        }
    });
}

}