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
2 changes: 2 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ <h1>
{{title}}
</h1>
<app-insert-coin></app-insert-coin>
<app-select-item></app-select-item>
<app-dispense-button></app-dispense-button>
6 changes: 5 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 11 additions & 9 deletions src/app/balance/balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ export class BalanceService {
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.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();
}

Expand Down
4 changes: 4 additions & 0 deletions src/app/dispense-button/dispense-button.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h3 {
color: white;
font-weight: bold;
}
2 changes: 2 additions & 0 deletions src/app/dispense-button/dispense-button.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<button class="button -blue" (click)="onItemDispensed()">Dispense!</button>
<h3>{{message}}</h3>
25 changes: 25 additions & 0 deletions src/app/dispense-button/dispense-button.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 { DispenseButtonComponent } from './dispense-button.component';

describe('DispenseButtonComponent', () => {
let component: DispenseButtonComponent;
let fixture: ComponentFixture<DispenseButtonComponent>;

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

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

it('should create', () => {
expect(component).toBeTruthy();
});
});
34 changes: 34 additions & 0 deletions src/app/dispense-button/dispense-button.component.ts
Original file line number Diff line number Diff line change
@@ -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);
}

}
1 change: 1 addition & 0 deletions src/app/insert-coin/insert-coin.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
<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>
<button class="button" (click)='returnCoins()'>Return Coins</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 @@ -21,4 +21,8 @@ export class InsertCoinComponent implements OnInit {
this.balanceService.addBalance(amount);
}

returnCoins() {
this.balanceService.setBalance(0);
alert('Coins returned');
}
}
Empty file.
8 changes: 8 additions & 0 deletions src/app/select-item/select-item.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul>
<li *ngFor='let item of items'>
<input type='radio' [value]='item.id' name='items-list' (change)='onItemSelected(item)'>
{{item.name}}
Price: {{item.cost}}
Remaining: {{item.remaining}}
</li>
</ul>
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();
});
});
22 changes: 22 additions & 0 deletions src/app/select-item/select-item.component.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}