Menu
Coddy logo textTech

すべてを組み合わせる

CoddyのRustジャーニー「Logic & Flow」セクションの一部 — レッスン 53/66。

challenge icon

チャレンジ

簡単

6つの入力を受け取ります。最初の入力は、addcheck、または sell のいずれかであるコマンドです。2番目の入力はアイテム名です。3番目の入力は、販売する数量(sell コマンドの場合)または初期価格(add コマンドの場合)を表す数値です。4番目の入力は、在庫にあるアイテム名のカンマ区切りリストです。5番目の入力は、各アイテムに対応する価格のカンマ区切りリストです。6番目の入力は、各アイテムに対応する数量のカンマ区切りリストです。

アイテムの追加、在庫の確認、アイテムの販売という3つの操作すべてを処理する完全な在庫管理システムを構築してください。HashMap を使用して在庫データを保存し、キーをアイテム名、値を価格と数量を含むタプル (f64, i32) とします。

要件:

  • std::collections から HashMap をインポートする
  • 最初の入力(コマンド)を読み取り、空白をトリミングする
  • 2番目の入力(アイテム名)を読み取り、空白をトリミングする
  • 3番目の入力(数量/価格の値)を読み取り、空白をトリミングする
  • 3番目の入力を f64 にパースする
  • 4番目の入力(カンマ区切りのアイテム名)を読み取り、空白をトリミングする
  • 5番目の入力(カンマ区切りの価格)を読み取り、空白をトリミングする
  • 6番目の入力(カンマ区切りの数量)を読み取り、空白をトリミングする
  • 在庫用にミュータブルな HashMap<String, (f64, i32)> を作成する
  • アイテム名をカンマで分割する
  • 価格をカンマで分割し、それぞれを f64 にパースする
  • 数量をカンマで分割し、それぞれを i32 にパースする
  • 各アイテムを、対応する価格と数量のタプルとともに在庫に挿入する
  • コマンドに対して match 式を使用し、実行する操作を決定する
  • add コマンドの場合:
    • .entry().or_insert() を使用して、アイテムが存在しない場合は追加し、3番目の入力を価格として、0 を初期数量として使用する
    • 次のように出力する: Added [item_name] to inventory
    • 次のように出力する: Price: $[price]
    • 次のように出力する: Quantity: 0
  • check コマンドの場合:
    • .get() を使用してアイテムが存在するか確認する
    • match 式を使用して Option を処理する
    • Some の場合、次のように出力する:
      • [item_name] is in stock
      • Price: $[price]
      • Quantity: [quantity]
    • None の場合、次のように出力する: [item_name] is not in stock
  • sell コマンドの場合:
    • 3番目の入力を販売数量として i32 にパースする
    • .get() を使用してアイテムが存在するか確認する
    • match 式を使用して Option を処理する
    • Some の場合:
      • 現在の数量が十分か確認する
      • 十分な場合、在庫を新しい数量で更新し、次のように出力する:
        • Sold [quantity_to_sell] [item_name]
        • Total: $[total_price]
        • Remaining stock: [new_quantity]
      • 不十分な場合、次のように出力する:
        • Insufficient stock for [item_name]
        • Available: [current_quantity]
    • None の場合、次のように出力する: [item_name] is not in stock
  • その他のコマンドの場合、次のように出力する: Invalid command

入力:

  • 1行目: コマンド (add, check, または sell)
  • 2行目: アイテム名 (例: Tablet)
  • 3行目: 数値 (add の場合は価格、sell の場合は数量、check の場合は無視される)
  • 4行目: カンマ区切りのアイテム名 (例: Laptop,Mouse,Keyboard)
  • 5行目: カンマ区切りの価格 (例: 999.99,25.50,75.00)
  • 6行目: カンマ区切りの数量 (例: 15,50,30)

出力:

  • add コマンドの場合:
    • Added [item_name] to inventory
    • Price: $[price]
    • Quantity: 0
  • check コマンドの場合 (アイテムが存在する):
    • [item_name] is in stock
    • Price: $[price]
    • Quantity: [quantity]
  • check コマンドの場合 (アイテムが存在しない): [item_name] is not in stock
  • sell コマンドの場合 (在庫が十分):
    • Sold [quantity_to_sell] [item_name]
    • Total: $[total_price]
    • Remaining stock: [new_quantity]
  • sell コマンドの場合 (在庫不足):
    • Insufficient stock for [item_name]
    • Available: [current_quantity]
  • sell コマンドの場合 (アイテムが存在しない): [item_name] is not in stock
  • 無効なコマンドの場合: Invalid command

自分で試してみよう

use std::collections::HashMap;
use std::io;

fn main() {
    // 入力を読み込む
    let mut command = String::new();
    io::stdin().read_line(&mut command).expect("Failed to read line");
    let command = command.trim();
    
    let mut item_name = String::new();
    io::stdin().read_line(&mut item_name).expect("Failed to read line");
    let item_name = item_name.trim();
    
    let mut value_input = String::new();
    io::stdin().read_line(&mut value_input).expect("Failed to read line");
    let value_input = value_input.trim();
    
    let mut items_input = String::new();
    io::stdin().read_line(&mut items_input).expect("Failed to read line");
    let items_input = items_input.trim();
    
    let mut prices_input = String::new();
    io::stdin().read_line(&mut prices_input).expect("Failed to read line");
    let prices_input = prices_input.trim();
    
    let mut quantities_input = String::new();
    io::stdin().read_line(&mut quantities_input).expect("Failed to read line");
    let quantities_input = quantities_input.trim();
    
    // 在庫のHashMapを作成する
    let mut inventory: HashMap<String, (f64, i32)> = HashMap::new();
    
    // TODO: 以下にコードを記述してください
    // カンマ区切りの入力を解析し、在庫データを格納する
    // match式を使用して、add、check、sellの各操作を処理する
    // コマンドに基づいて適切なメッセージを出力することを忘れないでください
    
}

Logic & Flowのすべてのレッスン