Write a program to calculate salary in TypeScript
Last Updated :
28 Feb, 2022
In this article, we will understand how we could create a basic Salary Calculator in TypeScript by using simple mathematical logics and operators (like divide("/") or multiply ("*") or addition ("+")).
Syntax: Following is the syntax of enums:
enum enums_name {
.......
}
Syntax: Following is the class declaration
class class_name {
constructor () {
.....
}
// Methods and variables.....
}
Following are the examples which show enums and class output on execution over the console.
Example 1: In this example, we will simply create enum and class and their randomly passed results.
JavaScript
enum fruits_name {
APPLE = 'Apple',
MANGO = 'Mango',
GRAPES = 'Grapes'
}
console.log('Enums Output : ');
console.log(fruits_name);
class Person {
name : string;
constructor (name : string) {
this.name = name;
}
displayName () : void {
console.log(`Name of a Person is : ${ this.name}`);
}
}
const person = new Person('GeeksforGeeks');
person.displayName();
Output:
Enums Output :
{ APPLE: 'Apple', MANGO: 'Mango', GRAPES: 'Grapes' }
Name of a Person is : GeeksforGeeks
Now that we have understood in detail about enums and classes let us dive into our task of creating a basic Salary Calculator through the following example.
Example 2: In this example, we will create a basic salary calculator, in which the user will pass a total salary amount and further our logic will separate Basic Salary, Medical amount, House Rent amount, and Conveyance amount and displays the result over the console.
JavaScript
enum SalaryDivision {
// Following values could be considered in %
Basic = 50,
HouseRent = 30,
Medical = 10,
Conveyance = 10
}
class SalaryCalculator {
GrossSalary : number;
constructor(GrossSalary : number) {
this.GrossSalary = GrossSalary;
}
displaySalary() : void {
let BasicSalary : number = (this.GrossSalary
* SalaryDivision.Basic) / 100;
let HouseRent : number = (this.GrossSalary
* SalaryDivision.HouseRent) / 100;
let Medical : number = (this.GrossSalary
* SalaryDivision.Medical) / 100;
let Conveyance : number = (this.GrossSalary
* SalaryDivision.Conveyance) / 100;
console.log("Basic Salary : " + BasicSalary);
console.log("HouseRent : " + HouseRent);
console.log("Medical : " + Medical);
console.log("Conveyance : " + Conveyance);
}
}
let salary1 = new SalaryCalculator(1000000);
let salary2 = new SalaryCalculator(50000000);
salary1.displaySalary();
salary2.displaySalary();
Output:
Basic Salary : 500000
HouseRent : 300000
Medical : 100000
Conveyance : 100000
Basic Salary : 25000000
HouseRent : 15000000
Medical : 5000000
Conveyance : 5000000
Reference: https://www.typescriptlang.org/docs/handbook/enums.html