I am new in Application Design. I have use-case as below
As per the above story I have implemented code as below without any Modularity, Extensible and Maintainable. Could someone share the thoughts how to design below bad code to production grade application ?
Based on this story context, there could be other stories which could build-upon in later. Example: Introduce new customer types like Gold, Diamond, Platinum, etc and their discounts slabs.
public class ShoppingCartDiscount {
public static void main(String args[]) {
System.out.println("Price for the premium customer: " + calculatePrice("Premium", 20000));
System.out.println("Price for the regular customer: " + calculatePrice("Regular", 8000));
}
static float calculatePrice(String customerType, float purchaseAmount) {
float total = 0;
if (customerType.equalsIgnoreCase("Regular") {
if (purchaseAmount > 5000 && purchaseAmount <= 10000) {
float firstSlab = purchaseAmount - 5000;
firstSlab = firstSlab - (float) (firstSlab * 0.1);
total = 5000 + firstSlab;
}
else if (purchaseAmount > 10000) {
float secondSlab = purchaseAmount - 10000;
secondSlab = secondSlab - (float) (secondSlab * 0.2);
float firstSlab = 10000 - 5000;
firstSlab = firstSlab - (float) (firstSlab * 0.1);
System.out.println("firstSlab:" + firstSlab);
total = 5000 + firstSlab;
// 4500 + 800 + No slab
total = total + secondSlab;
} else if (purchaseAmount <= 5000 && purchaseAmount >= 0) {
total = purchaseAmount;
} else {
return purchaseAmount;
}
} else if (customerType.equalsIgnoreCase("premium")) {
if (purchaseAmount <= 4000) {
total = purchaseAmount - (float) (purchaseAmount * 0.1);
}
if (purchaseAmount > 4000 && purchaseAmount <= 8000) {
float secondSlab = purchaseAmount - 4000;
secondSlab = secondSlab - (float) (secondSlab * 0.15);
float firstSlab = 8000 - 4000;
total = firstSlab - (float) (firstSlab * 0.1);
total = total + secondSlab;
}
if (purchaseAmount > 8000 && purchaseAmount <= 12000) {
float thirdSlab = purchaseAmount - 8000;
thirdSlab = thirdSlab - (float) (thirdSlab * 0.20);
float secondSlab = 8000 - 4000;
secondSlab = secondSlab - (float) (secondSlab * 0.15);
float firstSlab = 8000 - 4000;
total = firstSlab - (float) (firstSlab * 0.1);
total = total + secondSlab + thirdSlab;
}
if (purchaseAmount > 12000) {
float fourthSlab = purchaseAmount - 12000;
fourthSlab = fourthSlab - (float) (fourthSlab * 0.30);
float thirdSlab = 8000 - 4000;
thirdSlab = thirdSlab - (float) (thirdSlab * 0.20);
float secondSlab = 8000 - 4000;
secondSlab = secondSlab - (float) (secondSlab * 0.15);
float firstSlab = 8000 - 4000;
total = firstSlab - (float) (firstSlab * 0.1);
total = total + secondSlab + thirdSlab + fourthSlab;
}
}
return total;
}
}
Aucun commentaire:
Enregistrer un commentaire