Reputation: 7110
I need to implement more correct relation between Product
and its Price
. I know that sometimes they use this declaration
class Product {
int id;
String name;
int price;
}
But as for me it is not good approach especially if product price can be changed and it is good to have a history of this change. For example user wants to increase the price of the product on some date in the future. Using the above class he lost the history. Yes he can change the price on the date but the product can be purcahges later so the real date of price increase will be incorrect (if take it from first Order
).
For now I have two ideas of how to design classes.
1 - This one is my favourite idea. All prices are in the separate table. The current price can be taken from it using productId
. The SQL view can be built basis on ProductPrice
and Product
tables to always have a current price. Maybe this way required more GUI, i.e. I need to
class Product {
int id;
String name;
}
class ProductPrice {
int id;
int productId;
int price;
DateTime startDate;
DateTime endDate;
}
Archive previous price
on product edit dialog).class Product {
int id;
String name;
int price;
}
class ProductPriceHistory {
int id;
int productId;
int price;
DateTime endDate;
}
I am glad to hear if am I right or where am I wrong?
P.S. Any other suggestions?
Upvotes: 0
Views: 440
Reputation: 29649
"Right" and "wrong" depend on the business domain, and pricing in real-world applications can be complex. For instance:
Your option 1 is "right" if:
Option 2 is "right" if:
Upvotes: 1