Cấp bậc tác giả:

DOTNET

Write classes to represent 3 different types of Bee - Worker, Queen and Drone

Được viết bởi QuangIT ngày 06/01/2013 lúc 06:45 PM
Each Bee has a floating-point health property, which is not writable externally and upon creation is set to a value of 100 (percent).
  • 0
  • 13830

Write classes to represent 3 different types of Bee - Worker, Queen and Drone

Each Bee has a floating-point health property, which is not writable externally and upon creation is set to a value of 100 (percent).
Each Bee has a Damage() method that takes a single integer parameter that should be a value between 0 and 100. When this method is called, the health of the bee is to be reduced by that percentage of their current health.
When a Worker has a health below 70% it cannot fly and therefore is pronounced Dead. When a Queen has a health below 20%, or a Drone below 50%, it is pronounced dead. This 'Dead' property should be readable from each Bee. When a Bee is dead, no further health deductions should be recorded by the bee, although the Damage() method should still be invokable without error.
Your application should create a single list containing 10 instances of each type of Bee and store in a list or array. It must support methods to allow Damage() to be called for each Bee, and to return the health status of each bee, including whether it is alive or not.
 Your application interface must contains 2 functions ( user press “1” or “2” to activate this function :
 1 – Create bee list – Clear current bee list and create new random bees, then display in the console windows
 2 – Attack bees   - Attack current bee list , a different random value between 0 and 80 should be selected for each bee and applied with a call to Damage(). After attacked, the user interface should refresh to show the health status of the bees  in console windows
Other requirements:
- Application type: Console 

Code
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;

namespace BeeOOP
{
    class Program
    {
        public static void Main(string[] args)
        {
            List<Bee> list = new List<Bee>(0);
            CreateNewBees(list);
            bool exit = false;

            while (!exit)
            {
                Console.Write("Input: ");

                string resp = Console.ReadLine();
                switch (resp)
                {
                    case "1":
                        CreateNewBees(list);
                        for (int i = 0; i < list.Count; i++)
                        {
                            PrintBee(i + 1, list[i]);
                        }
                        break;
                    case "2":
                        var random = new Random();
                        for (int i = 0; i < list.Count; i++)
                        {
                            var damage = random.Next(0, 80);
                            list[i].Damage(damage);
                            PrintBee(i + 1, list[i]);
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        private static void PrintBee(int number, Bee bee)
        {
            Console.WriteLine("{0}: {1}\talive = {2}\thealth = {3}", number, bee.Type, bee.Alive, bee.Health);
        }

        private static void CreateNewBees(List<Bee> list)
        {
            list.Clear();
            for (int i = 0; i < 2; i++)
            {
                list.Add(new Worker());
                list.Add(new Drone());
                list.Add(new Queen());
            }
        }
    }

    public abstract class Bee
    {

        public virtual string Type { get; protected set; }

        public virtual float Health { get; protected set; }

        protected int HealthLimit { get; set; }

        public virtual bool Alive
        {
            get
            {
                return (this.Health > this.HealthLimit);
            }
        }

        public virtual void Damage(int damage)
        {
            if (this.Alive && damage > 0 && damage < 100)
            {
                this.Health -= damage;
            }
            if (this.Health < 0)
            {
                this.Health = 0;
            }
        }

        protected Bee(string type, int healthLimit)
        {
            this.Type = type;
            this.Health = 100;
            this.HealthLimit = healthLimit;
        }
    }

    public sealed class Worker : Bee
    {
        public Worker() : base("Worker", 70) { }
    }

    public sealed class Drone : Bee
    {
        public Drone() : base("Drone", 50) { }
    }

    public sealed class Queen : Bee
    {
        public Queen() : base("Queen", 20) { }
    }
}

Nguồn bài viết: Dngaz.com

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML