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: 1 addition & 1 deletion db.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"id": 4,
"name": "Mr Pibbs",
"cost": 0.5,
"remaining": 10
"remaining": 9
}
]
}
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></app-dispense>
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 { DispenseComponent } from './dispense/dispense.component';

@NgModule({
declarations: [
AppComponent,
InsertCoinComponent
InsertCoinComponent,
SelectItemComponent,
DispenseComponent
],
imports: [
BrowserModule,
Expand Down
Empty file.
1 change: 1 addition & 0 deletions src/app/dispense/dispense.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button (click)='onItemDispensed()'>Dispense</button>
25 changes: 25 additions & 0 deletions src/app/dispense/dispense.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 { DispenseComponent } from './dispense.component';

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

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

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

it('should create', () => {
expect(component).toBeTruthy();
});
});
43 changes: 43 additions & 0 deletions src/app/dispense/dispense.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-dispense',
templateUrl: './dispense.component.html',
styleUrls: ['./dispense.component.css']
})
export class DispenseComponent implements OnInit {
public dispense;

constructor(public itemService: ItemService, public balanceService: BalanceService) {
}

ngOnInit() {
}

onItemDispensed(callback) {
// if the price of the item is equal or less than the balance, AND there is inventory
if((this.itemService.getSelectedItem() <= this.balanceService.getBalance() === true) && (this.itemService.hasRemaining() === true)) {
// then dispence
this.itemService.dispenseItem(callback);
// then update
let diff = this.itemService.getSelectedItem() - this.balanceService.getBalance()
this.balanceService.onBalanceUpdated(diff);
// inform user
alert('Here you go');
} else {
// if it cost more
if (this.itemService.getSelectedItem() > this.balanceService.getBalance()) {
// alert user
alert('Insufficient Balance');
} else {
// otherwise it means there are no more.
alert('Sold out');
}
}

}


}
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>
5 changes: 5 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,9 @@ export class InsertCoinComponent implements OnInit {
this.balanceService.addBalance(amount);
}

returnCoins() {
this.balanceService.setBalance(0);
alert('Coins returned');
}

}
Empty file.
13 changes: 13 additions & 0 deletions src/app/select-item/select-item.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<p>
select-item works!
</p>
<ul>
<li *ngFor='let item of items'>
<input type='radio' [value]='item.id' name='item-list' (change)='onItemSelected(item)'>
{{item.name}}
{{item.cost}}
<!-- <button (click)='onItemSelected(item)'>{{item.name}}</button> -->
</li>
</ul>
<!-- <p>{{title}}</p>
<input [(ngModel)]="title" type='text'> -->
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();
});
});
31 changes: 31 additions & 0 deletions src/app/select-item/select-item.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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 title = 'james is the greatest';
// items = [
// {
// name: 'max',
// cost: 0.5
// }
// ];
constructor(public itemService: ItemService) {
}

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

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

}