Skip to content

Commit

Permalink
changed class person
Browse files Browse the repository at this point in the history
  • Loading branch information
Mary2811 committed Nov 2, 2023
1 parent ae8c19a commit 8da8aad
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions Classes/person.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
class Person {
constructor(name, age) {
constructor(name, age, gender) {
if (typeof name !== 'string' || typeof gender !== 'string' || typeof age !== 'number') {
throw new Error('Invalid argument type');
}
this.name = name;
this.age = age;
this.gender = gender;
}

sayHello() {
console.log(`Hello, my name is ${this.name}`);
static createMan(name, age) {
return new Person(name, age, 'man');
}

introduce() {
console.log(`I am ${this.age} years old`);
static createWoman(name, age) {
return new Person(name, age, 'woman');
}

celebrateBirthday() {
this.age++;
console.log(`Happy birthday! Now I am ${this.age} years old`);
greet() {
return `Hello, my name is ${this.name}. I am ${this.age} years old and I am a ${this.gender}.`;
}

isAdult() {
return this.age >= 18;
}

toString() {
return `Person: ${this.name}, ${this.age}, ${this.gender}`;
}
}

// Пример использования:

const person1 = new Person('John', 25);
person1.sayHello(); // Hello, my name is John
person1.introduce(); // I am 25 years old
person1.celebrateBirthday(); // Happy birthday! Now I am 26 years old
try {
const person1 = new Person('John', 25, 'man');
console.log(person1.greet()); // Output: Hello, my name is John. I am 25 years old and I am a man.
console.log(person1.isAdult()); // Output: true
console.log(person1.toString()); // Output: Person: John, 25, man

const person2 = Person.createWoman('Jane', 17);
console.log(person2.greet()); // Output: Hello, my name is Jane. I am 20 years old and I am a woman.
console.log(person2.isAdult()); // Output: true
console.log(person2.toString()); // Output: Person: Jane, 20, woman

const person2 = new Person('Alice', 30);
person2.sayHello(); // Hello, my name is Alice
person2.introduce(); // I am 30 years old
person2.celebrateBirthday(); // Happy birthday! Now I am 31 years old
const person3 = new Person(123, '30', 'man'); // Invalid argument type
} catch (error) {
console.log(error.message);
}

0 comments on commit 8da8aad

Please sign in to comment.