Skip to content

Commit

Permalink
task2 GREEN (+REFACTOR)
Browse files Browse the repository at this point in the history
  • Loading branch information
aquila committed May 11, 2022
1 parent 97f8f2c commit e1f1d41
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
15 changes: 11 additions & 4 deletions apps/katapotter/src/app/game.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { Game } from './game';

describe('Game', () => {
let game : Game;
beforeEach(() => {
game = new Game();
});

it('should create an instance', () => {
expect(game).toBeTruthy();
});

test('buy one', () => {
const game = new Game();
game.buy(1);
game.buy("p1", 1);
expect(game.price).toBe(100);
});

test('buy two', () => {
const game = new Game();
game.buy(2);
game.buy("p1", 1);
game.buy("p2", 1);
expect(game.price).toBe(190);
});
});
44 changes: 41 additions & 3 deletions apps/katapotter/src/app/game.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
export class Game {
buy(pins: number){}
// private _price = 0;
private booksvector: bookinfo[] = [];
private discount: number[] = [1, 0.95, 0.9, 0.8, 0.75];

buy(bookname: string, num: number){
let flag = false;
for( let i=0;i<this.booksvector.length;i++ ){
if(this.booksvector[i].name == bookname){
flag = true;
}
}
if(flag == false){
// let book = foo(bookname, num);
this.booksvector.push(foo(bookname, num))
}
}
get price(){
return 100;
let final_price = 0;
// console.log(this.booksvector.length)
for( let i=0;i<this.booksvector.length;i++ ){
final_price += this.booksvector[i].len * 100;
// console.log(this.booksvector[i].len)
// console.log(this.booksvector[i].name)
}

final_price = final_price * this.discount[this.booksvector.length-1];

return final_price;
}
}
}

interface bookinfo{
name: string;
len: number;
}

function foo(name_: string, len_: number): bookinfo {
return {
name: name_,
len: len_
};
}

0 comments on commit e1f1d41

Please sign in to comment.