From 9b85d4e3313e22e7566421ed6cccd95574ab984c Mon Sep 17 00:00:00 2001 From: akgoode Date: Fri, 17 Mar 2017 16:26:35 -0400 Subject: [PATCH] I'm Andrew Goode I worked with Maura Pine Maura found the rounding bug and I found the solution. Solution is to add the new amount first and then round the result instead of trying to do both operations in place. --- db-1489763535590.json | 28 ++++++++++++++++ db-1489777448046.json | 28 ++++++++++++++++ db-1489779834210.json | 28 ++++++++++++++++ db-1489779895182.json | 28 ++++++++++++++++ db-1489780609463.json | 28 ++++++++++++++++ db.json | 2 +- src/app/app.component.html | 2 ++ src/app/app.module.ts | 6 +++- src/app/balance/balance.service.ts | 20 ++++++------ .../dispense-item/dispense-item.component.css | 0 .../dispense-item.component.html | 1 + .../dispense-item.component.spec.ts | 25 +++++++++++++++ .../dispense-item/dispense-item.component.ts | 32 +++++++++++++++++++ .../insert-coin/insert-coin.component.html | 1 + src/app/insert-coin/insert-coin.component.ts | 4 +++ src/app/select-item/select-item.component.css | 3 ++ .../select-item/select-item.component.html | 5 +++ .../select-item/select-item.component.spec.ts | 25 +++++++++++++++ src/app/select-item/select-item.component.ts | 24 ++++++++++++++ 19 files changed, 279 insertions(+), 11 deletions(-) create mode 100644 db-1489763535590.json create mode 100644 db-1489777448046.json create mode 100644 db-1489779834210.json create mode 100644 db-1489779895182.json create mode 100644 db-1489780609463.json create mode 100644 src/app/dispense-item/dispense-item.component.css create mode 100644 src/app/dispense-item/dispense-item.component.html create mode 100644 src/app/dispense-item/dispense-item.component.spec.ts create mode 100644 src/app/dispense-item/dispense-item.component.ts create mode 100644 src/app/select-item/select-item.component.css create mode 100644 src/app/select-item/select-item.component.html create mode 100644 src/app/select-item/select-item.component.spec.ts create mode 100644 src/app/select-item/select-item.component.ts diff --git a/db-1489763535590.json b/db-1489763535590.json new file mode 100644 index 0000000..baf7af9 --- /dev/null +++ b/db-1489763535590.json @@ -0,0 +1,28 @@ +{ + "items": [ + { + "id": 1, + "name": "Sprite", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 2, + "name": "Coke", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 3, + "name": "Dr. Pepper", + "cost": 0.75, + "remaining": 10 + }, + { + "id": 4, + "name": "Mr Pibbs", + "cost": 0.5, + "remaining": 10 + } + ] +} \ No newline at end of file diff --git a/db-1489777448046.json b/db-1489777448046.json new file mode 100644 index 0000000..baf7af9 --- /dev/null +++ b/db-1489777448046.json @@ -0,0 +1,28 @@ +{ + "items": [ + { + "id": 1, + "name": "Sprite", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 2, + "name": "Coke", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 3, + "name": "Dr. Pepper", + "cost": 0.75, + "remaining": 10 + }, + { + "id": 4, + "name": "Mr Pibbs", + "cost": 0.5, + "remaining": 10 + } + ] +} \ No newline at end of file diff --git a/db-1489779834210.json b/db-1489779834210.json new file mode 100644 index 0000000..6bbdf7a --- /dev/null +++ b/db-1489779834210.json @@ -0,0 +1,28 @@ +{ + "items": [ + { + "id": 1, + "name": "Sprite", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 2, + "name": "Coke", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 3, + "name": "Dr. Pepper", + "cost": 0.75, + "remaining": 10 + }, + { + "id": 4, + "name": "Mr Pibbs", + "cost": 0.5, + "remaining": 0 + } + ] +} \ No newline at end of file diff --git a/db-1489779895182.json b/db-1489779895182.json new file mode 100644 index 0000000..6bbdf7a --- /dev/null +++ b/db-1489779895182.json @@ -0,0 +1,28 @@ +{ + "items": [ + { + "id": 1, + "name": "Sprite", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 2, + "name": "Coke", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 3, + "name": "Dr. Pepper", + "cost": 0.75, + "remaining": 10 + }, + { + "id": 4, + "name": "Mr Pibbs", + "cost": 0.5, + "remaining": 0 + } + ] +} \ No newline at end of file diff --git a/db-1489780609463.json b/db-1489780609463.json new file mode 100644 index 0000000..baf7af9 --- /dev/null +++ b/db-1489780609463.json @@ -0,0 +1,28 @@ +{ + "items": [ + { + "id": 1, + "name": "Sprite", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 2, + "name": "Coke", + "cost": 0.5, + "remaining": 10 + }, + { + "id": 3, + "name": "Dr. Pepper", + "cost": 0.75, + "remaining": 10 + }, + { + "id": 4, + "name": "Mr Pibbs", + "cost": 0.5, + "remaining": 10 + } + ] +} \ No newline at end of file diff --git a/db.json b/db.json index baf7af9..83d0521 100644 --- a/db.json +++ b/db.json @@ -4,7 +4,7 @@ "id": 1, "name": "Sprite", "cost": 0.5, - "remaining": 10 + "remaining": 5 }, { "id": 2, diff --git a/src/app/app.component.html b/src/app/app.component.html index 20f9499..a4fee6a 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -2,3 +2,5 @@

