Понедельник, Май 25, 2020

Beehive

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

namespace Beehive
{
    class Worker
    {
        private string currentJob = "";
        public string CurrentJob
        {
            get
            {
                return currentJob;
            }
        }
        private string[] jobsICanDo;
        private int shiftsToWork;
        private int shiftsWorked;
        public Worker(string[] jobsICanDo)
        {
            this.jobsICanDo = jobsICanDo;
        }
        public int ShiftsLeft
        {
            get
            {
                return shiftsToWork = shiftsWorked;
            }
        }
        public bool DoThisJob(string job,
            int numberOfShifts)
        {
            if (!string.IsNullOrEmpty(currentJob))
                return false;
            for (int i = 0; i < jobsICanDo.Length; i++)
                if (jobsICanDo[i] == job)
                {
                    currentJob = job;
                    this.shiftsToWork = numberOfShifts;
                    shiftsWorked = 0;
                    return true;
                }
            return false;
        }
        public bool WorkOneShift()
        {
            if (string.IsNullOrEmpty(currentJob))
                return false;
            shiftsWorked++;
            if (shiftsWorked > shiftsToWork)
            {
                shiftsWorked = 0;
                shiftsToWork = 0;
                currentJob = "";
                return true;
            }
            else
                return false;
        }
    }
}

 

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

namespace Beehive
{
    class Queen
    {
        private Worker[] workers;
        private int shiftNumber = 0;
        public Queen(Worker[] workers)
        {
            this.workers = workers;
        }
        public bool AssignWork(string job,
            int numberOfShifts)
        {
            for (int i = 0; i < workers.Length; i++)
                if (workers[i].DoThisJob(job, numberOfShifts))
                    return true;
            return false;
        }
        public string WorkTheNextShift()
        {
            shiftNumber++;
            string report = "Отчет для cмены #" +
                shiftNumber + "\r\n";
            for (int i = 0; i < workers.Length; i++)
            {
                if (workers[i].WorkOneShift())
                    report += "Рабочий #" + (i + 1) + " закончил работу\r\n";
                if (string.IsNullOrEmpty(workers[i].CurrentJob))
                    report += "Рабочий #" + (i + 1) + " не работает\r\n";
                else
                    if (workers[i].ShiftsLeft > 0)
                        report += "Рабочий #" + (i + 1) + " выполняет '" + workers[i].CurrentJob +
                            "' еще " + workers[i].ShiftsLeft + " смен\r\n";
                    else
                        report += "Рабочий #" + (i + 1) + " закончит '" +
                            workers[i].CurrentJob + "' после этой смены\r\n";
            }
            return report;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Beehive
{
    public partial class Form1 : Form
    {
        Queen queen;
        public Form1()
        {
            InitializeComponent();
            workerBeeJob.Items.Add("Nectar collector");
            workerBeeJob.Items.Add("Honey manufacturing");
            workerBeeJob.Items.Add("Egg care");
            workerBeeJob.Items.Add("Baby bee tutoring");
            Worker[] workers = new Worker[4];
            workers[0] = new Worker(new string[] { "Nectar collector", "Honey manufacturing" });
            workers[1] = new Worker(new string[] { "Egg care", "Baby bee tutoring" });
            workers[2] = new Worker(new string[] { "Hive maintenance", "String patrol" });
            workers[3] = new Worker(new string[] { "Nectar collector", "Honey manufacturing",
                "Egg care", "Baby bee tutoring","Hive maintenance", "String patrol"});
            queen = new Queen(workers);
        }

        private void nextShift_Click(object sender, EventArgs e)
        {
            report.Text = queen.WorkTheNextShift();
        }

        private void assignJob_Click(object sender, EventArgs e)
        {
            if (queen.AssignWork(workerBeeJob.Text,
                (int)shifts.Value) == false)
                MessageBox.Show("Для этого задания рабочих нет '"
                    + workerBeeJob.Text + "'", "Матка говорит...");
            else
                MessageBox.Show("Задание '" + workerBeeJob.Text + "' будет закончено через "
                    + shifts.Value + " смен", "Матка говорит...");
        }
    }
}


 

НАЗАД                                                                                                                                         ДАЛЕЕ


Back to Top