From 3d33196e5394a2461aab096d539e8786b7cd4f06 Mon Sep 17 00:00:00 2001 From: Sofistication Date: Fri, 17 Mar 2017 16:26:11 -0400 Subject: [PATCH] Vending Machine - 'paired' with @cathyob and @squirtlesquad1988 - other than the bug which @akgoode and @ladypinapple solved, - we had the messages display different content based on what was bought - and in their own part of the page rather than in alerts. --- src/app/app.component.html | 2 ++ src/app/app.module.ts | 6 +++- src/app/balance/balance.service.ts | 20 ++++++----- .../dispense-button.component.css | 4 +++ .../dispense-button.component.html | 2 ++ .../dispense-button.component.spec.ts | 25 ++++++++++++++ .../dispense-button.component.ts | 34 +++++++++++++++++++ .../insert-coin/insert-coin.component.html | 1 + src/app/insert-coin/insert-coin.component.ts | 4 +++ src/app/select-item/select-item.component.css | 0 .../select-item/select-item.component.html | 8 +++++ .../select-item/select-item.component.spec.ts | 25 ++++++++++++++ src/app/select-item/select-item.component.ts | 22 ++++++++++++ 13 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 src/app/dispense-button/dispense-button.component.css create mode 100644 src/app/dispense-button/dispense-button.component.html create mode 100644 src/app/dispense-button/dispense-button.component.spec.ts create mode 100644 src/app/dispense-button/dispense-button.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/src/app/app.component.html b/src/app/app.component.html index 20f9499..abfb655 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..f70d851 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 { DispenseButtonComponent } from './dispense-button/dispense-button.component'; @NgModule({ declarations: [ AppComponent, - InsertCoinComponent + InsertCoinComponent, + SelectItemComponent, + DispenseButtonComponent ], imports: [ BrowserModule, diff --git a/src/app/balance/balance.service.ts b/src/app/balance/balance.service.ts index 7dd9878..0a947c2 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-button/dispense-button.component.css b/src/app/dispense-button/dispense-button.component.css new file mode 100644 index 0000000..5adb96f --- /dev/null +++ b/src/app/dispense-button/dispense-button.component.css @@ -0,0 +1,4 @@ +h3 { + color: white; + font-weight: bold; +} diff --git a/src/app/dispense-button/dispense-button.component.html b/src/app/dispense-button/dispense-button.component.html new file mode 100644 index 0000000..5dee319 --- /dev/null +++ b/src/app/dispense-button/dispense-button.component.html @@ -0,0 +1,2 @@ + +

{{message}}

diff --git a/src/app/dispense-button/dispense-button.component.spec.ts b/src/app/dispense-button/dispense-button.component.spec.ts new file mode 100644 index 0000000..2f1b9e6 --- /dev/null +++ b/src/app/dispense-button/dispense-button.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { DispenseButtonComponent } from './dispense-button.component'; + +describe('DispenseButtonComponent', () => { + let component: DispenseButtonComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ DispenseButtonComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(DispenseButtonComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/dispense-button/dispense-button.component.ts b/src/app/dispense-button/dispense-button.component.ts new file mode 100644 index 0000000..a129954 --- /dev/null +++ b/src/app/dispense-button/dispense-button.component.ts @@ -0,0 +1,34 @@ +import { Component, OnInit } from '@angular/core'; +import { ItemService } from '../item/item.service'; +import { BalanceService } from '../balance/balance.service'; + +@Component({ + selector: 'app-dispense-button', + templateUrl: './dispense-button.component.html', + styleUrls: ['./dispense-button.component.css'] +}) +export class DispenseButtonComponent implements OnInit { + public message: string; + constructor(private itemService: ItemService, public balanceService: BalanceService) { } + + ngOnInit() { + } + + onItemDispensed() { + let balance = this.balanceService.getBalance(); + + if (this.itemService.hasSufficientBalance(balance) && this.itemService.hasRemaining()) { + this.itemService.dispenseItem((response) => { + this.balanceService.deductBalance(response.cost); + this.message = `Enjoy your ${response.name}, NERD`; + }); + } else if (this.itemService.hasRemaining()) { + this.message = 'Not enough balance!'; + } else { + this.message = 'Sold Out! Try another drink'; + } + // alert(this.item.name); + // console.log(this.balance); + } + +} 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..e69de29 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..f0fce40 --- /dev/null +++ b/src/app/select-item/select-item.component.html @@ -0,0 +1,8 @@ +
    +
  • + + {{item.name}} + Price: {{item.cost}} + Remaining: {{item.remaining}} +
  • +
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..3708fd5 --- /dev/null +++ b/src/app/select-item/select-item.component.ts @@ -0,0 +1,22 @@ +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; + constructor(private itemService: ItemService) { } + + ngOnInit() { + this.itemService.onItemsRetrieved((items) => { + this.items = items + }) + } + + onItemSelected(item) { + this.itemService.setSelectedItem(item); + } +}