0

Is there a way to make it so I can sort through an ArrayList output the highest number found along with the lowest number found? The way I have it now is a "shoppinglist" which is set up like this:

public void addToBag(String anyItem, int anyUnits, double anyCost){
    int checkUnits = anyUnits;
    double checkCost = anyCost;
    if(checkUnits < 0 || checkCost < 0){
        System.out.println("You must enter a value equal to or greater than 0!");
    }else{
    shoppingBag.add(new Item(anyItem, anyUnits, anyCost));
}
}

and the output which only outputs the list

  public void calculateSalesReceipt(){
    System.out.println("Enter the sales tax percentage (ex. 0.08 for 8%) or type \"random\" for a random number: ");
    double tax = keybd.nextDouble();
    if(tax < 0){
        System.out.println("You must enter a value equal to or greater than 0!");
    }else{
    getPricePreTax();
    total = total;
    taxCost = total * tax;
    finaltotal = total + taxCost;
    System.out.println("Sales Receipt");
    System.out.println("-------------");
    for(Item currentProduct : shoppingBag){
        System.out.println(currentProduct.getName() + " - " + currentProduct.getUnits() + " units " + " - $" + currentProduct.getCost());
    }
    System.out.println("Total cost: $" + total);
    System.out.println("Total tax: $" + taxCost);
    System.out.println("Total cost with tax: $" + finaltotal);
    System.out.println("Do you have any coupons? Enter \"yes\" or \"no\"");
    String answer = keybd.next();
    if(answer.equals("yes")){
                applyCoupon();
            }else if(answer.equals("no")){
            System.out.println("Thank you!");
        }else if(answer != "yes" || answer != "no"){
            System.out.println("Thank you!");
        }
    System.out.println("Coupon discounts: $" + couponAmount);
    System.out.println("Grand total: $" + (finaltotal-couponAmount));
}
}

Thanks!

EDIT:

Would this work?

 public void getLargest(){
    for(Item currentProduct : shoppingBag){
        System.out.println(Collections.max());
    }
}

3 Answers 3

5

You probably want Collections.max() (and Collections.min(), respectively). If you want the entire list in order, you probably want Collections.sort().

Sign up to request clarification or add additional context in comments.

1 Comment

public void getLargest(){ for(Item currentProduct : shoppingBag){ System.out.println(Collections.max()); } }
1

Using Google Guava:

Function<Item, Double> itemCostFunction = new Function<Item, Double>() {
  @Override public Double apply(Item item) {
    return item.getCost();
  }
};

List<Item> sortedItems = Ordering.natural().onResultOf(itemCostFunction).sortedCopy(shoppingBag);

Now

Item biggest = Iterables.getLast(sortedItems);  // costliest
Item smallest = sortedItems.get(0);  // cheapest 

2 Comments

...yes, thas really nice. But maybe as the guy is learning, its better to understand how to do it using the standard api. The last line looks a bit like magic... :-)
Why not use ordering.max / ordering.min?
0

What do you want to sort on? Cost?

You can create an new class; CostComparator implementing a Comparator<Item>, and compare Items depeding on their price.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.