2017-06-16 119 views
-2

一個名單上有輸出包含類

list<Employee> EmployeeList = new list<Employee>(); 

我希望能夠輸出創建的對象。我有不同類型的員工。在這個例子中,我使用了類管理器。一些用戶輸入後,我結束與

Employee newEmployee = new Employee(name, address); 
newEmployee = new Manager(name, address, salary, bonus); 
EmployeeList.Add(newEmployee); 

該選項,如果我嘗試用console.writeline(EmployeeList) 什麼我剛剛得到的命名空間。(類類型),所以在這種情況下,mynamespace.Manager

我熟悉與列表,但不使用類作爲關鍵/參數。

編輯: 代碼並不完美,但總體目標是將Employees添加到列表中並按名稱順序顯示它們。

class Program 
{ 
    static void Main(string[] args) 
    { 

     List<Employee> EmployeeList = new List<Employee>(); 


     bool loop = true; 

     while (loop) 
     { 

     Console.Clear(); 
     Console.WriteLine("Main Menu"); 
     Console.WriteLine("1. Add Employee"); 
     Console.WriteLine("2. Remove Employee"); 
     Console.WriteLine("3. Display Payroll"); 
     Console.WriteLine("4. Exit"); 

     Console.Write("Selection: "); 



     string input = Console.ReadLine().ToLower(); 

      switch (input) 
      { 
       case "1": 
       case "add employee": 
        { 

         Console.WriteLine("Add Employee"); 
         Console.WriteLine("1. Full Time"); 
         Console.WriteLine("2. Part Time"); 
         Console.WriteLine("3. Contractor"); 
         Console.WriteLine("4. Salaried"); 
         Console.WriteLine("5. Manager"); 
         Console.WriteLine("6. Previous Menu"); 
         Console.Write("Selection: "); 
         string choice = Console.ReadLine().ToLower(); 

         if (choice.Contains("1") || choice.Contains("full time")) 
         { 
          Console.Write("\nEmployee Name: "); 
          string name = Console.ReadLine(); 
          while (string.IsNullOrWhiteSpace(name)) 
          { 
           Console.WriteLine("Must not be blank"); 
           name = Console.ReadLine(); 
          } 

          Console.Write("Employee Address: "); 
          string address = Console.ReadLine(); 
          while (string.IsNullOrWhiteSpace(address)) 
          { 
           Console.WriteLine("Must not be blank"); 
           address = Console.ReadLine(); 
          } 

          Console.Write("Employee Pay per Hour: "); 
          string pph = Console.ReadLine(); 
          decimal payPerHour; 

          while (!decimal.TryParse(pph, out payPerHour)) 
          { 
           Console.WriteLine("Must be a decimal"); 
           pph = Console.ReadLine(); 
          } 


          Employee newEmployee = new Employee(name, address); 
          newEmployee = new FullTime(name, address, payPerHour); 
          EmployeeList.Add(newEmployee); 

Employee類

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

namespace TrevorMolano_Project_CE07 

{ 

class Employee : IComparable<Employee> 
{ 


    string name; 
    string address; 


    public int CompareTo(Employee obj) 
    {   
     Employee person = obj; 

     return string.Compare(name, obj.name); 


    } 

    public virtual decimal CalculatePay(decimal _hpw, decimal _pph, decimal _nbb) 
    { 
     decimal hpw = _hpw; 
     decimal pph = _pph; 
     decimal answer; 
     answer = hpw * pph * 52; 
     return answer; 

    } 

    public Employee(string _name, string _address) 
    { 
     name = _name; 
     address = _address; 
    } 
} 
} 
+1

你想要什麼不清楚去做。請提供[mcve]。順便說一句,實例化'Employee'的代碼的第一行在您重新聲明爲下一行中的'Manager'時是非常冗餘的。 – Jamiec

回答

2

是啊,這是正確的,因爲調用Console.Writeline()一樣,直接調用默認ToString()方法,確實你有注意的事項。你所要做的是遍歷目錄,並在其中顯示的每個對象像

foreach(var item in EmployeeList) 
{ 
    console.writeline(item.name +"\t"+item.address); 
} 

此外,您EmployeeList收集Employee類型所看到如下

list<Employee> EmployeeList = new list<Employee>(); 

然後將下面的代碼塊無效除非Manager的類型是Employee

Employee newEmployee = new Employee(name, address); 
newEmployee = new Manager(name, address, salary, bonus); 
EmployeeList.Add(newEmployee); 
+0

好吧,我不明白我的錯誤。嘗試了一些測試,我認爲我在正確的道路上。謝謝。 – Xoax

0

如果重寫ToString()方法在你的位置Y您可以同時使用

Console.WriteLine(string.Join(Environment.NewLine, EmployeeList)); 

類:

public class Employee 
    { 
     public string Name { get; set; } 
     // ... 
     public override string ToString() 
     { 
      return string.Format("Employee: {0}", Name); 
     } 
    } 

    public class Manager : Employee 
    { 
     public override string ToString() 
     { 
      return string.Format("Manager: {0}", Name); 
     } 
    } 

如果你不希望重寫的ToString(),您可以使用LINQ選擇所需數據:

Console.WriteLine(string.Join(Environment.NewLine, EmployeeList.Select(x => x.Name + " " + x.Salary)));