Reputation: 1993
I am having great difficulties with something very simple, and after trying to find an answer for two days, I need some help.
In my products database, I'm working with currencies. I would like to store all the product prices in cents. Everything is great up until this point, since I am also working with units. My products consist of groceries. So, for example, I have the following products which I can select from:
- Bottle of Coca Cola
- Can of Beer
- Pack of Milk
.. and so on
The units of the above products are usually in numbers, you can get 1 bottle of coke, or two, and so on.
Next to this, I have products like chicken, or steak. They measure in ounces, pounds, stones, etcetera. I would like to store the price of 1 gram for each product.
Now, I can add a field called units
in my database, and simply for each product add a unit. Like, for bottles of coke, I can add a unit called bottle
or simply piece
. And for the chicken, steak I could say gram
. Or I could use the numeric values, and for bottle I simply use 1
and for gram, probably 1000 for one kilogram, or 500 for pounds.
Then, to perform some calculations, I could simply do this, for each product, when displaying the data:
$total_price = $product->price * product->unit * product->amount / 100
The amount stands for the amount entered, for example, I want two bottles of coke, I enter 2
for the amount. Or, I want 700 grams of chicken, I enter the 700 simply. It does work, I guess. But it just doesn't feel right to me, it feels like there should be a better way to do this.
Another issue is, when displaying all the prices for each product. I ofcourse do not want to display them in cents, but rather in euros, so I simply divide the price per piece
with 100 and then display that price. So, for example, one bottle of coke costs 2 euros, then in my database the price
field for a bottle of coke is 200 (cents)
and when retrieving the price and displaying it, I divide it by 100 to display the proper 2 euros.
But for the chicken, steak and so on, I cannot do that. I would for example first need to calculate the price back from 1 gram to 1000 gram and then divide that price by 100 again to display the correct euros.
So, hopfully someone understands my issue here and any pointers in the right direction to help me find my answer will be greatly appreciated.
Upvotes: 0
Views: 145
Reputation: 13630
Not sure I understand your logic here, but it sounds like you could get a lot of this worked out by using well-thought-out Object Oriented Programming (OOP). Start with a base Product
- and extend that with WeightedProduct
and PackagedProduct
. Then implement your common logic inside Product
, and your specific logic in each of the respective extending classes (WeightedProduct
and PackagedProduct
).
class Product {
public function __construct () {}
}
class WeightedProduct extends Product {
$unit = 'piece';
public function __construct () {
parent::__construct();
}
protected function getTotal () {
// return the total based on WeightedProducts
}
}
class PackagedProduct extends Product {
$unit = 'gram';
public function __construct () {
parent::__construct();
}
protected function getTotal () {
// return the total based on PackagedProducts
}
}
Upvotes: 1