HiveBrain v1.2.0
Get Started
← Back to all entries
principlejavaMinor

compare two logic commented one to my logic java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
commentedlogicjavaonetwocompare

Problem

I'm trying to review this block of code to make sure logic is fine. The first version that I commented will create duplicated order 'my college developer' but I fixed it but "really not sure" I want to make sure my fix it exactly same as commented code

the old logic "this code include duplicated"

public void setOrderedProduct(String prodName, int eoq) {
    String productCd = getProdCd(prodName);
    // Here only one time one product
    boolean prodPresent = false;
    int indexOfProd = 0;
    int sizeOfItems = items.size();
    for (int i = 0; i  0) {
            items.set(indexOfProd, itemData);
            ProductView.updatePurchaseDetails(itemData, vendorno);
        } else {
            items.remove(indexOfProd);
            ProductView.removePurchaseDetails(itemData, vendorno);
        }
    }
    if (items.size() > 0)
        calculateTotal();
}


my fixed logic

```
public void setOrderedProduct(String prodName, int eoq) {
boolean dataPresent = false;
if (orderedProdNameList.contains((Object) prodName)) {
dataPresent = true;
}
if (dataPresent) {
int indexOfProdName = orderedProdNameList.indexOf((Object) prodName);
String productCd = getProdCd(prodName);
// Here only one time one product
PurchaseOrderDTO itemData = new PurchaseOrderDTO();

boolean prodPresent = false;
itemData.setProdname(prodName);
itemData.setQty(eoq);
itemData.setProdcd(productCd);
double costPrice = ProductView.getProductCost(vendorno, productCd);
itemData.setPrice(costPrice);
double extPrice = ProductView.getProductMSRP(vendorno, productCd);
itemData.setExt(extPrice);
// Now add the details to the database
ProductView.addPurchaseDetails(itemData, vendorno);
if (eoq > 0) {
items.set(indexOfProdName, itemData);
ProductView.updatePurchaseDetails(itemData, vendorno);
} else if (eoq == 0) {

Solution

Not sure if your fixed logic actually works. But I see couple of issues with the code.

-
both if and else block is setting a boolean. But I don't find any use of it in your code. boolean prodPresent = false


-
How do you get access to items ? is it instance variable ?

-
The if and else block has lot of common code which can be written outside of the condition.

Here I am not concerned with your logic but tried to organize the second version of the code which you posted. My intention is to avoid repetitive code in both the if-else block

public void setOrderedProduct(String prodName, int eoq) {
PurchaseOrderDTO itemData = new PurchaseOrderDTO();
String productCd = getProdCd(prodName);

itemData.setProdname(prodName);
itemData.setQty(eoq);
itemData.setProdcd(productCd);

double costPrice = ProductView.getProductCost(vendorno, productCd);
itemData.setPrice(costPrice);

double extPrice = ProductView.getProductMSRP(vendorno, productCd);
itemData.setExt(extPrice);

if (orderedProdNameList.contains((Object) prodName)) {
  int indexOfProdName = orderedProdNameList.indexOf((Object) prodName);

  ProductView.addPurchaseDetails(itemData, vendorno);
  if (eoq > 0) {
    items.set(indexOfProdName, itemData);
    ProductView.updatePurchaseDetails(itemData, vendorno);
  } else if (eoq == 0) {
    for (int i = 0; i  0) { calculateTotal(); }
}

Code Snippets

public void setOrderedProduct(String prodName, int eoq) {
PurchaseOrderDTO itemData = new PurchaseOrderDTO();
String productCd = getProdCd(prodName);

itemData.setProdname(prodName);
itemData.setQty(eoq);
itemData.setProdcd(productCd);

double costPrice = ProductView.getProductCost(vendorno, productCd);
itemData.setPrice(costPrice);

double extPrice = ProductView.getProductMSRP(vendorno, productCd);
itemData.setExt(extPrice);

if (orderedProdNameList.contains((Object) prodName)) {
  int indexOfProdName = orderedProdNameList.indexOf((Object) prodName);

  ProductView.addPurchaseDetails(itemData, vendorno);
  if (eoq > 0) {
    items.set(indexOfProdName, itemData);
    ProductView.updatePurchaseDetails(itemData, vendorno);
  } else if (eoq == 0) {
    for (int i = 0; i < items.size(); i++) {
      if (items.get(i).getProdname().equalsIgnoreCase(prodName)) {
        items.remove(i);
        ProductView.removePurchaseDetails(itemData, vendorno);
      }
    }
  }

} else {
  orderedProdNameList.add(prodName);
  items.add(items.size(), itemData);
  ProductView.addPurchaseDetails(itemData, vendorno);
}
 if (items.size() > 0) { calculateTotal(); }
}

Context

StackExchange Code Review Q#32987, answer score: 4

Revisions (0)

No revisions yet.