0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【保険商品管理システムの開発】生命保険のControllersを作成

Posted at

コード

Controllers/LifeInsuranceController.cs
using InsuranceApp.Models;

namespace InsuranceApp.Controllers
{
    /// <summary>
    /// 生命保険契約のビジネスロジックを担当するコントローラー
    /// </summary>
    public class LifeInsuranceController
    {
        private readonly List<InsurancePolicy> _policies = new();
        private readonly List<Customer> _customers = new();

        /// <summary>
        /// 顧客を登録
        /// </summary>
        public void RegisterCustomer(Customer customer)
        {
            _customers.Add(customer);
        }

        /// <summary>
        /// 保険契約を追加
        /// </summary>
        public void AddPolicy(InsurancePolicy policy)
        {
            _policies.Add(policy);
        }

        /// <summary>
        /// 特定の顧客の有効契約一覧を取得
        /// </summary>
        public List<InsurancePolicy> GetActivePoliciesByCustomer(string customerId)
        {
            return _policies
                .Where(p => p.CustomerId == customerId && p.IsActive && !p.IsExpired())
                .ToList();
        }

        /// <summary>
        /// 保険料の合計を算出
        /// </summary>
        public decimal CalculateTotalPremium(string customerId)
        {
            return _policies
                .Where(p => p.CustomerId == customerId && p.IsActive && !p.IsExpired())
                .Sum(p => p.Premium);
        }

        /// <summary>
        /// 保険金請求処理
        /// </summary>
        public decimal Claim(string policyId)
        {
            var policy = _policies.FirstOrDefault(p => p.PolicyId == policyId);
            if (policy == null || !policy.IsActive || policy.IsExpired())
            {
                throw new InvalidOperationException("保険契約が無効です。");
            }

            policy.IsActive = false; // 支払後は失効
            return policy.CoverageAmount;
        }
    }
}

変数名の前に_を付ける理由

    private readonly List<InsurancePolicy> _policies = new();
    private readonly List<Customer> _customers = new();

なぜ _ を付けるのか?
1. 可読性向上
フィールドだとすぐわかる。ローカル変数やメソッド引数と混同しない。
2. 規約・スタイルの一貫性
• Microsoft の公式ガイドラインでは _ を推奨していませんが
多くのチームやフレームワーク(特に ASP.NET Core の公式コードなど)では _ を採用しています。
• 例えば Entity Framework の内部コードや ASP.NET Core のサンプルコードも _dbContext のように _ を付けています。
3. IDE の補完時の整理
_ が付くことでフィールドがリストの上にまとまって表示されやすい(好みの問題)。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?