Object-Oriented Programming/Validation/JavaScript

index.js

edit
// This program demonstrates use of the Temperature class.

const Temperature = require("./temperature");

main();

// Runs the main program.
function main() {
    let temperature = new Temperature({celsius: 37});
    console.log(temperature.fahrenheit);

    temperature = new Temperature({fahrenheit: 98.6});
    console.log(temperature.celsius);

    temperature = new Temperature();
    temperature.celsius = 37;
    console.log(temperature.fahrenheit);

    temperature = new Temperature();
    temperature.fahrenheit = 98.6
    console.log(temperature.celsius);

    temperature = new Temperature();
    console.log(temperature.toCelsius(98.6));
    console.log(temperature.toFahrenheit(37));

    try {
        temperature = new Temperature({celsius: 1, fahrenheit: 2});
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        temperature = new Temperature({celsius: 'a'});
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        temperature = new Temperature({celsius: -500});
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        temperature = new Temperature({fahrenheit: 'b'});
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        temperature = new Temperature({fahrenheit: -600});
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        console.log(temperature.toCelsius('c'));
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        console.log(temperature.toCelsius(-700));
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        console.log(temperature.toFahrenheit('d'));
    }
    catch(error) {
        console.log(error.message);
    }

    try {
        console.log(temperature.toFahrenheit(-800));
    }
    catch(error) {
        console.log(error.message);
    }
}

temperature.js

edit
// Temperature converter. Provides temperature conversion functions. Supports Fahrenheit and Celius temperatures.
//
// Examples:
//    let temperature = new Temperature({celsius: 37});
//    console.log(temperature.fahrenheit);
//
//    temperature = new Temperature({fahrenheit: 98.6});
//    console.log(temperature.celsius);
//
//    temperature = new Temperature();
//    temperature.celsius = 37;
//    console.log(temperature.fahrenheit);
//
//    temperature = new Temperature();
//    temperature.fahrenheit = 98.6
//    console.log(temperature.celsius);
//
//    temperature = new Temperature();
//    console.log(temperature.toCelsius(98.6));
//    console.log(temperature.toFahrenheit(37));
//
// References:
//    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
//    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
//    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
//    https://www.smashingmagazine.com/2016/07/how-to-use-arguments-and-parameters-in-ecmascript-6/
// https://rollbar.com/guides/javascript-throwing-exceptions

class Temperature
{
    _celsius = undefined;
    _fahrenheit = undefined;

    // Creates Temperature object.
    // Throws error if both celsius and fahrenheit are initialized.
    constructor({celsius, fahrenheit} = {}) {
        if (celsius != undefined && fahrenheit != undefined) {
            throw new Error("Only initialize celsius or fahrenheit, not both.");
        }

        if (celsius != undefined) {
            this.celsius = celsius;
        }

        if (fahrenheit != undefined) {
            this.fahrenheit = fahrenheit
        }
    }

    // Returns absolute zero Celsius.
    get absoluteZeroCelsius() {
        return -273.15;
    }

    // Returns absolute zero Fahrenheit.
    get absoluteZeroFahrenheit() {
        return -459.67;
    }

    // Returns Celsius value.
    get celsius() {
        return this._celsius;
    }

    // Sets Celsius value.
    // Throws error if Celius temperature is not a number.
    // Throws error if Celius temperature is below absolute zero.
    set celsius(value) {
        this._celsius = this.validateCelsius(value);
        this._fahrenheit = this.toFahrenheit(value);
    }

    // Returns Fahrenheit value.
    get fahrenheit() {
        return this._fahrenheit;
    }

    // Sets Fahrenheit value.
    // Throws error if Fahrenheit temperature is not a number.
    // Throws error if Fahrenheit temperature is below absolute zero.
    set fahrenheit(value) {
        this._fahrenheit = this.validateFahrenheit(value);
        this._celsius = this.toCelsius(value);
    }

    // Converts Fahrenheit temperature to Celsius.
    toCelsius(fahrenheit) {
        fahrenheit = this.validateFahrenheit(fahrenheit);
        return (fahrenheit - 32) * 5 / 9;
    }

    // Converts Celsius temperature to Fahrenheit.
    toFahrenheit(celsius) {
        celsius = this.validateCelsius(celsius);
        return celsius * 9 / 5 + 32;
    }

    // Validates Celsius temperature.
    // Throws error if Celius temperature is not a number.
    // Throws error if Celius temperature is below absolute zero.
    validateCelsius(celsius) {
        if (isNaN(celsius)) {
            throw new Error("celsius must be a number. Received '" + celsius + "'");
        }

        if (celsius < this.absoluteZeroCelsius) {
            throw new Error("celsius must not be below absolute zero (" +
                this.absoluteZeroCelsius + ") Received " + celsius);
        }

        return celsius;
    }

    // Validates Fahrenheit temperature.
    // Throws error if Fahrenheit temperature is not a number.
    // Throws error if Fahrenheit temperature is below absolute zero.
    validateFahrenheit(fahrenheit) {
        if (isNaN(fahrenheit)) {
            throw new Error("fahrenheit must be a number. Received '" + fahrenheit + "'");
        }

        if (fahrenheit < this.absoluteZeroFahrenheit) {
            throw new Error("fahrenheit must not be below absolute zero (" +
                this.absoluteZeroFahrenheit + ") Received " + fahrenheit);
        }

        return fahrenheit;
    }
}

module.exports = Temperature;

Try It

edit

Copy and paste the code above into one of the following free online development environments or use your own JavaScript compiler / interpreter / IDE.

See Also

edit