-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
aquila
committed
May 11, 2022
1 parent
97f8f2c
commit e1f1d41
Showing
2 changed files
with
52 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ | ||
}; | ||
} | ||
|