Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Smell: Long Method in "BillyBurger" #5

Open
DJavierReyesM opened this issue Jan 16, 2021 · 0 comments
Open

Code Smell: Long Method in "BillyBurger" #5

DJavierReyesM opened this issue Jan 16, 2021 · 0 comments

Comments

@DJavierReyesM
Copy link

DJavierReyesM commented Jan 16, 2021

Hello again. I was checking something, and notice something: In the Hamburger class (where the code smell Data Clumps was reviewed) there is a method called itemisedHamburger (), which allows to calculate the total price to pay for the hamburger and its extras. The way in which the method to calculate the total to pay is implemented tends to have a method with very long code which reuses code in a bad way by having to validate each of the additional ones. You can refactor your code by ussing Substitute Algorithm. With this technique, the size of the code would be greatly reduced by not having to reuse code in a bad way through validations through if blocks for each of the additional ones, avoiding repeating the code unnecessarily. To consolidate the change in the first instance, a field is added as a list that will allow storing the additional ones and in turn the number of additional ones is deleted along with their methods to only leave the aforementioned list and an addAditional () method is added so additional ones are added to the list, thus avoiding having many methods with similar algorithms:

The field for the additionals (Don't forget to initializate it) and my proppose for the code:

  private ArrayList<HamburgerAdditional>listAdditional;
  public void addAddition(HamburgerAdditional addition) {
	if(this.listAdditional.size()>3) {
		System.out.println("The Standard Hamburguer can only have 4 additionals");
	}
	else {	
    this.listAdditional.add(addition);
	}
}

public double itemisedHamburger() {
    double subTotal = this.basePrice;
    System.out.println("Total price of "+ this.name+ " burger made from "
            + this.breadRollType+ " and "+ this.meat+ ":");
    System.out.println("base price: " + this.basePrice);
 
    for(HamburgerAdditional x: this.listAdditional) {
    	if(x != null) {
    		subTotal += x.getPrice();
            System.out.println("additional " + x.getName()+ ": "+ x.getPrice()
                    + " subtotal: "+ (double) Math.round(subTotal * 100) / 100);		
    	}  	
    }
    return Math.round(subTotal * 100)/100;

}

Have a nice day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant