Skip to content

Commit

Permalink
add exercise4
Browse files Browse the repository at this point in the history
  • Loading branch information
talgat-ruby committed Oct 18, 2023
1 parent f14c78d commit 2bdde17
Show file tree
Hide file tree
Showing 24 changed files with 600 additions and 0 deletions.
14 changes: 14 additions & 0 deletions exercise4/problem1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Problem 1

Create a method in the `Person` class which returns how another person's age compares. Format is **{other person name}
is {older than / younger than / the same age as} me.**

```ts
const p1 = Person("Samuel", 24)
const p2 = Person("Joel", 36)
const p3 = Person("Lily", 24)

p1.compareAge(p2) // "Joel is older than me."
p2.compareAge(p1) // "Samuel is younger than me."
p1.compareAge(p3) // "Lily is the same age as me."
```
37 changes: 37 additions & 0 deletions exercise4/problem1/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, test } from "vitest";
import Person from "./index";

describe("exercise4 - problem1", () => {
const p1 = new Person("Samuel", 24);
const p2 = new Person("Joel", 36);
const p3 = new Person("Lily", 24);

test.each([
{
output: p1.compareAge(p2),
expected: "Joel is older than me.",
},
{
output: p1.compareAge(p3),
expected: "Lily is the same age as me.",
},
{
output: p2.compareAge(p1),
expected: "Samuel is younger than me.",
},
{
output: p2.compareAge(p3),
expected: "Lily is younger than me.",
},
{
output: p3.compareAge(p1),
expected: "Samuel is the same age as me.",
},
{
output: p3.compareAge(p2),
expected: "Joel is older than me.",
},
])(".compareAge", ({ output, expected }) => {
expect(output).toBe(expected);
});
});
3 changes: 3 additions & 0 deletions exercise4/problem1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Person {}

export default Person;
19 changes: 19 additions & 0 deletions exercise4/problem2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Problem 2

Write a class called `Name` and create the following attributes given a first name and last name (as `firstName`
and `lastName`):

* An attribute called `fullName` which returns the first and last names.
* An attribute called `initials` which returns the first letters of the first and last name. Put a `.` between the two
letters.

Remember to allow the attributes `firstName` and `lastName` to be accessed individually as well.

```ts
const p1 = new Name("john", "SMITH")

p1.firstName // "John"
p1.lastName // "Smith"
p1.fullName // "John Smith"
p1.initials // "J.S"
```
33 changes: 33 additions & 0 deletions exercise4/problem2/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, test } from "vitest";
import Name from "./index";

describe("exercise4 - problem2", () => {
test.each([
{
firstName: "john",
lastName: "SMITH",
expected: {
firstName: "John",
lastName: "Smith",
fullName: "John Smith",
initials: "J.S",
},
},
{
firstName: "sARah",
lastName: "SMITH",
expected: {
firstName: "John",
lastName: "Smith",
fullName: "John Smith",
initials: "J.S",
},
},
])(".Name", ({ firstName, lastName, expected }) => {
const person = new Name(firstName, lastName);
expect(person.firstName).toBe(expected.firstName);
expect(person.lastName).toBe(expected.lastName);
expect(person.fullName).toBe(expected.fullName);
expect(person.initials).toBe(expected.initials);
});
});
3 changes: 3 additions & 0 deletions exercise4/problem2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Name {}

export default Name;
15 changes: 15 additions & 0 deletions exercise4/problem3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Problem 3

**Inheritance**

Complete `class`: `Rectangle` and `Circle` and combine it in order to calculate sum of all areas. _Do it in OOP way_.

```ts
const circle1 = new Circle(2);
const circle2 = new Circle(3);
const rect1 = new Rectangle(2, 4);
const rect2 = new Rectangle(3, 2);

console.log(sumOfAllAreas(circle1, circle2)) // ~ 40. ...
console.log(sumOfAllAreas(circle1, rect1)) // ~ 20. ...
```
50 changes: 50 additions & 0 deletions exercise4/problem3/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, test } from "vitest";
import { Circle, Rectangle, sumOfAllAreas } from "./index";

