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
6 changes: 3 additions & 3 deletions db.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
"id": 1,
"name": "Sprite",
"cost": 0.5,
"remaining": 10
"remaining": -2
},
{
"id": 2,
"name": "Coke",
"cost": 0.5,
"remaining": 10
"remaining": 5
},
{
"id": 3,
"name": "Dr. Pepper",
"cost": 0.75,
"remaining": 10
"remaining": 9
},
{
"id": 4,
Expand Down
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-item></app-dispense-item>
10 changes: 8 additions & 2 deletions 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 { DispenseItemComponent } from './dispense-item/dispense-item.component';

@NgModule({
declarations: [
AppComponent,
InsertCoinComponent
InsertCoinComponent,
SelectItemComponent,
DispenseItemComponent
],
imports: [
BrowserModule,
Expand All @@ -25,4 +29,6 @@ import { BalanceService } from './balance/balance.service';
],
bootstrap: [AppComponent]
})
export class AppModule { }
export class AppModule {

}
Empty file.
1 change: 1 addition & 0 deletions src/app/dispense-item/dispense-item.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button (click)='onDispenseItem()'>Dispense Item</button>
25 changes: 25 additions & 0 deletions src/app/dispense-item/dispense-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 { DispenseItemComponent } from './dispense-item.component';

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

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

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

it('should create', () => {
expect(component).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions src/app/dispense-item/dispense-item.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Component, OnInit } from '@angular/core';
import { ItemService } from '../item/item.service';
import { BalanceService } from '../balance/balance.service';

@Component({
selector: 'app-dispense-item',
templateUrl: './dispense-item.component.html',
styleUrls: ['./dispense-item.component.css']
})
export class DispenseItemComponent implements OnInit {
public items;
coinBalance = 0;
public selectedItem;
constructor(public itemService: ItemService, public balanceService: BalanceService) { }

ngOnInit() {
this.itemService.onItemsRetrieved((items) => this.items = items);
this.balanceService.onBalanceUpdated((balance) => {
this.coinBalance = balance;
});
}

onDispenseItem() {
this.selectedItem = this.itemService.getSelectedItem();
if (this.coinBalance >= this.selectedItem.cost && this.itemService.hasRemaining()) {
this.itemService.dispenseItem((item) => {
this.balanceService.deductBalance(item.cost);
this.balanceService.setBalance(0);
alert('Thank you for purchase!! Coins returned!');
});
} else if (!this.itemService.hasRemaining()) {
alert('SOLD OUT!!!');
}
else {
alert('Insufficient 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>
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!');
}

}
4 changes: 4 additions & 0 deletions src/app/select-item/select-item.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
li {
color: white;
list-style-type: none;
}
5 changes: 5 additions & 0 deletions src/app/select-item/select-item.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<li *ngFor='let item of items'> <!-- for loop -->
<input type='radio' [value]='item.id' name='item-list' (change)='onItemSelected(item)' >
<!-- <button (click)='onItemSelected(item)'>{{item.name}}</button> -->
{{item.name}}, {{item.cost}}
</li>
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();
});
});
21 changes: 21 additions & 0 deletions src/app/select-item/select-item.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 selectedItem;
constructor(public itemService: ItemService) { }

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

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