深入.net和C#编程第二章上机练习2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyComputer
{
    public class Computer
    {
        /// <summary>
        /// 计算机型号
        /// </summary>
        public string  Type { get; set; }
        /// <summary>
        /// 计算机ID
        /// </summary>
        public string  ID { get; set; }
        /// <summary>
        /// 购买日期
        /// </summary>
        public string BuyDate { get; set; }
       
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyComputer
{
    public class Manager
    {
        /// <summary>
        /// 设置计算机ID
        /// </summary>
        /// <param name="comps"></param>
        public void SetID(Computer[] comps)
        {
            Random rand = new Random();
            foreach (Computer cp in comps)
            {
                cp.ID = cp.Type + "-" + rand.Next(1000, 9999);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyComputer
{
    class Program
    {
        static void Main(string[] args)
        {
            Manager myManager = new Manager();
            //初始化计算机信息
            #region Init Computers
            Computer[] computers = new Computer[3]{
                                                    new Computer(),
                                                    new Computer(),
                                                    new Computer()
                                                    };
            computers[0].Type = "hpCQ-217TX";
            computers[0].BuyDate = "2013-10-5";
            computers[1].Type = "Mc240CH/A";
            computers[1].BuyDate = "2013-10-5";
            computers[2].Type = "SYNW18H/W";
            computers[2].BuyDate = "2013-10-5";
            #endregion
            //设置计算机ID之前的计算机信息
            Console.WriteLine("***************设置计算机ID前******************");
            Console.WriteLine("计算机型号\t计算机ID     \t购买时间");
            foreach (Computer item in computers)
            {
                Console.WriteLine("{0}\t{1}\t        {2}\t", item.Type, item.ID, item.BuyDate);
            }
            Console.WriteLine("\n***************设置计算机ID后******************");
            //设置计算机ID
            myManager.SetID(computers);

            //设置计算机ID之后的计算机信息
            Console.WriteLine("计算机型号\t计算机ID     \t购买时间");
            foreach (Computer item in computers)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t", item.Type, item.ID, item.BuyDate);
            }
            Console.ReadLine();
        }
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值