Product product = products.iterator().next(); assertEquals(productName, product.getProductName()); assertEquals(price, product.getPrice());
public class PriceSorter implements Comparator<Product>{ public int compare(Product one, Product another){ int returnVal = 0; if(one.getPrice() < another.getPrice()){ returnVal = -1; }else if(one.getPrice() > another.getPrice()){ returnVal = 1; }else if(one.getPrice() == another.getPrice()){ returnVal = 0; } return returnVal; } }
public class SortByPrice implements Comparator{ public int compare(Object o1, Object o2) { Product p1 = (Product) o1; Product p2 = (Product) o2; // return -1, 0, 1 to determine less than, equal to or greater than return (p1.getPrice() > p2.getPrice() ? 1 : (p1.getPrice() == p2.getPrice() ? 0 : -1)); // **or** the previous return statement can be simplified to: return p1.getPrice() - p2.getPrice(); } }
public int orderProducts (List<Product> products) { int price = 0; for (Product prod : products) { price += prod.getPrice(); } return price; }
class Product implements Comparable<Product>{ private static int count = 0; private int code; private String name; private String description; private Double price; @Override public int compareTo(Product p) { return price.compareTo(p.getPrice()); } }
Product[] products = // your products. Product mostExpensiveProduct = product[0]; for (Product product : products) { if (product.getPrice() > mostExpensiveProduct.getPrice()) { mostExpensiveProduct = product; } } System.out.println("Most expensive product is " + mostExpensiveProduct.getName() + " with price " + mostExpensiveProduct.getPrice());
protected void updateTally() { Product p1 = (Product) box1Prices.getSelectedItem(); Product p2 = (Product) box2Prices.getSelectedItem(); double price1 = p1 != null ? p1.getPrice() : 0d; double price2 = p2 != null ? p2.getPrice() : 0d; tallyLabel.setText(NumberFormat.getCurrencyInstance().format(price1 + price2)); }
BigDecimal calculateTotal() { BigDecimal total = new BigDecimal(0); for(Product eachProduct : products.keySet()) { total.add(eachProduct.getPrice() * products.get(eachProduct)); } return total; }
public void actionPerformed (ActionEvent e){ Product product = (Product)mainCheckoutList.getSelectedItem(); defaultMainList.addElement(product); runningTally += product.getPrice(); mainTillPrice.setText(NumberFormat.getCurrencyInstance().format(runningTally));
ImmutableList<Product> results = FluentIterable.from(products).filter(new Predicate<Product>() { @Override public boolean apply(Product input) { return input.getType() == ProductType.BANANA; } }).toSortedList(new Comparator<Product>() { @Override public int compare(Product o1, Product o2) { return o1.getPrice().compareTo(o2.getPrice()); } });
Criteria crt=session.createCriteria(Product.class); List<Product> l=crt.list(); for(Product p:l) { System.out.println(p.getPrice()); System.out.println(p.getProductId()); System.out.println(p.getProName()); }
public Optional<BigDecimal> getActualUnitPrice() { if (this.price != null) { return Optional.of(price); } else if (this.getParent().isPresent() && this.getParent().get().isLoaded()) { Purchasable parent = this.getParent().get().get(); if (!Product.class.isAssignableFrom(parent.getClass())) { throw new RuntimeException("Cannot handle a parent purchasable that is not a product"); } Product parentProduct = (Product) parent; return Optional.fromNullable(parentProduct.getPrice()); } return Optional.absent(); }