describe("exercise4 - problem3", () => {
test("inherits from the same parent", () => {
const circle = new Circle(2);
const rect = new Rectangle(3, 2);

expect(Object.getPrototypeOf(circle.constructor).name).not.toBe("");
expect(Object.getPrototypeOf(rect.constructor).name).not.toBe("");
expect(Object.getPrototypeOf(circle.constructor).name).toBe(
Object.getPrototypeOf(rect.constructor).name
);
});

describe("find sum of all areas", () => {
const circle1 = new Circle(2);
const circle2 = new Circle(3);
const rect1 = new Rectangle(2, 4);
const rect2 = new Rectangle(3, 2);

test.each([
{
shapes: [circle1, circle2],
expected: 40,
},
{
shapes: [rect1, rect2],
expected: 14,
},
{
shapes: [circle1, rect1],
expected: 20,
},
{
shapes: [circle1, rect2, circle2],
expected: 46,
},
{
shapes: [rect1, circle1, rect2, circle2],
expected: 54,
},
])(
"Math.floor(sumOfAllAreas($shapes)) -> ~$expected",
({ shapes, expected }) => {
expect(Math.floor(sumOfAllAreas(...shapes))).toBe(expected);
}
);
});
});
8 changes: 8 additions & 0 deletions exercise4/problem3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Update it as much as you want, just don't change the name
export class Circle {}

// Update it as much as you want, just don't change the name
export class Rectangle {}

// Update it as much as you want, just don't change the name
export function sumOfAllAreas() {}
17 changes: 17 additions & 0 deletions exercise4/problem4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Problem 4

The same as problem 3, but in FP style.

Complete `class`: `Rectangle` and `Circle` and combine it in order to calculate sum of all areas. _Do it in FP way_.

```ts
const circle1 = new Circle(2);
const circle2 = new Circle(3);
const rect1 = new Rectangle(2, 4);
const rect2 = new Rectangle(3, 2);

console.log(sumOfAllAreas(circle1, circle2)) // ~ 40. ...
console.log(sumOfAllAreas(circle1, rect1)) // ~ 20. ...
```

**HINT**: use `interface` instead of `inheritance`.
47 changes: 47 additions & 0 deletions exercise4/problem4/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, test } from "vitest";
import { Circle, Rectangle, sumOfAllAreas } from "./index";

describe("exercise4 - problem4", () => {
test("DOES NOT inherits", () => {
const circle = new Circle(2);
const rect = new Rectangle(3, 2);

expect(Object.getPrototypeOf(circle.constructor).name).toBe("");
expect(Object.getPrototypeOf(rect.constructor).name).toBe("");
});

describe("find sum of all areas", () => {
const circle1 = new Circle(2);
const circle2 = new Circle(3);
const rect1 = new Rectangle(2, 4);
const rect2 = new Rectangle(3, 2);

test.each([
{
shapes: [circle1, circle2],
expected: 40,
},
{
shapes: [rect1, rect2],
expected: 14,
},
{
shapes: [circle1, rect1],
expected: 20,
},
{
shapes: [circle1, rect2, circle2],
expected: 46,
},
{
shapes: [rect1, circle1, rect2, circle2],
expected: 54,
},
])(
"Math.floor(sumOfAllAreas($shapes)) -> ~$expected",
({ shapes, expected }) => {
expect(Math.floor(sumOfAllAreas(...shapes))).toBe(expected);
}
);
});
});
8 changes: 8 additions & 0 deletions exercise4/problem4/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Update it as much as you want, just don't change the name
export class Circle {}

// Update it as much as you want, just don't change the name
export class Rectangle {}

// Update it as much as you want, just don't change the name
export function sumOfAllAreas() {}
22 changes: 22 additions & 0 deletions exercise4/problem5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Problem 5

