Студопедия

Главная страница Случайная страница

Разделы сайта

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Public void addItem(String title, double price, int quantity, ItemType type)






{

if (title == null || title.length() == 0 || title.length() > 32)

throw new IllegalArgumentException(" Illegal title");

if (price < 0.01)

throw new IllegalArgumentException(" Illegal price");

if (quantity < = 0)

throw new IllegalArgumentException(" Illegal quantity");

Item item = new Item();

item.title = title;

item.price = price;

item.quantity = quantity;

item.type = type;

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 $100.00 1 50% $50.00

*...

* 31 Item 42 $999.00 1000 - $999000.00

* end line: ---------------------------------------------------------

* last line: 31 $999050.60

*

* if no items in cart returns " No items." string.

*/

Public String formatTicket()

{

if (items.size() == 0)

return " No items.";

List< String[]> lines = new ArrayList< String[]> ();

String[] header = {" #", " Item", " Price", " Quan.", " Discount", " Total" };

int[] align = new int[] { 1, -1, 1, 1, 1, 1 };

// formatting each line

double total = 0.00;

int index = 0;

for (Item item: items) {

int discount = calculateDiscount(item.type, item.quantity);

double itemTotal = item.price * item.quantity * (100.00 - discount) / 100.00;

lines.add(new String[]{

String.valueOf(++index),

Item.title,

MONEY.format(item.price),

String.valueOf(item.quantity),

(discount == 0)? " -": (String.valueOf(discount) + " %"),

MONEY.format(itemTotal)

});

total += itemTotal;

}

String[] footer = { String.valueOf(index), " ", " ", " ", " ",

MONEY.format(total) };

// formatting table

// column max length

int[] width = new int[]{0, 0, 0, 0, 0, 0};

for (String[] line: lines)

for (int i = 0; i < line.length; i++)

width[i] = (int) Math.max(width[i], line[i].length());

for (int i = 0; i < header.length; i++)

width[i] = (int) Math.max(width[i], header[i].length());

for (int i = 0; i < footer.length; i++)

width[i] = (int) Math.max(width[i], footer[i].length());

// line length

int lineLength = width.length - 1;

for (int w: width)

lineLength += w;

StringBuilder sb = new StringBuilder();

// header

for (int i = 0; i < header.length; i++)

appendFormatted(sb, header[i], align[i], width[i]);

sb.append(" \n");

// separator

for (int i = 0; i < lineLength; i++)

sb.append(" -");

sb.append(" \n");

// lines

for (String[] line: lines) {

for (int i = 0; i < line.length; i++)

appendFormatted(sb, line[i], align[i], width[i]);

sb.append(" \n");

}

if (lines.size() > 0) {

// separator

for (int i = 0; i < lineLength; i++)

sb.append(" -");

sb.append(" \n");

}

// footer

for (int i = 0; i < footer.length; i++)

appendFormatted(sb, footer[i], align[i], width[i]);

Return sb.toString();

}

// --- private section -----------------------------------------------------






© 2023 :: MyLektsii.ru :: Мои Лекции
Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав.
Копирование текстов разрешено только с указанием индексируемой ссылки на источник.