Implement the classes and methods to maintain user data using inheritance as described below. Create a class User and its methods as follows:
  • The constructor takes a single parameter, userName, and sets user name.
  • The method getUsername() returns the username.
  • The method setUsername(username) set’s the username of the user to the given username.
Create a class ChatUser that inherits User class and has the following methods:
  • The constructor takes a single parameter, userName, then sets username to userName and the initial warning count to 0.
  • The method giveWarning() that increases the warning count by 1.
  • The method getWarningCount() that returns the current warning count.
The locked stub code in the editor validates the correctness of the ChatUser class implementation by performing the following operations:
  • SetName username: This operation updates the username.
  • GiveWarning: This operation increases the warning count of the user.
After performing all the operations, the locked stub code prints the current username and warning count of the user.  Finally, the use of inheritance is tested.
Input Format For Custom Testing
The first line contains a string, n, the initial username when the ChatUser object is created. The second line contains an integer, m, the number of operations. Each line i of the m subsequent lines (where 0 ≤ i < m) contains one of the two operations listed above with a parameter if necessary.
Sample Case 0

Sample Input For Custom Testing

STDIN                    Function
-----                    --------
Jay                    → username = 'Jay'
5                      → number of operations = 5
GiveWarning            → first operation
GiveWarning
SetName JayMenon
GiveWarning
GiveWarning            → fifth operation

Sample Output

User JayMenon has a warning count of 4
ChatUser extends User: true

Explanation

A ChatUser object is created with the usernameJay‘. As per the given operations, the name is set to ‘JayMenon‘ and the warning count is increased 4 times. Hence the final output is ‘User JayMenon has warning count of 4’. The last line checks if ChatUser inherits the User class.

Solution:

				
					'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding("ascii");
let inputString = "";
let currentLine = 0;

process.stdin.on("data", function (chunk) {
    inputString += chunk;
});
process.stdin.on("end", function () {
    inputString = inputString.split('\n');
    main();
});

function readLine() {
  return inputString[currentLine++];
}

class User {
    constructor(userName) {
        this.userName = userName;
    }

    getUsername() {
        return this.userName;
    }

    setUsername(username) {
        this.userName = username;
    }
}

class ChatUser extends User {
    constructor(userName) {
        super(userName);
        this.warningCount = 0;
    }

    giveWarning() {
        this.warningCount++;
    }

    getWarningCount() {
        return this.warningCount;
    }
}


function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
    
    const initialUsername = readLine().trim();
    const chatUserObj = new ChatUser(initialUsername);
    
    let numberOfOperations = parseInt(readLine().trim());
    while (numberOfOperations-- > 0) {
        const inputs = readLine().trim().split(' ');
        const operation = inputs[0];
        const username = inputs[1];

        switch(operation) {
            case 'GiveWarning':
                chatUserObj.giveWarning();
                break;
            case 'SetName':
                chatUserObj.setUsername(username);
                break;
            default:
                break;
        }
    }
    ws.write(`User ${chatUserObj.getUsername()} has a warning count of ${chatUserObj.getWarningCount()}\n`);
    ws.write(`ChatUser extends User: ${(chatUserObj instanceof User).toString()}`);
    ws.end();
}