Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TempConverter {
- public enum TEMP_TYPE {
- Celsius, Fahrenheit;
- }
- private final static TEMP_TYPE DEFAULT_TEMP_TYPE = TEMP_TYPE.Celsius;
- private final TEMP_TYPE convertingFrom, convertingTo;
- public TempConverter(TEMP_TYPE convertFrom) {
- convertingFrom = convertFrom;
- if (convertFrom == TEMP_TYPE.Celsius) {
- convertingTo = TEMP_TYPE.Fahrenheit;
- }
- else if (convertFrom == TEMP_TYPE.Fahrenheit) {
- convertingTo = TEMP_TYPE.Celsius;
- }
- else {
- convertingTo = DEFAULT_TEMP_TYPE;
- }
- }
- public int convertTemp(double fromTemp) {
- int toTemp = 0;
- if (convertingFrom == TEMP_TYPE.Fahrenheit) {
- toTemp = (int)(Math.round((fromTemp - 32) * (5.0 / 9.0)));
- }
- else if (convertingFrom == TEMP_TYPE.Celsius) {
- toTemp = (int)(Math.round(fromTemp * 1.8 + 32));
- }
- else {
- throw new TempTypeDoesntExistException("Temperature type isn't supported.");
- }
- return toTemp;
- }
- public TEMP_TYPE fromTempType() {
- return convertingFrom;
- }
- public TEMP_TYPE toTempType() {
- return convertingTo;
- }
- public String fromTempTypeStr() {
- return convertingFrom.toString();
- }
- public String toTempTypeStr() {
- return convertingTo.toString();
- }
- public static class TempTypeDoesntExistException extends IndexOutOfBoundsException {
- public TempTypeDoesntExistException() { }
- public TempTypeDoesntExistException(String message) {
- super(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment