Главная страница Случайная страница КАТЕГОРИИ: АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника |
Items.add(item);
} /** * Formats shopping price. * * @return string as lines, separated with \n, * first line: # Item Price Quan. Discount Total * second line: --------------------------------------------------------- * next lines: NN Title $PP.PP Q DD% $TT.TT * 1 Some title $.30 2 - $.60 * 2 Some very long ti... $100.00 1 50% $50.00 *... * 31 Item 42 $999.00 1000 - $999000.00 * end line: --------------------------------------------------------- * last line: 31 $999050.60 * * Item title is trimmed to 20 chars adding '...' * * if no items in cart returns " No items." string. */ Public String toString() { StringBuffer sb = new StringBuffer(); if (items.size() == 0) return " No items."; double total = 0.00; sb.append(" # Item Price Quan. Discount Total\n"); sb.append(" ---------------------------------------------------------\n"); for (int i = 0; i < items.size(); i++) { Item item = (Item) items.get(i); int discount = calculateDiscount(item); double itemTotal = item.price * item.quantity * (100.00 - discount) / 100.00; appendPaddedRight(sb, String.valueOf(i + 1), 2); sb.append(" "); AppendPaddedLeft(sb, item.title, 20); sb.append(" "); AppendPaddedRight(sb, MONEY.format(item.price), 7); sb.append(" "); AppendPaddedRight(sb, String.valueOf(item.quantity), 4); sb.append(" "); if (discount == 0) sb.append(" -"); else { AppendPaddedRight(sb, String.valueOf(discount), 7); sb.append(" %"); } sb.append(" "); AppendPaddedRight(sb, MONEY.format(itemTotal), 10); sb.append(" \n"); total += itemTotal; } sb.append(" ---------------------------------------------------------\n"); AppendPaddedRight(sb, String.valueOf(items.size()), 2); sb.append(" "); AppendPaddedRight(sb, MONEY.format(total), 10); Return sb.toString(); } // --- private section ----------------------------------------------------- Private static final NumberFormat MONEY; static { DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator('.'); MONEY = new DecimalFormat(" $#.00", symbols); } /** * Adds to string buffer given string, padded with spaces. * @return " str".length() == width */ Private static void appendPaddedRight(StringBuffer sb, String str, int width) { for (int i = str.length(); i < width; i++) sb.append(" "); Sb.append(str); } /** * Adds string to buffer, wills spaces to width. * If string is longer than width it is trimmed and ends with '...' */ Private static void appendPaddedLeft(StringBuffer sb, String str, int width) { if (str.length() > width) { Sb.append(str.substring(0, width-3)); sb.append("..."); } else { Sb.append(str); for (int i = str.length(); i < width; i++) sb.append(" "); } } /** * Calculates item's discount. * For Item.Type.REGULAR discount is 0%; * For Item.Type.SECOND discount is 50% if quantity > 1 * For Item.Type.DISCOUNT discount is 10% + 10% for each full 10 items, but not more than 50% * total * For Item.Type.SALE discount is 90% * For each full 100 items item gets additional 10%, but not more than 80% total */ Private static int calculateDiscount(Item item) { int discount = 0; switch (item.type) { case Item.Type.SECOND: if (item.quantity > 1) discount = 50; Break; case Item.Type.DISCOUNT: discount = 10 + item.quantity / 10 * 10; if (discount > 50) discount = 50; Break; case Item.Type.SALE: discount = 90; } discount += item.quantity / 100 * 10; if (discount > 80) discount = 80;
|