oop-labs-collection/labs/6/Appliance.java

146 lines
3.9 KiB
Java

/*
* %W% %E% Dymik739
* Email: dymik739@109.86.70.81
*
* Copyright (C) 2023 FIOT Dev Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Class representing a general appliance and containing basic methods
* that are used in multiple child classes. Supposed to be extended by
* other classes and not to be used as a standalone class.
*
* @author Dymik739
* @since 0.3
*/
public class Appliance implements Comparable<Appliance> {
/** Indicates if this device is drawing power from the power network. */
private boolean powerConnected;
/** Defines the type of this device */
private String type;
/**
* Constructor for this class. Should be called only from within
* constructors of other classes which inherit this one.
*
* @param connected defines if the device is connected to the network
* at the start
*/
public Appliance(boolean connected) {
this.powerConnected = connected;
}
/**
* Getter for checking the power connection.
*
* @return true if connected and false otherwise
*/
public boolean getPowerState() {
return powerConnected;
}
/**
* Getter for the device type variable.
*
* @return device type string
*/
public String getType() {
return type;
}
/**
* Setter for setting the device type string.
* Should be used only from the constructors of the classes
* which inherit this one!
*
* @param type type of the device
*/
public void setType(String type) {
this.type = type;
}
/**
* Method for connecting power to the device.
*/
public void plug() {
powerConnected = true;
}
/**
* Method for disconnecting power from the device.
*/
public void unplug() {
powerConnected = false;
}
/**
* Method for getting the smaller value out of two.
*
* @param v1 first value
* @param v2 second value
*
* @return smaller value of the two given
*/
public float min(float v1, float v2) {
return v1 <= v2 ? v1 : v2;
}
/**
* Method for getting the bigger value out of two.
*
* @param v1 first value
* @param v2 second value
*
* @return bigger value of the two given
*/
public float max(float v1, float v2) {
return v1 >= v2 ? v1 : v2;
}
/**
* Dummy method for getting the power consumption of the device.
* Should be overridden by the child class!
*
* @return current power consumption.
*/
public float getPowerConsumption() {
return 0f;
}
/**
* Dummy method for performing the simulation.
* Should be overridden by the child class!
*
* @param seconds delta time for the correct simulation step
* @param ventRPM air flow created by the vent
*/
public void step(float seconds, float ventRPM) {}
/**
* Method for comparing this appliance's power consumption to another
* one. Part os the Comparable implementation.
*
* @param o another appliance to compare to
*
* @return difference between power consumption values
*/
@Override
public int compareTo(Appliance o) {
return (int) (getPowerConsumption() - o.getPowerConsumption());
}
}