C#面向对象基础练习之狗血剧情小故事

题目要求

  • 买房是⼤事:创建两个类,模拟⽣活中房屋和⼈的特征和⾏为。
    房屋类:
    属性:房屋主⼈、地址、⾯积、每平⽶价格
    ⽅法:估价、升值等
    ⼈类:
    属性:姓名、性别、钱、配偶、房⼦
    ⽅法:挣钱、买房⼦、结婚、卖房⼦、离婚等
    在Main⽅法中运⽤以上两个类创建对象模拟以下情景:
    ⼀个⼈⼯作挣钱,有⼀天终于攒够了钱,买了⼀个房⼦,然后找了⼀个对象结婚。
    后来婚姻出现了第三者,两⼈离婚后此⼈卖掉了房⼦,最终和第三者结婚。

博主自由发挥了一下,有一些小的分支细节没有做处理,太累了,歇了
代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bMethodWithParams
{

    #region 
    /// <summary>
    /// 房屋类
    /// </summary>
    class House
    {
        /// <summary>
        /// 房屋主人
        /// </summary>
        private Person houseHost = null;

        /// <summary>
        /// 房屋地址
        /// </summary>
        private string address;

        /// <summary>
        /// 房屋面积
        /// </summary>
        private float area;

        /// <summary>
        /// 房屋价格万元/平米
        /// </summary>
        private float priceOfPerSquare;

        /// <summary>
        /// 房屋构造函数
        /// </summary>
        /// <param name="person"></param>
        /// <param name="address"></param>
        /// <param name="area"></param>
        /// <param name="priceOfPerSquare"></param>
        public House(Person person, string address, float area, float priceOfPerSquare)
        {
            this.Person = person;
            this.address = address;
            this.area = area;
            this.priceOfPerSquare = priceOfPerSquare;
        }

        public Person Person
        {
            get
            {
                return houseHost;
            }
            set
            {
                houseHost = value;
            }


        }

        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }
        }

        public float Area
        {
            get
            {
                return area;
            }
            set
            {
                area = value;
            }
        }

        public float PriceOfPerSquare
        {
            get
            {
                return priceOfPerSquare;
            }
            set
            {
                priceOfPerSquare = value;
            }
        }

        /// <summary>
        /// 房屋估价方法
        /// </summary>
        /// <returns>返回估价结果</returns>
        public float Evaluation()
        {
            return area * priceOfPerSquare;
        }

        /// <summary>
        /// 房屋升值
        /// </summary>
        public void PriceChange()
        {
            priceOfPerSquare += 0.1f;
        }

    }

    /// <summary>
    /// 人类
    /// </summary>
    class Person
    {
        private string name;
        private string sex;
        private float money;
        private Person spouse = null;
        private House[] homes = new House[3];

        /// <summary>
        /// 人类构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sex"></param>
        /// <param name="money"></param>
        /// <param name="spouse"></param>
        /// <param name="homes"></param>
        public Person(string name, string sex, float money, Person spouse, House[] homes)
        {
            this.name = name;
            this.sex = sex;
            this.money = money;
            this.spouse = spouse;
            this.homes = homes;
        }
        public Person(string name, string sex, float money)
        {
            this.name = name;
            this.sex = sex;
            this.money = money;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Sex
        {
            get
            {
                return sex;
            }
            set
            {
                sex = value;
            }
        }

        public float Money
        {
            get
            {
                return money;
            }
            set
            {
                money = value;
            }
        }

        public Person Spouse
        {
            get
            {
                return spouse;
            }
            set
            {
                spouse = value;
            }
        }

        public House[] Homes
        {
            get
            {
                return homes;
            }
            set
            {
                homes = value;
            }
        }

        /// <summary>
        /// 挣钱方法,把挣到的钱添加到财产里
        /// </summary>
        /// <param name="money">挣的钱</param>
        public void EarnMoney(float money)
        {
            this.money += money;
            Console.WriteLine($"{name}工作了一年,挣了{money}万。现在有资产{this.money}万");
        }

        /// <summary>
        /// 判断是否买的起
        /// </summary>
        /// <param name="house">要买的房屋</param>
        public int AffordHouse(House house)
        {
            if (house.Person != null)
            {
                Console.WriteLine("该房产已售出!购买失败!");
                return 0;
            }
            else if (money < house.Evaluation())
            {
                Console.WriteLine("资金不够,买不起!滚粗!");
                return 1;
            }
            else
            {
                return this.BuyOrNot(house);
            }
        }

        /// <summary>
        /// 询问是否真的要购买所选的房产
        /// </summary>
        /// <param name="house">所选的房产</param>
        private int BuyOrNot(House house)
        {
            Console.WriteLine($"您确定要购买位于{house.Address}的这套房产吗?");
            Console.WriteLine("确定请按数字键“1”并回车,否则按“2”:");
            string flag = Console.ReadLine();

            if (flag.Equals("1"))
            {
                return this.BuyHouse(house);
            }
            else if (flag.Equals("2"))
            {
                Console.WriteLine("您取消了房屋购买活动!");
                return 2;
            }
            else
            {
                Console.WriteLine("您的输入有误!请重新输入!");
                return this.BuyOrNot(house);
            }

        }

        /// <summary>
        /// 买房操作
        /// </summary>
        /// <param name="house"></param>
        private int BuyHouse(House house)
        {
            for (int i = 0; i < homes.Length; i++)
            {
                if (homes[i] == null)
                {
                    homes[i] = house;

                    this.money -= house.Evaluation();

                    //房屋所有权变更
                    house.Person = this;
                    Console.WriteLine($"尊敬的{name},恭喜您购买了位于{house.Address}的一套房产!总共花费{house.Evaluation()}万元。");
                    return 3;
                }

                if (i == homes.Length - 1)
                {
                    Console.WriteLine("购买失败!您无法再拥有更多的房产!要不给你老婆儿子情人买一套?");
                    return 4;
                }
            }

            return 5;
        }

        /// <summary>
        /// 售出房产方法
        /// </summary>
        /// <param name="house">要出手的房产</param>
        public void SellHouse(House house)
        {

            if (house.Person != this)
            {
                Console.WriteLine("你跟我闹呢?!这是你的房产吗?!滚一边去!");
                return;
            }

            for (int i = 0; i < homes.Length; i++)
            {
                if (homes[i] == house)
                {
                    homes[i] = null;

                    this.money += house.Evaluation();

                    Console.WriteLine($"{name}售出了位于{house.Address}的房产。得到资金{house.Evaluation()}万元。");
                    break;
                }
            }
        }

        /// <summary>
        /// 结婚方法
        /// </summary>
        /// <param name="lover">要结婚的对象</param>
        public void Marry(Person lover)
        {
            if (this.spouse == null && lover.Spouse == null)
            {
                this.Spouse = lover;
                lover.Spouse = this;

                Console.WriteLine($"{DateTime.Now},{this.name}与{lover.Name}结为夫妻!");
            }
            else
            {
                Console.WriteLine("嘿嘿~~,你们两个中有一个结婚了哦!");
            }
        }


        public void Divorce()
        {
            if (this.Spouse == null)
            {
                Console.WriteLine("???醒一醒!你压根没有配偶!要跟谁离婚呢?");
                return;
            }

            Console.WriteLine($"{DateTime.Now},{name}与{spouse.Name}离婚了。");
            spouse.Spouse = null;
            spouse = null;


        }

    }

    #endregion


    class Program
    {
        static void Main(string[] args)
        {
            //定义四个房子
            House hut = new House(null,"荒野",30f,0.5f);

            House bungalow = new House(null, "郊区", 100f, 1f);

            House building = new House(null, "市区", 150f, 2f);

            House villa = new House(null, "江边", 200f, 5f);

            //定义三个人
            Person leadingMan = new Person("渣男", "男", 10f);

            Person leadingLady = new Person("女一号", "女", 100000000f);

            Person mistress = new Person("情妇", "女",0f);

            Console.WriteLine("这个故事是三个人的爱恨纠葛~~");

            Console.WriteLine("故事开始--------");

            Console.WriteLine("渣男想要努力挣钱,走上人生巅峰,迎娶白富美!");
            Console.WriteLine("于是开始为了别墅努力------");
            Console.WriteLine("努力中。。。");

            int count = 0;

            while(leadingMan.Money < building.Evaluation())
            {
                leadingMan.EarnMoney(50f);
                if (count++ > 9)
                {
                    break;
                }

            }

            Console.WriteLine($"工作了{count - 1}年,{leadingMan.Name}准备去买房了。");
//flag记录买房结果,决定后续分支
            int flag = leadingMan.AffordHouse(building);

            switch (flag)
            {
                case 0:
                    Console.WriteLine($"因为工作太久,已经买不到房子了,{leadingMan.Name}孤独一生。");
                    break;

                case 1:
                    Console.WriteLine($"因为好高骛远,买不起想要的房子,{leadingMan.Name}孤独一生。");
                    break;

                case 2:
                    Console.WriteLine($"因为心疼钱,没有买房子,{leadingMan.Name}孤独一生。");
                    break;

                case 3:
                    Console.WriteLine($"{leadingMan.Name}买下了{leadingMan.Homes[0].Address},准备结婚生孩子走向幸福啦!");
                    break;

                case 4:
                    Console.WriteLine($"{leadingMan.Name}原来十个隐藏的富二代,没有找到真爱,孤独一生。");
                    break;

                case 5:
                    Console.WriteLine($"程序出错,程序员大大紧急修复中。。。");
                    break;

                default:
                    break;
            }

            if (flag == 3)
            {
                leadingMan.Marry(leadingLady);

                Console.WriteLine("本来以为日子就这样平淡的过下去,直到有一天,她出现了。。。");

                Console.WriteLine($"{leadingMan.Name}厌倦了平淡的生活,决定跟{mistress.Name}在一起!他要离婚!");

                leadingMan.Divorce();
                
				leadingMan.SellHouse(building);
				
                Console.WriteLine($"离婚后,{leadingMan.Name}和{mistress.Name}奔向新生活!");

                leadingMan.Marry(mistress);

            }

            if (flag == 4)
            {
                //TODO
            }

        }
    }
}

运行结果如下:
在这里插入图片描述
好啦,这道题就是这样啦。

写了这么长的代码,你还能留住你的赞吗?

有疑问的可以私信博主。
点个关注,给个赞呗!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值