From e8aa16ae4a3453430da44082216d4c6465fd0cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CSinyru=E2=80=9D?= <“znchen720@gmail.comgit config --global pull.rebase truegit config --global branch.autosetuprebase alwaysgit config --global push.default simplegit config --global branch.autosetupmerge truegit config --global core.editor atom> Date: Fri, 17 Mar 2017 16:26:10 -0400 Subject: [PATCH] Finish Angular Lab on Components Implemented a vending machine app that enables user to 1. increase balance 2. buy drinks 3. return remaining balance 4. retrieve and updates database Name: Zhu Chen Github: https://github.com/sinyru Paired with: Lauren Paik, Faith Kaya --- db.json | 6 +-- src/app/app.component.html | 1 + src/app/app.component.ts | 1 + src/app/app.module.ts | 4 +- .../insert-coin/insert-coin.component.html | 3 +- src/app/insert-coin/insert-coin.component.ts | 6 +++ src/app/select-item/select-item.component.css | 0 .../select-item/select-item.component.html | 7 ++++ .../select-item/select-item.component.spec.ts | 25 +++++++++++++ src/app/select-item/select-item.component.ts | 37 +++++++++++++++++++ 10 files changed, 85 insertions(+), 5 deletions(-) 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.json b/db.json index baf7af9..dc3315b 100644 --- a/db.json +++ b/db.json @@ -4,19 +4,19 @@ "id": 1, "name": "Sprite", "cost": 0.5, - "remaining": 10 + "remaining": 4 }, { "id": 2, "name": "Coke", "cost": 0.5, - "remaining": 10 + "remaining": 7 }, { "id": 3, "name": "Dr. Pepper", "cost": 0.75, - "remaining": 10 + "remaining": 8 }, { "id": 4, diff --git a/src/app/app.component.html b/src/app/app.component.html index 20f9499..9faf7de 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -2,3 +2,4 @@

{{title}}

+ diff --git a/src/app/app.component.ts b/src/app/app.component.ts index ef01059..86fca99 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,6 @@ import { Component } from '@angular/core'; + @Component({ selector: 'app-root', templateUrl: './app.component.html', diff --git a/src/app/app.module.ts b/src/app/app.module.ts index b290358..1764d2e 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -8,11 +8,13 @@ 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'; @NgModule({ declarations: [ AppComponent, - InsertCoinComponent + InsertCoinComponent, + SelectItemComponent ], imports: [ BrowserModule, diff --git a/src/app/insert-coin/insert-coin.component.html b/src/app/insert-coin/insert-coin.component.html index 0513897..12baed4 100644 --- a/src/app/insert-coin/insert-coin.component.html +++ b/src/app/insert-coin/insert-coin.component.html @@ -1,4 +1,5 @@ -

{{ coinBalance? (coinBalance | currency:'USD':true): 'INSERT COIN' }}

+

{{ coinBalance? (coinBalance | currency:'EUR':true): 'INSERT COIN' }}

+ diff --git a/src/app/insert-coin/insert-coin.component.ts b/src/app/insert-coin/insert-coin.component.ts index 7c16031..70fe1b3 100644 --- a/src/app/insert-coin/insert-coin.component.ts +++ b/src/app/insert-coin/insert-coin.component.ts @@ -21,4 +21,10 @@ export class InsertCoinComponent implements OnInit { this.balanceService.addBalance(amount); } + returnCoins() { + this.balanceService.setBalance(0); + alert('Coins Return!'); + } + + } 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..60a1b7e --- /dev/null +++ b/src/app/select-item/select-item.component.html @@ -0,0 +1,7 @@ +
  • + + {{item.name}} +
  • + + 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..b55392e --- /dev/null +++ b/src/app/select-item/select-item.component.ts @@ -0,0 +1,37 @@ +import { Component, OnInit } from '@angular/core'; +import { ItemService } from '../item/item.service'; +import { BalanceService } from '../balance/balance.service'; + + +@Component({ + selector: 'app-select-item', + templateUrl: './select-item.component.html', + styleUrls: ['./select-item.component.css'] +}) +export class SelectItemComponent implements OnInit { + public items; + + constructor(public itemService: ItemService, public balanceService: BalanceService) { } + // constructor(public balanceService: BalanceService) { } + ngOnInit() { + this.itemService.onItemsRetrieved((items)=>{ + this.items = items; + }) + } + + onItemSelected(item) { + this.itemService.setSelectedItem(item); + } + + getDrink() { + if (this.itemService.hasSufficientBalance(this.balanceService.getBalance())){ + this.itemService.dispenseItem(()=>{ + alert("You got " + this.itemService.getSelectedItem().name); + this.balanceService.deductBalance(this.itemService.getSelectedItem().cost); + }) + } else { + alert("Not Enough money"); + } + } + +}