**Encapsulation**

Complete `class`: `BankAccount`. With only 2 public methods, no public properties:

1. `withdraw`: Withdraws money from balance. But only if amount does exceed balance. Return balance in success,
otherwise `-1`
2. `deposit`: Deposits money to balance. But only if amount does exceed `1_000_000`. Return balance in success,
otherwise `-1`

_Do it in OOP way_.

```ts
const account = new BankAccount();

console.log(account.withdraw(100)) // -1
console.log(account.deposit(100)) // 100
console.log(account.withdraw(10)) // 90
console.log(account.deposit(10_000_000)) // -1
console.log(account.withdraw(10)) // 80
```
62 changes: 62 additions & 0 deletions exercise4/problem5/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, test } from "vitest";
import BankAccount from "./index";

describe("exercise4 - problem5", () => {
test("should deposit to balance", () => {
const account = new BankAccount();

account.deposit(300);

expect(account.deposit(3000)).toBe(3300);
});

test("should withdraw balance", () => {
const account = new BankAccount();

account.deposit(200);

expect(account.withdraw(100)).toBe(100);
});

test("should deposit and withdraw several times", () => {
const account = new BankAccount();

account.deposit(200);
account.withdraw(100);
account.withdraw(50);
account.deposit(100);

expect(account.withdraw(50)).toBe(200 - 100 - 50 + 100 - 50);
});

test("should deposit only if not greater than 1 000 000", () => {
const account = new BankAccount();

expect(account.deposit(1_000_000)).toBe(1_000_000);
expect(account.deposit(1_000_001)).toBe(-1);
expect(account.deposit(2_000_000)).toBe(-1);
expect(account.withdraw(100_000)).toBe(900_000);
});

test("should withdraw only if has sufficient funds", () => {
const account = new BankAccount();

account.deposit(1_000_000);

expect(account.withdraw(1_000_000)).toBe(0);
expect(account.withdraw(1_000_000)).toBe(-1);
expect(account.withdraw(1)).toBe(-1);
expect(account.deposit(1)).toBe(1);
});

test("should only have 2 public methods: withdraw, deposit; 0 public properties", () => {
const account = new BankAccount();

expect(Object.getOwnPropertyNames(Object.getPrototypeOf(account))).toEqual([
"constructor",
"withdraw",
"deposit",
]);
expect(Object.getOwnPropertyNames(account)).toEqual([]);
});
});
3 changes: 3 additions & 0 deletions exercise4/problem5/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class BankAccount {}

export default BankAccount;
27 changes: 27 additions & 0 deletions exercise4/problem6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Problem 6

Take `BankAccount` from previous problem. And create 2 new: `FedexAccount` and `KazPostAccount`

1. `FedexAccount`: has 2 public methods `sendMail(recipient)` and `recieveMail(sender)`. Just use `console.log` with any
random text within the body.
2. `KazPostAccount`: has all public methods of `BankAccount` and `FedexAccount`.

Task:

* function `withdrawMoney` should only accept `BankAccount` and `KazPostAccount` instances. **HINT** only forbidden by
typescript
* function `sendLetterTo` should only accept `FedexAccount` and `KazPostAccount` instances. **HINT** only forbidden by
typescript

```ts
const normanOsborne = new BankAccount();
const peterParker = new FedexAccount();
const auntMay = new KazPostAccount();

withdrawMoney([normanOsborne, auntMay], 10); // no error
sendLetterTo([peterParker, auntMay], "Mary Jane"); // no error

withdrawMoney([peterParker, auntMay], 10); // peterParker error
```

**NOTE**: no tests because it is more about types and structure than implementation. But please do not ignore this task.
7 changes: 7 additions & 0 deletions exercise4/problem6/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { describe, expect, test } from "vitest";

describe("exercise4 - problem6", () => {
test("always true", () => {
expect(true).toBe(true);
});
});
Loading

0 comments on commit 2bdde17

Please sign in to comment.