📜  在 TypeScript 中编写一个计算工资的程序

📅  最后修改于: 2022-05-13 01:56:14.473000             🧑  作者: Mango

在 TypeScript 中编写一个计算工资的程序

在本文中,我们将了解如何使用简单的数学逻辑和运算符(如除(“/”)或乘(“*”)或加(“+”))在 TypeScript 中创建一个基本的工资计算器。

语法:以下是枚举的语法:

enum enums_name {
    .......
}

语法:以下是类声明

class class_name {
    constructor () {
        .....
    }
    // Methods and variables.....
}

以下是通过控制台执行时显示枚举和类输出的示例。

示例 1:在此示例中,我们将简单地创建枚举和类以及它们随机传递的结果。

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();


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();


输出:

Enums Output : 
{ APPLE: 'Apple', MANGO: 'Mango', GRAPES: 'Grapes' }
Name of a Person is : GeeksforGeeks

现在我们已经详细了解了枚举和类,让我们通过以下示例深入了解创建基本工资计算器的任务。

示例 2:在此示例中,我们将创建一个基本工资计算器,用户将在其中传递总工资金额,然后我们的逻辑将进一步区分基本工资、医疗金额、房屋租金金额和交通费金额,并将结果显示在安慰。

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();

输出:

Basic Salary : 500000
HouseRent : 300000
Medical : 100000
Conveyance : 100000
Basic Salary : 25000000
HouseRent : 15000000
Medical : 5000000
Conveyance : 5000000

参考: https://www.typescriptlang.org/docs/handbook/enums.html