2017-06-16 20 views
-2

が含まれているリストは、私が出力クラス

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

は、私が作成したオブジェクトを出力することができるようにしたいです。私にはさまざまなタイプの従業員がいます。この例では、Class Managerタイプを使用しました。一部のユーザーの入力した後、私は私がconsole.writeline(EmployeeList) で何かをしようとするオプションが

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

と私は名前空間を取得していること。(クラスの型)終わるので、この場合には、mynamespace.Manager

は私がよく知ってクラスをキー/パラメータとして使用することはできません。

編集: コードは完璧ではありませんが、目標は従業員をリストに追加し、名前順に表示することです。

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); 

従業員クラス

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

うん、それは右のそれはあなたが守ってきたものを行い、デフォルトToString()メソッドを呼び出すように直接Console.Writeline()を呼び出しているためです。次に、以下のコードブロックは無効である

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

の下に見られるように、何をする必要があり、リストをループであり、また、

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

みたいに各オブジェクトを表示し、あなたのEmployeeListコレクションがタイプEmployeeでありますあなたは、クラスYのtoString()メソッドをオーバーライドする場合ManagerはタイプEmployee

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

ああ、私は間違いを理解できませんでした。いくつかのテストを試して、私は正しい道にいると思う。ありがとうございました。 – Xoax

0

のでない限り、あなたはToStringメソッド(上書きしたくない場合は、LINQのと必要なデータを選択することができます)

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); 
     } 
    } 

:OUは

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

クラスを使用することができます

Console.WriteLine(string.Join(Environment.NewLine, EmployeeList.Select(x => x.Name + " " + x.Salary))); 
関連する問題