割引の適用
CoddyのDartジャーニー「論理とフロー」セクションの一部 — レッスン 36/65。
チャレンジ
簡単条件を満たす注文に対してパーセンテージベースの割引を適用する割引システムを実装し、ショッピングカート計算プロジェクトを継続するプログラムを作成してください。前回のレッスンの合計金額計算を基に、プログラムで以下のことを行います。
- 既存の製品カタログの
Mapと、前回のセットアップのショッピングカート機能を使用する - 顧客がカートに追加したいアイテムを表す複数の文字列入力を読み込む(入力は
"cart_done"を受け取ると終了する) - 各アイテムについて、
containsKey()を使用して製品カタログに存在するかどうかを確認し、有効なアイテムをカートに追加する - カート内のすべてのアイテムの価格を合計して小計を計算する
- 割引のしきい値を表す文字列入力を読み込む
- 適用する割引率を表す文字列入力を読み込む
- 小計がしきい値以上の場合のみ割引を適用する
- 割引額と、割引適用後の最終合計金額を計算する
- 割引計算レポートを以下に示す正確な形式で表示する
例えば、店に "Apple" が 1.50、"Banana" が 0.80、"Orange" が 2.00、"Milk" が 3.25 であり、顧客が "Apple"、"Orange"、"Milk"、"Apple" を追加したいとし、割引のしきい値が 5.00、割引率が 10 の場合、プログラムは次のように出力する必要があります。
Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Orange': Item added to cart successfully
Adding 'Milk': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Cart Contents: [Apple, Orange, Milk, Apple]
Cost Calculation:
Apple: $1.50
Orange: $2.00
Milk: $3.25
Apple: $1.50
Subtotal: $8.25
Discount Analysis:
Threshold: $5.00
Discount Rate: 10%
Subtotal ($8.25) meets threshold requirement
Discount Applied: $0.83
Final Total: $7.42
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Discount Status: 10% discount applied
Cart Status: Ready for checkout店に "Laptop" が 999.99、"Mouse" が 25.50、"Keyboard" が 75.00 であり、顧客が "Mouse"、"Keyboard" を追加したいとし、割引のしきい値が 150.00、割引率が 15 の場合、プログラムは次のように出力する必要があります。
Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Keyboard': Item added to cart successfully
Cart Contents: [Mouse, Keyboard]
Cost Calculation:
Mouse: $25.50
Keyboard: $75.00
Subtotal: $100.50
Discount Analysis:
Threshold: $150.00
Discount Rate: 15%
Subtotal ($100.50) does not meet threshold requirement
Discount Applied: $0.00
Final Total: $100.50
Cart Summary:
Total items in cart: 2
Unique products in cart: 2
Items successfully added: 2
Items not found: 0
Discount Status: No discount applied
Cart Status: Ready for checkout店に "Novel" が 12.99、"Magazine" が 4.50、"Textbook" が 89.00 であり、顧客が "Novel"、"Textbook"、"Magazine"、"Novel" を追加したいとし、割引のしきい値が 75.00、割引率が 20 の場合、プログラムは次のように出力する必要があります。
Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Textbook': Item added to cart successfully
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Cart Contents: [Novel, Textbook, Magazine, Novel]
Cost Calculation:
Novel: $12.99
Textbook: $89.00
Magazine: $4.50
Novel: $12.99
Subtotal: $119.48
Discount Analysis:
Threshold: $75.00
Discount Rate: 20%
Subtotal ($119.48) meets threshold requirement
Discount Applied: $23.90
Final Total: $95.58
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Discount Status: 20% discount applied
Cart Status: Ready for checkoutプログラムはまず小計を計算し、次に条件付き if 文を使用して、それが割引のしきい値以上であるかどうかを確認する必要があります。しきい値を満たしている場合は、小計に割引率を掛けて100で割ることで割引額を計算します。小計から割引額を引いて最終合計金額を求めます。すべての通貨値は、小数点以下ちょうど2桁を表示するようにフォーマットしてください。入力形式は、"cart_done" で終わるカートに追加するアイテム名、その後に割引のしきい値と割引率(どちらも適切な数値型に変換する必要がある文字列として)が続きます。
自分で試してみよう
import 'dart:io';
void main() {
// Product catalog with prices
Map<String, double> products = {
'Apple': 1.50,
'Banana': 0.80,
'Orange': 2.00,
'Milk': 3.25,
'Laptop': 999.99,
'Mouse': 25.50,
'Keyboard': 75.00,
'Novel': 12.99,
'Magazine': 4.50,
'Textbook': 89.00
};
// Shopping cart list
List<String> cart = [];
// Counters for summary
int itemsAdded = 0;
int itemsNotFound = 0;
print('Shopping Cart Operations:');
// Read items until "cart_done"
String? item;
while ((item = stdin.readLineSync()) != 'cart_done') {
if (item != null) {
if (products.containsKey(item)) {
cart.add(item);
print("Adding '$item': Item added to cart successfully");
itemsAdded++;
} else {
print("Adding '$item': Item not available in store");
itemsNotFound++;
}
}
}
// Display cart contents
print('Cart Contents: $cart');
// Calculate total cost
print('Cost Calculation:');
double totalCost = 0.0;
for (String cartItem in cart) {
double price = products[cartItem]!;
totalCost += price;
print('$cartItem: \$${price.toStringAsFixed(2)}');
}
print('Total Cost: \$${totalCost.toStringAsFixed(2)}');
// Cart summary
Set<String> uniqueProducts = cart.toSet();
print('Cart Summary:');
print('Total items in cart: ${cart.length}');
print('Unique products in cart: ${uniqueProducts.length}');
print('Items successfully added: $itemsAdded');
print('Items not found: $itemsNotFound');
print('Cart Status: Ready for checkout');
}論理とフローのすべてのレッスン
1リスト操作の応用
リストのプロパティ: first と lastリストの状態: isEmpty と isNotEmptyリストの反転リストへの追加: insertリストの削除: removeWhereリスト内の検索: indexOfリストのソートリストのシャッフルまとめ - リストオーガナイザー