Skip to content

Commit

Permalink
Now prints prices. Magic numbers everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanTihanyi committed Feb 22, 2016
1 parent deb14eb commit c04e1e9
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Prices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Loc = Can
400 < W < 500 $5.05
Loc = US
Standard
W < 30 $1.80
30 < W < 50 $1.20
W < 30 $1.20
30 < W < 50 $1.80
Non-Standard
W < 100 $2.95
100 < W < 200 $5.15
Expand Down
29 changes: 28 additions & 1 deletion app/pages/item-types/item-types.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,32 @@
</ion-navbar>

<ion-content padding class="item-types">

<ion-card>
<ion-card-header text-center>
Please enter information about your package (heh)
</ion-card-header>
<ion-list>
<form #dimForm="ngForm" novalidate>
<ion-item>
<ion-label floating>Length (mm)</ion-label>
<ion-input [(ngModel)]="dimensions.length" ngControl="length" #length="ngForm" type="text" required></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Width (mm)</ion-label>
<ion-input [(ngModel)]="dimensions.width" ngControl="width" #width="ngForm" type="text" required></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Thickness (mm)</ion-label>
<ion-input [(ngModel)]="dimensions.thickness" ngControl="thickness" #thickness="ngForm" type="text" required></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Weight (g)</ion-label>
<ion-input [(ngModel)]="dimensions.weight" ngControl="weight" #weight="ngForm" type="text" required></ion-input>
</ion-item>
</form>
</ion-list>
</ion-card>
<div padding>
<button block secondary (click)="calculatePrice(dimForm)">Continue</button>
</div>
</ion-content>
41 changes: 40 additions & 1 deletion app/pages/item-types/item-types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Page, NavController} from 'ionic/ionic';
import {Page, NavController, Alert} from 'ionic/ionic';
import {PostalRateServices} from '../../providers/postal-rate-services/postal-rate-services'


Expand All @@ -14,6 +14,45 @@ import {PostalRateServices} from '../../providers/postal-rate-services/postal-ra
export class ItemTypesPage {
constructor(nav:NavController, dataServices: PostalRateServices) {
this.nav = nav;
this.service = dataServices;
this.data = dataServices.dest;
this.dimensions = {
length: 0,
thickness: 0,
width: 0,
weight: 0,
}
this.price = 0;
this.isStandard = true;
this.error = '';
}


calculatePrice(form) {
if (form.valid) {
this.error = this.service.getError(this.dimensions);
if (this.error === '') {
this.isStandard = this.service.getStandard(this.dimensions);
this.price = this.service.getPrice(this.dimensions, this.data, this.isStandard);
let alert = Alert.create({
title: 'PRICE',
subTitle: 'The price to ship your package is $' + this.price.toFixed(2) + '.',
buttons: ['Ok']
});
this.nav.present(alert);
} else {
let alert = Alert.create({
subTitle: this.error,
buttons: ['Ok']
});
this.nav.present(alert);
}
} else {
let alert = Alert.create({
subTitle: 'Please fill in all fields!',
buttons: ['Ok']
});
this.nav.present(alert);
}
}
}
2 changes: 1 addition & 1 deletion app/pages/postage-destination/postage-destination.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h3>(A Mobile Postal Rate Calculator)</h3>
</ion-card-content>
</ion-card>
<ion-card>
<ion-card-header>
<ion-card-header text-center>
Where would you like to send a package?
</ion-card-header>
<ion-list>
Expand Down
2 changes: 1 addition & 1 deletion app/pages/postage-destination/postage-destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export class PostageDestinationPage {
}
setDestination(dest) {
this.service.setDest(dest);
this.nav.push(ItemTypesPage, {dest: dest, name: name});
this.nav.push(ItemTypesPage, {dest: dest});
}
}
65 changes: 58 additions & 7 deletions app/providers/postal-rate-services/postal-rate-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,66 @@ export class PostalRateServices {
setDest(dest) {
this.dest = dest;
};
//
//L < 140 Error
//W < 90 Error
//T < 0.18 Error
//We < 2 Error
//L > 380 Error
//W > 270 Error
//T > 20 Error
//We > 500 Error

getPrice(item, weight) {
if (weight < 0) return 0;
for(var i = 0; i < item.length; i++) {
if(weight > item[i].minWeight && weight <= item[i].maxWeight) return item[i].price;
}
return 0;
getError(dim) {
if (dim.length < 140) return 'Length must be larger than 140 mm.';
if (dim.width < 90) return 'Width must be larger than 90 mm.';
if (dim.thickness < 0.18) return 'Thickness must be larger than 0.18 mm.';
if (dim.weight < 2) return 'Weight must be larger than 2 g.';
if (dim.length > 380) return 'Length must be smaller than 380 mm.';
if (dim.width > 270) return 'Width must be smaller than 270 mm.';
if (dim.thickness > 20) return 'Length must be smaller than 20 mm.';
if (dim.weight > 500) return 'Weight must be smaller than 500 g.';
else return '';
}

};
getStandard(dim) {
if (140 < dim.length && dim.length < 245 && 90 < dim.width && dim.width < 156
&& 0.18 < dim.thickness && dim.thickness < 5 && 2 < dim.weight && dim.weight < 50) return true;
else return false;
}

getPrice(dim, data, isStandard) {
if (data.name === 'Canada') {
if (isStandard) {
if (dim.weight < 30) return 0.85;
else return 1.2;
} else {
if (dim.weight < 100) return 1.8;
else if (100 < dim.weight && dim.weight < 200) return 2.95;
else if (200 < dim.weight && dim.weight < 300) return 4.1;
else if (300 < dim.weight && dim.weight < 400) return 4.7;
else return 5.05;
}
} else if (data.name === 'USA') {
if (isStandard) {
if (dim.weight < 30) return 1.2;
else return 1.8;
} else {
if (dim.weight < 100) return 2.95;
else if (100 < dim.weight && dim.weight < 200) return 5.15;
else return 10.3;
}
} else if (data.name === 'USA') {
if (isStandard) {
if (dim.weight < 30) return 2.5;
else return 3.6;
} else {
if (dim.weight < 100) return 5.9;
else if (100 < dim.weight && dim.weight < 200) return 10.3;
else return 20.6;
}
}
return 0;
}
}

0 comments on commit c04e1e9

Please sign in to comment.