保険契約モデル
Models/InsurancePolicy.cs
namespace InsuranceApp.Models
{
/// <summary>
/// 保険契約モデル
/// </summary>
public class InsurancePolicy
{
public string PolicyId { get; set; } // 契約ID
public string CustomerId { get; set; } // 契約者ID
public string PlanName { get; set; } // 商品名
public decimal Premium { get; set; } // 保険料(月額)
public decimal CoverageAmount { get; set; } // 保険金額
public DateTime StartDate { get; set; } // 契約開始日
public DateTime EndDate { get; set; } // 契約終了日
public bool IsActive { get; set; } // 契約有効フラグ
/// <summary>
/// 満期かどうかを判定
/// </summary>
public bool IsExpired() => DateTime.Now > EndDate;
}
}
IsExpired は「今日が有効期限を過ぎているか」を判定するプロパティです。
例:
• 今日 = 2025-08-18, 期限 = 2025-08-17 → IsExpired = true
• 今日 = 2025-08-18, 期限 = 2025-08-18 → IsExpired = false(同日なのでセーフ)
顧客情報モデル
Models/Customer.cs
namespace InsuranceApp.Models
{
/// <summary>
/// 顧客情報モデル
/// </summary>
public class Customer
{
public string CustomerId { get; set; }
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public string Email { get; set; }
/// <summary>
/// 年齢を計算
/// </summary>
public int GetAge()
{
var today = DateTime.Today;
int age = today.Year - BirthDate.Year;
if (BirthDate.Date > today.AddYears(-age)) age--;
return age;
}
}
}
誕生日がまだ来ていなければ1歳分引く処理
if (BirthDate.Date > today.AddYears(-age)) age--;
例:
• 今日が 2025/08/18
• 生年月日が 2000/12/01
age = 2025 - 2000; // 25歳 (仮)
次に判定👇
today.AddYears(-age) → 2025/08/18 - 25年 = 2000/08/18
比較すると:
• BirthDate = 2000/12/01
• 2000/12/01 > 2000/08/18 → true
つまり「誕生日(12/01)がまだ来ていない」ので age–;
→ 実際の年齢 = 24歳 🎯