2018年1月5日金曜日

開発環境

初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)日向 俊二 (翻訳)、オライリージャパン)の12章(演算子のオーバーロード)、12.6(練習問題)、練習12-2.を取り組んでみる。

コード

using System;

namespace Sample12_2
{
    class Invoice
    {
        private string vendor;
        private double amount;
        public Invoice(string vendor, double amount)
        {
            this.vendor = vendor;
            this.amount = amount;
        }

        public static Invoice operator +(Invoice lhs, Invoice rhs)
        {
            if (lhs.vendor == rhs.vendor)
            {
                return new Invoice(lhs.vendor, lhs.amount + rhs.amount);
            }
            return new Invoice("", 0);
        }
        public static bool operator ==(Invoice lhs, Invoice rhs)
        {
            if (lhs.vendor == rhs.vendor && lhs.amount.Equals(rhs.amount))
            {
                return true;
            }
            return false;
        }
        public static bool operator !=(Invoice lhs, Invoice rhs)
        {
            return !(lhs == rhs);
        }
        public override bool Equals(object obj)
        {
            if (!(obj is Invoice)) {
                return false;
            }
            return this == (Invoice)obj;
        }
        public override string ToString()
        {
            return string.Format("Vendor: {0}, Amount: {1}", vendor, amount);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Invoice i1 = new Invoice("vendor1", 10);
            Invoice i2 = new Invoice("vendor1", 20);
            Invoice i3 = new Invoice("vendor1", 10);
            Invoice i4 = new Invoice("vendor2", 10);
            Invoice i5 = new Invoice("vendor2", 20);
            Invoice i6 = new Invoice("vendor2", 10); 
            Invoice[] invoices = { i1, i2, i3, i4, i5, i6};

            foreach (var item1 in invoices)
            {
                Console.WriteLine(item1);
                foreach (var item2 in invoices)
                {
                    Console.WriteLine("\t{0}: {1}", item2, item1 == item2);
                }
            }
        }
    }
}

入出力結果(Terminal)

Vendor: vendor1, Amount: 10
 Vendor: vendor1, Amount: 10: True
 Vendor: vendor1, Amount: 20: False
 Vendor: vendor1, Amount: 10: True
 Vendor: vendor2, Amount: 10: False
 Vendor: vendor2, Amount: 20: False
 Vendor: vendor2, Amount: 10: False
Vendor: vendor1, Amount: 20
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor1, Amount: 20: True
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor2, Amount: 10: False
 Vendor: vendor2, Amount: 20: False
 Vendor: vendor2, Amount: 10: False
Vendor: vendor1, Amount: 10
 Vendor: vendor1, Amount: 10: True
 Vendor: vendor1, Amount: 20: False
 Vendor: vendor1, Amount: 10: True
 Vendor: vendor2, Amount: 10: False
 Vendor: vendor2, Amount: 20: False
 Vendor: vendor2, Amount: 10: False
Vendor: vendor2, Amount: 10
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor1, Amount: 20: False
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor2, Amount: 10: True
 Vendor: vendor2, Amount: 20: False
 Vendor: vendor2, Amount: 10: True
Vendor: vendor2, Amount: 20
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor1, Amount: 20: False
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor2, Amount: 10: False
 Vendor: vendor2, Amount: 20: True
 Vendor: vendor2, Amount: 10: False
Vendor: vendor2, Amount: 10
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor1, Amount: 20: False
 Vendor: vendor1, Amount: 10: False
 Vendor: vendor2, Amount: 10: True
 Vendor: vendor2, Amount: 20: False
 Vendor: vendor2, Amount: 10: True

Press any key to continue...

0 コメント:

コメントを投稿