void pattern_OnMonthlyReport(Report r) { ListViewItem item = new ListViewItem(r.MonthID.ToString()); item.SubItems.Add(r.nEmployees.ToString()); item.SubItems.Add(r.nIdle.ToString()); if (r.ProductKind >= 0) { item.SubItems.Add(((ProductKind)(r.ProductKind)).ToString()); } else { item.SubItems.Add("-"); } item.SubItems.Add(r.nProjects.ToString()); item.SubItems.Add(r.nProducts.ToString()); item.SubItems.Add(String.Format("{0:C}", r.MonthlyCost)); item.SubItems.Add(String.Format("{0:C}", r.MonthlyRevenue)); item.SubItems.Add(String.Format("{0:C}", r.Balance)).ForeColor = r.Balance > 0 ? SystemColors.WindowText : Color.Red; //listView1.Items.Add(item); listViewItems.Add(item); }
//public void Run() //{ // //GrowHistory.Clear(); // Company company = new Company(); // company.SafeMode = false; // //GrowHistory.Push(company); // //TryGrowNextMonth(); // for (int i = 0; i <= nMonthsToRun; i++) // { // Debug.WriteLine(String.Format("-------------------- MonthID = {0:D}, {1:S}", i, "Grow Mode")); // company.MonthID = i; // This state would not raise error since it's been test-driven or it's pre-defined // Company lastGoodState = CloneCompanyState(company); // int n = GetPredefindedResults(i); // if (n < 0) // { // n = 0; // bool successfullyDrived; // var snapshot = CloneCompanyState(company); // do // { // //lastState = CloneCompanyState(company); // if (n != 0) // { // company.CreateNewProducts(ProductKind.Classic); // } // successfullyDrived = TestDrive(company); // if (successfullyDrived) // { // lastGoodState = CloneCompanyState(company); // company = CloneCompanyState(snapshot); // Reset the state // n++; // } // } while (successfullyDrived); // n--; // company = CloneCompanyState(lastGoodState); // if (n < 0) throw new Exception("Bad model"); // } // else // { // for (int j = 0; j < n; j++) // { // if (j == 1) // { // company.CreateNewProducts(ProductKind.Classic); // } // lastGoodState = CloneCompanyState(company); // } // } // NotifyMonthDone(i, n, lastGoodState); // } //} //bool TestDrive(Company company) //{ // Company cs = CloneCompanyState(company); // //if (!company.SafeMode) // //{ // // SafeHistory.Clear(); // // SafeHistory.Push(CloneCompanyState(company)); // //} // //cs = SafeHistory.Peek(); // cs.SafeMode = true; // return TestNextMonth(cs) == TestResult.Okay; //} //enum TestResult { Okay, SafeMode, InsufficientFunds, NoStaff } //TestResult TestNextMonth(Company cs) //{ // //Company cs = SafeHistory.Peek(); // int i = cs.MonthID; // i++; // Debug.WriteLine(String.Format("-------------------- MonthID = {0:D}, {1:S}", i, "Safe Mode")); // try // { // cs.MonthID = i; // if (i >= nMonthsToRun) // { // Debug.WriteLine("Test ended successfully."); // return TestResult.Okay; // } // } // catch (MyException ex) // { // //SafeHistory.Pop(); // if (ex is SafeModeException) return TestResult.SafeMode; // else // if (ex is InsufficientFundsException) return TestResult.InsufficientFunds; // else // if (ex is NoStaffException) return TestResult.NoStaff; // else // throw; // } // if (i <= nMonthsToRun - 1) // { // TestResult testResult; // int n = 0; // var snapshot = CloneCompanyState(cs); // //Company lastState; // TestResult lastResult = TestResult.Okay; // do // { // //lastState = CloneCompanyState(cs); // //SafeHistory.Push(CloneCompanyState(cs)); // if (n == 1) // cs.CreateNewProducts(ProductKind.Classic); // testResult = TestNextMonth(cs); // switch (testResult) // { // case TestResult.Okay: // { // return TestResult.Okay; // } // case TestResult.InsufficientFunds: // { // if (lastResult == TestResult.SafeMode) // { // return TestResult.InsufficientFunds; // } // else // { // cs = CloneCompanyState(snapshot); // n++; // lastResult = TestResult.InsufficientFunds; // break; // } // } // case TestResult.SafeMode: // { // if (lastResult == TestResult.InsufficientFunds) // { // return TestResult.SafeMode; // } // else // { // cs = CloneCompanyState(snapshot); // n--; // lastResult = TestResult.SafeMode; // if (n < 0) return TestResult.SafeMode; // break; // } // } // case TestResult.NoStaff: // { // return TestResult.NoStaff; // } // default: // { // Debug.Assert(false); // break; // } // } // } while (true); // //n--; // } // else return TestNextMonth(cs); //} //public override void NextMonth() //{ // Debug.WriteLine("--------------------"); // Debug.WriteLine("MonthID = " + MonthID.ToString()); // Debug.WriteLine("Plan to create " + actions[MonthID].ToString() + " project(s)."); // if (actions[MonthID] < 0) throw new ApplicationException("action failed."); // projectCreatedThisMonth = 0; // Employees.MonthID = MonthID; // BusinessCycles.MonthID = MonthID; // Accounting.MonthID = MonthID; // Debug.WriteLine("Totally " + projectCreatedThisMonth.ToString() + " project(s) created this month."); // Debug.WriteLine(String.Format("nEmployees = {0:D}, nProjects = {1:D}, nProducts = {2:D}", Employees.GetNumOfEmployees(), BusinessCycles.NumberOfProjects, BusinessCycles.NumberOfProducts)); //} void NotifyMonthDone(int iMonth, int ProductKind, Company companyState) { Report r = new Report(); r.MonthID = iMonth; r.nEmployees = companyState.NumberOfEmployees; r.nIdle = companyState.NumberOfIdleStaffs; r.ProductKind = ProductKind; r.nProjects = companyState.NumberOfProjects; r.nProducts = companyState.NumberOfProducts; r.MonthlyCost = companyState.Accounting.MonthlyCost / 10000; r.MonthlyRevenue = companyState.Accounting.MonthlyRevenue / 10000; r.Balance = companyState.Accounting.Balance / 10000; OnMonthlyReport(r); }