static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); SalesPerson danny = new SalesPerson(); danny.Age = 31; danny.Name = "Danny"; danny.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "222-33-1223", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); double cost = chucky.GetBenefitsCost(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-33-1243", 31); fran.GiveBonus(200); fran.DisplayStats(); Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; CastingExamples(); Console.ReadLine(); }
public void TestProtectedViaDot() { SalesPerson s = new SalesPerson(); s.empName = "aaa"; Employee e = new SalesPerson(); //e.empName = "aaa"; //ERROR, but it workjs fine in Java }
// Create a subclass object and access base class functionality static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; // Assume Manager has a constructor matching this signature: // (string fullName, int age, int empID, // float currPay, string ssn, int numbofOpts) Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); double cost = chucky.GetBenefitCost(); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); // Define my benefit level. BenefitPackage.BenefitPackageLevel myBenefitLevel = BenefitPackage.BenefitPackageLevel.Platinum; SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Error! Cannot create an abstract class! //Employee X = new Employee(); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); // ITSpecialist ITSpecialist itsepc = new ITSpecialist() { Age = 31, Education = "MSc in IT", ID = 45, Name = "Phil", SocialSecurityNumber = "444-33-2222", SpecialistKnowledge = ".NET", Pay = 90000, Benefits = new Employee.BenefitPackage() }; Console.WriteLine("FedTax -> {0}", itsepc.getFedTax(90000)); Console.WriteLine("SSTax -> {0}", itsepc.getSsTax(125, 90000)); Console.WriteLine("\r\n"); // Secretary Secretary sec = new Secretary() { Age = 23, Education = "Gymi", ID = 47, Name = "Susy", SocialSecurityNumber = "333-22-1111", LanguageKnowledge = "French", Pay = 45000, Benefits = new Employee.BenefitPackage() }; Console.WriteLine("FedTax -> {0}", sec.getFedTax(45000)); Console.WriteLine("HealthFee -> {0}", sec.getHealthFee(100, 45000)); CastingExamples(); Console.ReadLine(); }
static void Main(string[] args) { SalesPerson danny = new SalesPerson(); danny.Age = 31; danny.Name = "Danny"; danny.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); double cost = chucky.GetBenefitCost(); chucky.DisplayStats(); Console.WriteLine("Benefícios: {0}", cost); Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; Console.WriteLine("Level: {0}", myBenefitLevel); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); try { PTSalesPerson ptSales = ((PTSalesPerson)danny); } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } PTSalesPerson ptCast = danny as PTSalesPerson; if (ptCast == null) { Console.WriteLine("Sorry, danny is not PTSalesPerson"); } Console.WriteLine(danny.ToString() + "&&" + danny.GetType()); Console.ReadLine(); }
static List <Employee> InitialEmployees() { Executive dan = new Executive("Dan", "Doe", DateTime.Parse("3/20/1963"), 200000, "121-12-1211", 50000, ExecTitle.CEO); Executive connie = new Executive("Connie", "Chung", DateTime.Parse("2/5/1972"), 150000, "229-67-7898", 40000, ExecTitle.CFO); Manager chucky = new Manager("Chucky", "Kirkland", DateTime.Parse("4/23/1967"), 100000, "333-23-2322", 9000); Manager mary = new Manager("Mary", "Tompson", DateTime.Parse("5/9/1963"), 200000, "121-12-1211", 9500); Engineer bob = new Engineer("Bob", "Pirs", DateTime.Parse("6/30/1986"), 120000, "334-24-2422", DegreeName.MS); SalesPerson fran = new SalesPerson("Fran", "Smith", DateTime.Parse("7/5/1975"), 80000, "932-32-3232", 31); PTSalesPerson sam = new PTSalesPerson("Sam", "Abbot", DateTime.Parse("8/11/1984"), 20000, "525-76-5030", 20); PTSalesPerson sally = new PTSalesPerson("Sally", "McRae", DateTime.Parse("9/12/1979"), 30000, "913-43-4343", 10); SupportPerson mike = new SupportPerson("Mike", "Roberts", DateTime.Parse("10/31/1975"), 15000, "229-67-7898", ShiftName.One); SupportPerson steve = new SupportPerson("Steve", "Kinny", DateTime.Parse("11/21/1982"), 80000, "913-43-4343", ShiftName.Two); // Bonuses and promotions dan.GiveBonus(1000); bob.GiveBonus(500); sally.GiveBonus(400); dan.GivePromotion(); chucky.GivePromotion(); fran.GivePromotion(); // Add reports - just report error and continue on exception try { dan.AddReport(chucky); dan.AddReport(mary); connie.AddReport(fran); connie.AddReport(sally); mary.AddReport(sam); mary.AddReport(mike); chucky.AddReport(bob); chucky.AddReport(steve); } catch (Exception e) { Console.WriteLine($"Error when adding reports: {e.Message}"); } return(new List <Employee>() { dan, connie, chucky, mary, bob, fran, sam, sally, mike, steve }); //return new List<Employee>() { dan, connie, chucky }; }
public static int Main(string[] args) { Console.WriteLine("These folks work at " + Employee.Company); Console.WriteLine(); // Uncomment to test abstract base class. // Employee e = new Employee(); Manager chucky = new Manager("Chucky", 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 93, 30000, "932-32-3232", 31); fran.GiveBonus(201); fran.DisplayStats(); // Casting! // A Manager 'is-a' object. object o = new Manager("Frank Zappa", 9, 40000, "111-11-1111", 5); // A Manager 'is-a' Employee too. Employee e = new Manager("MoonUnit Zappa", 2, 20000, "101-11-1321", 1); // A PTSales dude(tte) is a Sales dude(tte) SalesPerson sp = new PTSalesPerson("Jill", 834, 100000, "111-12-1119", 90); FireThisPerson(e); FireThisPerson(sp); // Error! Must cast when moving from base to derived! // FireThisPerson(o); FireThisPerson((Employee)o); // Numerical casts... int x = 30000; byte b = (byte)x; // Loss of information here… return(0); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); //Random r = new Random(); //Console.WriteLine(r); //Console.WriteLine(r.Next(500)); //SalesPerson m = new SalesPerson(); //Console.WriteLine(); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); Console.WriteLine(chucky); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); ArrayObjectObjects(); Console.WriteLine(); // Ack! You can't cast frank to a Hexagon, but this compiles fine! object frank = new Manager(); Hexagon hex; try { hex = (Hexagon)frank; } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } Console.WriteLine(); CastingExamples(); Console.ReadLine(); }
static void Bonus() { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("=> Bonus"); Manager chucky = new Manager("Chucky", 52, 92, 100002, "333221112", 9002); chucky.Display(); chucky.GiveBonus(500); chucky.Display(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 5003, "933221111", 31); fran.Display(); fran.GiveBonus(300); fran.Display(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Create a subclass object and access base class functionality. SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); double cost = chucky.GetBenefitCost(); // Create and use the public inner class. OK! OuterClass.PublicInnerClass inner; inner = new OuterClass.PublicInnerClass(); // Compiler Error! Cannot access the private class. //OuterClass.PrivateInnerClass inner2; //inner2 = new OuterClass.PrivateInnerClass(); // Define my benefit level. Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; Console.WriteLine("BenefitPackageLevel: {0}", myBenefitLevel); Console.WriteLine(); // Give each employee a bonus? chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); // Error! Cannot create an instance of an abstract class! //Employee X = new Employee(); CastingExamples(); Console.ReadLine(); }
static void Main(string[] args) { SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.ID = 787979; fred.Pay = 30000; fred.SalesNumber = 479; Manager mike = new Manager("Mike", 34, 54, 60000, "234-54-544", 435); //Console.WriteLine("Mike's SSN: {0}, benefit : {1}", mike.SoscialSecurityNomber, mike.GetBenefitCost()); Manager john = new Manager(); mike.ShowEmp(); double b = 8 / 2 * (2 + 2); Console.WriteLine("{0}", b); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); // use the nested class chucky.Benefits.benefitPackageLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; // use the standalone class chucky.empBenefits2.benefitPackageLevel = BenefitPackage.BenefitPackageLevel.Platinum; chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); ArrayOfArbitraryObjects(); // a weird test of casting CastingExamples(); Console.WriteLine(); // Ack! You can't cast frank to a Hexagon, but this compiles fine! object frank = new Manager(); Hexagon hex; try { hex = (Hexagon)frank; } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); }
static void CastingExamples() { // A manager 'is-a' System.Object, so we can // store a manager refrence in an object variable just fine object franky = new Manager("Franky", 9, 3000, 40000, "asdfw-222-121", 5); //Type Casting // (ClassIwantToCastTo)referenceIHave GivePromotions((Manager)franky); // A manage 'is-a" Employee too Employee moonUnit = new Manager(" Mooshy", 8, 23, 234234, "dfgw-234", 7); GivePromotions(moonUnit); // A PtSales person 'is-a' salses person SalesPerson jill = new SalesPerson("Jill", 834, 3002, 10000, "sdfg3-23-23", 90); }
static void Main( string[] args ) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Give each employee a bonus? Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); CastingExamples(); Console.ReadLine(); }
static void Main(string[] args) { Manager chucky = new Manager("Chucky", 50, 92, 10000, "233-23-2333", 9000); chucky.GiveBonus(300); chucky.DisPlay(); double cost = chucky.GetBenefitsCost();//利用委托,外部无法发现GetBenefitsCost()是BenefitsPackage类的方法 Console.WriteLine(cost); //OuterClass.PublicInnerClass inner=new OuterClass.PublicInnerClass(); //inner.publicIntNum = 7; SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisPlay(); Console.ReadLine(); }
static void Subclasses() { Console.ForegroundColor = ConsoleColor.Green; SalesPerson fred = new SalesPerson { Age = 31, Name = "Fred", SalesNumber = 50 }; Console.WriteLine($"SalesPerson: {fred.Name}, {fred.SalesNumber}, {fred.Age}"); PTSalesPerson jason = new PTSalesPerson { Age = 26, Name = "Jason", SalesNumber = 35 }; Console.WriteLine($"Part-time SalesPerson: {jason.Name}, {jason.SalesNumber}, {jason.Age}"); Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); Console.WriteLine($"Manager: {chucky.Name}, {chucky.StockOptions}, {chucky.Age}"); }
static void Main(string[] args) { Console.WriteLine("**** The Employee Class Hierarchy ****\n"); SalesPerson fred = new SalesPerson(); fred.p_empAge = 31; fred.p_empName = "Fred"; fred.p_SalesNumber = 100; // Assume Manager has a constructor matching this signature: // ... Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Give each employee a bonus? Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); CastingExamples(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // Error! Cannot create an abstract class! //Employee X = new Employee(); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 0, "333-23-2322", 0); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); CastingExamples(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy ****\n"); Manager chucky = new Manager("Cucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 300, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); // A Manager "is-a" System.Object, so we can // store a Manager reference in an object variable just fine. object frank = new Manager("Frank Zappa", 9, 3000, 40000, "123", 5); GivePromotion((Manager)frank); // explicit casting, yay! //A Manager "is-an" Employee too Employee moonUnit = new Manager("MoonUnit Zappa", 2, 3001, 20000, "567", 1); GivePromotion(moonUnit); // A PTSalesPerson "is-a" SalesPerson SalesPerson jill = new PTSalesPerson("Jill", 834, 30001, 123123, "123124", 90); GivePromotion(jill); // Use "as" to test compatability /* Hexagon hex2 = frank as Hexagon; * if (hex2 == null) * Console.WriteLine("Sorry, frank is not a Hexagon..."); */ Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("****** The Employee Class Hierarchy ******\n"); SalesPerson fred = new SalesPerson() { Age = 31, Name = "Fred", SalesNumber = 50, Pay = 6000 }; Manager chucky = new Manager("Chucky", 45, 1497, 102450, "1678-05-04", 9000); double cost = chucky.GetBenefitCost(); Employee.BenefitPackage.BenefitPackageLevel myBenefitlevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; chucky.DisplayStats(); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); fred.DisplayStats(); fred.GiveBonus(150); fred.DisplayStats(); Console.WriteLine(); CastingExamples(); Hexagon hex; object frank = new Manager(); try { hex = (Hexagon)frank; } catch (Exception e) { ConsoleColor prevCol = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.ForegroundColor = prevCol; } TestAsKeyword(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("**** Yhe employee class hierarchy ****"); SalesPerson fred = new SalesPerson(); fred.EmpName = "Fred"; fred.EmpAge = 32; fred.SalesNumber = 210; fred.CurrPay = 100; fred.GiveBonus(1000); fred.DisplayStatus(); Console.WriteLine(); Manager chuky = new Manager("Chuky", 42, 10010, 30000, 52); Console.WriteLine("Benefit: {0}", chuky.GetBenefitCost().ToString()); chuky.GiveBonus(300); chuky.DisplayStatus(); Console.WriteLine(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); // Error! Cannot create an abstract class! // Employee X = new Employee(); FunWithCasting(); Console.ReadLine(); }
// Create a subclass object and access base class functionality. static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierachy *****\n"); SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; //Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); //chucky.GiveBonus(300); //double cost = chucky.GetBenefitCost(); //chucky.DisplayStats(); //Console.WriteLine(); //SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); //fran.GiveBonus(200); //fran.DisplayStats(); //Console.WriteLine(); // Define my benefit level. Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; Console.WriteLine("My Benefit Level is {0}", myBenefitLevel); Console.WriteLine(); // A better bonus system! Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); }
static void Main(string[] args) { //коментар //коментар2 //Practice makes perfect Manager chucky = new Manager("Chucky", 50, 92, 100000, "333 - 32 - 3332", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932 - 22 - 6538", 31); fran.GiveBonus(200); fran.DisplayStats(); //Employee employee = new Employee(); - abstract class bans this action Console.WriteLine(); object[] things = new object[4]; things[0] = new Hexagon(); things[1] = 3.4; things[2] = new Manager(); things[3] = "Last thing"; foreach (object ob in things) { Hexagon h = ob as Hexagon; if (h == null) { Console.WriteLine("Item is not a Hexagon"); } else { h.Draw(); } } GivePromoution(chucky); Console.ReadKey(); }
public static void Main(string[] args) { SalesPerson fred = new SalesPerson { Age = 31, Name = "Fred", SalesNumber = 50 }; // extending PtSalesPerson from SalesPerson PtSalesPerson partTime1 = new PtSalesPerson { Name = "Hari", Age = 31, Id = 2342, SalesNumber = 40, Pay = 234.4F, }; //Manager Manager chucky = new Manager("Chucky", 50, 92, 1000000, "333-2332-s23", 9000); double cost = chucky.GetBefitCost(); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson frank = new SalesPerson("Fran", 43, 93, 3000, "9333-32-3232", 31); frank.GiveBonus(200); frank.DisplayStats(); Console.WriteLine(); CastingExamples(); }
static void Main(string[] args) { SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; Manager chucky = new Manager("Chucky", 50, 92, 100000, "3-33-11", 9000); double cost = chucky.GetBenefitsCost(); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "3-22-22", 31); fran.GiveBonus(200); fran.DisplayStats(); OuterClass.PublicInnerClass inner = new OuterClass.PublicInnerClass(); //OK //Employee e = new Employee(); ERROR, abstract class Console.ReadLine(); }
static void Main(string[] args) { #region Uncomment to test abstract base class. // Employee e = new Employee(); // Uncomment to test readonly field. // Employee brenner = new Employee(); // brenner.SSNField = "666-66-6666"; #endregion Console.WriteLine("***** These folks work at {0} *****", Employee.Company); Manager chucky = new Manager("Chucky", 35, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); chucky.Age++; // Happy birthday Chuck! Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 26, 93, 30000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); #region Casting examples Console.WriteLine("***** Casting examples *****"); // A Manager 'is-a' object. object frank = new Manager("Frank Zappa", 50, 9, 40000, "111-11-1111", 5); // A Manager 'is-a' Employee too. Employee moonUnit = new Manager("MoonUnit Zappa", 16, 2, 20000, "101-11-1321", 1); // A PTSales dude(tte) is a Sales dude(tte) SalesPerson jill = new PTSalesPerson("Jill", 30, 834, 100000, "111-12-1119", 90); // Cast from the generic System.Object into a strongly // typed Manager. Console.WriteLine("***** Counting frank's stock options *****"); Manager mgr = (Manager)frank; Console.WriteLine("Frank's options: {0}", mgr.NumbOpts); Console.WriteLine("Frank's options: {0}", ((Manager)frank).NumbOpts); Console.WriteLine("\n***** Firing employees using casting operations! *****"); TheMachine.FireThisPerson(moonUnit); Console.WriteLine(); TheMachine.FireThisPerson(jill); Console.WriteLine(); // Error! Must cast when moving from base to derived! // TheMachine.FireThisPerson(frank); TheMachine.FireThisPerson((Employee)frank); Console.WriteLine(); #endregion // Creating a (very) nested BenefitPackageLevel enum variable. Employee.BenefitPackage.BenefitPackageLevel myBenifitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; Console.ReadLine(); }
static void Main() { Console.WriteLine("***** Assignment 4: Employee Reports *****\n"); // Create Employees Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); Manager mary = new Manager("Mary", 54, 90, 200000, "121-12-1211", 9500); SalesPerson fran = new SalesPerson("Fran", 43, 93, 80000, "932-32-3232", 31); SalesPerson bob = new SalesPerson("Bob", 31, 94, 120000, "334-24-2422", 30); PTSalesPerson sally = new PTSalesPerson("Sally", 32, 95, 30000, "913-43-4343", 10); PTSalesPerson sam = new PTSalesPerson("Sam", 33, 96, 20000, "525-76-5030", 20); PTSalesPerson mike = new PTSalesPerson("Mike", 45, 91, 15000, "229-67-7898", 30); // Employee list Employee[] emps = { chucky, mary, fran, bob, sally, sam, mike }; // Add reports try { mary.AddReport(fran); mary.AddReport(mike); chucky.AddReport(bob); chucky.AddReport(sally); chucky.AddReport(sam); mary.RemoveReport(fran); mary.RemoveReport(chucky); chucky.AddReport(fran); chucky.AddReport(mike); chucky.AddReport(mary); } catch (Manager.TooManyReportsException e) { Console.WriteLine("*** Error! ***"); Console.WriteLine("Source: {0}", e.Source); Console.WriteLine("Method: {0}", e.TargetSite); Console.WriteLine("Message: {0}", e.Message); Console.WriteLine("Custom Data:"); foreach (DictionaryEntry de in e.Data) { Console.WriteLine("-> {0}: {1}", de.Key, de.Value); } } Console.WriteLine("\nDisplay Managers\n"); mary.DisplayStats(); Console.WriteLine(); chucky.RemoveReport(sam); // Remove a report for testing chucky.DisplayStats(); Console.Write("Reports by Name: "); foreach (Employee emp in chucky.ReportsByName()) { Console.Write("{0} ", emp.Name); } Console.Write("\nReports by Age: "); foreach (Employee emp in chucky.ReportsByAge()) { Console.Write("{0}-{1} ", emp.Age, emp.Name); } Console.Write("\nReports by Pay: "); foreach (Employee emp in chucky.ReportsByPay()) { Console.Write("{0:C}-{1} ", emp.Pay, emp.Name); } Console.WriteLine(); Console.ReadLine(); }
// Add Employee spare props public new static string SpareAddProp1Name() { return(SalesPerson.SpareAddProp1Name()); }
// Create a subclass object and access base class functionality. static void Main(string[] args) { Console.WriteLine("***** The Employee Class Hierarchy *****\n"); SalesPerson fred = new SalesPerson(); fred.Age = 31; fred.Name = "Fred"; fred.SalesNumber = 50; // Assume Manager has a constructor matching this signature: // (string fullName, int age, int empID, float currPay, string ssn, int numbOfOpts) Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); Console.WriteLine(chucky.Name); // Error! Can't access protected data from client code. //Employee emp = new Employee(); //emp.empName = "Fred"; double cost = chucky.GetBenefitCost(); Console.WriteLine($"cost: {cost}"); // Define my benefit level. Employee.BenefitPackage.BenefitPackageLevel myBenefitLevel = Employee.BenefitPackage.BenefitPackageLevel.Platinum; Console.WriteLine($"myBenefitLevel: {myBenefitLevel}"); // Give each employee a bonus? // chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); chucky.GiveBonus(300); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); // Ack! You can't cast frank to a Hexagon, but this compiles fine! // Catch a possible runtime exception which can be happened by invalid explicit type cast. object frank = new Manager(); Hexagon hex; try { hex = (Hexagon)frank; } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); } // Use "as" to test compatability. object[] things = new object[4]; things[0] = new Hexagon(); things[1] = false; things[2] = new Manager(); things[3] = "Last thing"; foreach (object item in things) { Hexagon h = item as Hexagon; if (h == null) { Console.WriteLine("Item is not a hexagon"); } else { h.Draw(); } } Console.ReadLine(); }
public new static string SpareAddProp1Valid(object obj) { return(SalesPerson.SpareAddProp1Valid(obj)); }
public new static object SpareAddProp1Convert(object obj) { return(SalesPerson.SpareAddProp1Convert(obj)); }
public CompDetails(Object data) : this() { this.DataContext = data; if (data is Employee) { string name = ""; string value = ""; if (data is Manager) { Manager man = (Manager)data; name = "Reports"; string options = "Stock Options"; // Set spare prop name and value man.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = man.ReportsByName(); this.SpareProp2Name.Content = options; this.SpareProp2Value.Content = man.StockOptions; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; this.SpareProp2Name.Visibility = Visibility.Visible; this.SpareProp2Value.Visibility = Visibility.Visible; } if (data is Executive) { Executive exe = (Executive)data; name = "Reports"; string options = "Stock Options "; // Set spare prop name and value exe.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = exe.ReportsByName(); this.SpareProp2Name.Content = options; this.SpareProp2Value.Content = exe.StockOptions; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; this.SpareProp2Name.Visibility = Visibility.Visible; this.SpareProp2Value.Visibility = Visibility.Visible; } if (data is Engineer) { Engineer eng = (Engineer)data; name = "Highest Degree"; // Set spare prop name and value eng.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = eng.HighestDegree; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } if (data is SalesPerson) { SalesPerson sales = (SalesPerson)data; name = "Number of Sales"; // Set spare prop name and value sales.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = sales.SalesNumber; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } if (data is PTSalesPerson) { PTSalesPerson pTSales = (PTSalesPerson)data; name = "Number of Sales"; // Set spare prop name and value pTSales.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = pTSales.SalesNumber; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } if (data is SupportPerson) { SupportPerson support = (SupportPerson)data; name = "Shift: "; // Set spare prop name and value support.GetSpareProp1(ref name, ref value); this.SpareProp1Name.Content = name; this.SpareProp1Value.Content = support.Shift; // Make visible after setting values this.SpareProp1Name.Visibility = Visibility.Visible; this.SpareProp1Value.Visibility = Visibility.Visible; } } }
static void Main(string[] args) { // create a subclass object and access base class functionality Console.WriteLine("***** This Employee class Hierarchy *****\n"); SalesPerson fred = new SalesPerson { Age = 31, Name = "Fred", SalesNumber = 50 }; // Nested Type Definitions // Create and use the public inner class // example: Employee.BenefitPackage.BenefitPackageLevel.Platinum; Manager chucky = new Manager("Chucky", 50, 92, 100000, "333-23-2322", 9000); double cost = chucky.GetBenefitCost(); // calling the BenefitPackage, a class instance within the employee class with its own property PTSalesPerson pauly = new PTSalesPerson("Paul", 22, 298, 28_000, "444-99-4990", 1, "Fred"); // "Paul", 22, 298, 28_000, "444-99-4990", 1, "Fred" or "Chucky" // display how things are going Console.WriteLine($"\nSalesPerson *** {fred.GetName()} ***"); fred.DisplayStats(); Console.WriteLine($"\nManager *** {chucky.GetName()} ***"); chucky.DisplayStats(); Console.WriteLine($"\nPartTime SalesPerson *** {pauly.GetName()} ***"); pauly.DisplayStats(); Console.Write($"{pauly.GetName()} reports to:"); Console.Write($"{pauly.ManagerName()}\n\n"); // give a bonus float bonusAmt = 2_500; chucky.GiveBonus(bonusAmt); Console.WriteLine("{0} got a bonus of {1:c}!", chucky.Name, bonusAmt); chucky.DisplayStats(); Console.WriteLine(); SalesPerson fran = new SalesPerson("Fran", 43, 93, 3000, "932-32-3232", 31); fran.GiveBonus(200); fran.DisplayStats(); Console.WriteLine(); // give a promotion or two GivePromotion(fran); GivePromotion(pauly); Keyword_AS_Example(); // temporarily showing Casting Examples method() CastingExamples(); // the need for an abstract class: // Employee emmy = new Employee("Emma", 34, 239, 47_000, "324-23-3434"); // emmy.DisplayStats(); // to avoid allowing instantiation of an employee with no assignment/job classification // MUST remove keyword abstract from the Employee Class // pause program executions before exit Console.ReadLine(); }
public new static object SpareAddProp1DefaultValue() { return(SalesPerson.SpareAddProp1DefaultValue()); }