{{title}}

+ + diff --git a/src/app/app.module.ts b/src/app/app.module.ts index b290358..62f9f67 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -8,11 +8,15 @@ import { InsertCoinComponent } from './insert-coin/insert-coin.component'; import { ItemService } from './item/item.service'; import { BalanceService } from './balance/balance.service'; +import { SelectItemComponent } from './select-item/select-item.component'; +import { DispenseItemComponent } from './dispense-item/dispense-item.component'; @NgModule({ declarations: [ AppComponent, - InsertCoinComponent + InsertCoinComponent, + SelectItemComponent, + DispenseItemComponent ], imports: [ BrowserModule, diff --git a/src/app/balance/balance.service.ts b/src/app/balance/balance.service.ts index 7dd9878..a8c41be 100644 --- a/src/app/balance/balance.service.ts +++ b/src/app/balance/balance.service.ts @@ -8,27 +8,29 @@ export class BalanceService { private subject: Subject = new Subject(); constructor() { } - + private updateSubject(): void { this.subject.next(this.balance); } - setBalance(amount): void { - this.balance = amount; + setBalance(amount): void { + this.balance = amount; this.updateSubject(); } - - getBalance(): number { - return this.balance; + + getBalance(): number { + return this.balance; } - - addBalance(amount): void { + + addBalance(amount): void { this.balance += amount; - this.updateSubject(); + this.balance = (Math.round(this.balance * 100) / 100); + this.updateSubject(); } deductBalance(amount): void { this.balance -= amount; + this.balance = (Math.round(this.balance * 100) / 100); this.updateSubject(); } diff --git a/src/app/dispense-item/dispense-item.component.css b/src/app/dispense-item/dispense-item.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/dispense-item/dispense-item.component.html b/src/app/dispense-item/dispense-item.component.html new file mode 100644 index 0000000..fea2140 --- /dev/null +++ b/src/app/dispense-item/dispense-item.component.html @@ -0,0 +1 @@ + diff --git a/src/app/dispense-item/dispense-item.component.spec.ts b/src/app/dispense-item/dispense-item.component.spec.ts new file mode 100644 index 0000000..e3d2ca1 --- /dev/null +++ b/src/app/dispense-item/dispense-item.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { DispenseItemComponent } from './dispense-item.component'; + +describe('DispenseItemComponent', () => { + let component: DispenseItemComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ DispenseItemComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(DispenseItemComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/dispense-item/dispense-item.component.ts b/src/app/dispense-item/dispense-item.component.ts new file mode 100644 index 0000000..cadd5af --- /dev/null +++ b/src/app/dispense-item/dispense-item.component.ts @@ -0,0 +1,32 @@ +import { Component, OnInit } from '@angular/core'; +import { ItemService } from '../item/item.service'; +import { BalanceService } from '../balance/balance.service'; + +@Component({ + selector: 'app-dispense-item', + templateUrl: './dispense-item.component.html', + styleUrls: ['./dispense-item.component.css'] +}) +export class DispenseItemComponent implements OnInit { + + constructor(private itemService: ItemService, private balanceService: BalanceService) { } + + ngOnInit() { + } + + onDispenseClick() { + let item = this.itemService.getSelectedItem(); + let balance = this.balanceService.getBalance(); + + if (this.itemService.hasRemaining() && this.itemService.hasSufficientBalance(balance)) { + this.itemService.dispenseItem(() => { + this.balanceService.deductBalance(item.cost); + alert('Successfully dispensed ' + item.name); + }); + } else if (!this.itemService.hasRemaining()) { + alert('No more ' + item.name); + } else { + alert('Insufficient funds'); + } + } +} diff --git a/src/app/insert-coin/insert-coin.component.html b/src/app/insert-coin/insert-coin.component.html index 0513897..d0ef4c8 100644 --- a/src/app/insert-coin/insert-coin.component.html +++ b/src/app/insert-coin/insert-coin.component.html @@ -2,3 +2,4 @@ + diff --git a/src/app/insert-coin/insert-coin.component.ts b/src/app/insert-coin/insert-coin.component.ts index 7c16031..a646b68 100644 --- a/src/app/insert-coin/insert-coin.component.ts +++ b/src/app/insert-coin/insert-coin.component.ts @@ -21,4 +21,8 @@ export class InsertCoinComponent implements OnInit { this.balanceService.addBalance(amount); } + returnCoins() { + this.balanceService.setBalance(0); + alert('Coins returned'); + } } diff --git a/src/app/select-item/select-item.component.css b/src/app/select-item/select-item.component.css new file mode 100644 index 0000000..349c63a --- /dev/null +++ b/src/app/select-item/select-item.component.css @@ -0,0 +1,3 @@ +li { + list-style: none; +} diff --git a/src/app/select-item/select-item.component.html b/src/app/select-item/select-item.component.html new file mode 100644 index 0000000..3a8802c --- /dev/null +++ b/src/app/select-item/select-item.component.html @@ -0,0 +1,5 @@ +
    +
  • + {{item.name}} ${{item.cost}} +
  • +
diff --git a/src/app/select-item/select-item.component.spec.ts b/src/app/select-item/select-item.component.spec.ts new file mode 100644 index 0000000..217a5ae --- /dev/null +++ b/src/app/select-item/select-item.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { SelectItemComponent } from './select-item.component'; + +describe('SelectItemComponent', () => { + let component: SelectItemComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ SelectItemComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(SelectItemComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/select-item/select-item.component.ts b/src/app/select-item/select-item.component.ts new file mode 100644 index 0000000..128e725 --- /dev/null +++ b/src/app/select-item/select-item.component.ts @@ -0,0 +1,24 @@ +import { Component, OnInit } from '@angular/core'; +import { ItemService } from '../item/item.service'; + +@Component({ + selector: 'app-select-item', + templateUrl: './select-item.component.html', + styleUrls: ['./select-item.component.css'] +}) +export class SelectItemComponent implements OnInit { + public items; + public selectedItem; + constructor(private itemService: ItemService) { } + + ngOnInit() { + this.itemService.onItemsRetrieved((items) => { + this.items = items + }); + } + + onItemSelected(item){ + this.itemService.setSelectedItem(item); + } + +}