Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ <h1>
{{title}}
</h1>
<app-insert-coin></app-insert-coin>
<app-select-item></app-select-item>
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 12 additions & 9 deletions src/app/balance/balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@ import { Observable } from 'rxjs/Observable';
@Injectable()
export class BalanceService {
private balance: number = 0;
// subject is our observable
// kind of like an event queue-er
// .next puts another event in the queue
private subject: Subject<number> = new Subject<number>();

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.updateSubject();
}

deductBalance(amount): void {
Expand Down
2 changes: 2 additions & 0 deletions src/app/insert-coin/insert-coin.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<!-- currency is an angular type -->
<p class="display -sm-width">{{ coinBalance? (coinBalance | currency:'USD':true): 'INSERT COIN' }}</p>
<!-- on click, add cents to the total -->
<button class="button -yellow" (click)='addBalance(.05)'>05c</button>
<button class="button -blue" (click)='addBalance(.10)'>10c</button>
<button class="button -green" (click)='addBalance(.25)'>25c</button>
4 changes: 4 additions & 0 deletions src/app/insert-coin/insert-coin.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import { BalanceService } from '../balance/balance.service';
styleUrls: ['./insert-coin.component.scss'],
providers: []
})

// tells the class it needs to conform to OnInit
export class InsertCoinComponent implements OnInit {
coinBalance = 0;
constructor(public balanceService: BalanceService) { }

// whenever a component uses this, the balance gets updated
ngOnInit() {
this.balanceService.onBalanceUpdated((balance) => {
this.coinBalance = balance;
});
}

// this is what we invoke with the click handlers
addBalance(amount) {
this.balanceService.addBalance(amount);
}
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions src/app/select-item/select-item.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<li *ngFor='let item of items'>
<input type='radio' [value]='item.id' name='items-list' (change)='onItemSelected(item)'>
{{item.name}} -
{{item.cost}} -
{{item.remaining}}
</li>
<button (click)='onDispenseCan(item)'>Buy a Can!</button>
25 changes: 25 additions & 0 deletions src/app/select-item/select-item.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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<SelectItemComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SelectItemComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(SelectItemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
43 changes: 43 additions & 0 deletions src/app/select-item/select-item.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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;
public selectedItem;
constructor(public itemService: ItemService, public balanceService: BalanceService) { }

ngOnInit() {
this.itemService.onItemsRetrieved((items) => {
this.items = items;
})
}

onItemSelected(item) {
this.itemService.setSelectedItem(item);
}

onDispenseCan(item) {
let balance = this.balanceService.getBalance()
let can = this.itemService.getSelectedItem()
if (!this.itemService.hasRemaining()){
alert('no more cans!');
}
else if (this.itemService.hasSufficientBalance(balance)) {
this.itemService.dispenseItem(() => {
this.balanceService.deductBalance(can.cost)
const remainingBalance = this.balanceService.getBalance()
alert('here is your change: ' + remainingBalance)
this.balanceService.setBalance(0)
});
}
else {
alert('not enough funds');
};
}
}