Realising of the function - c#

Can anybody please tell me, how to realise my function in main body ?
All works, but i want to do a catalog of employers, so how to write employe to list or massive?
class Catalog : Employe
{
Employe[] employes = new Employe[10];
Employe p1 = new Employe(14, "Mark", "James", 124151, "Coder", 4000);
public Catalog(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
{
employes[1] = p1;
}
public void CatalogLog()
{
for(int i = 0; i < employes.Length; i++)
Console.WriteLine(employes[i]);
}
}
class TestInheritence
{
public static void Main(string[] args)
{
Employe[] employes = new Employe[10];
}
}

I think you didn't set up the inheritance hierarchy with the right logic. The base class Employee is extensible and contains base methods:
public class Employee
{
private int _id;
private string _firstName;
public Employee(int id, string firstName)
{
_id = id;
_firstName = firstName;
}
public int GetID()
{
return _id;
}
public void SetID(int id)
{
if(id > 0)
_id = id;
}
public void Print()
{
Console.WriteLine("ID: {0}\tFirst Name: {1}", this._id, this._firstName);
}
}
The derived class allows the object to expand by adding new methods and properties to the properties of the base class:
public class Manager : Employee
{
private string _city;
public Manager(int id, string firstName, string city) : base(id, firstName)
{
_city = city;
}
public string GetCity()
{
return _city;
}
}
To test how these two classes work, you can review the application code below:
public class Program
{
public static void Main()
{
Employee[] employees = new[]
{
new Employee(1, "Thomas"),
new Employee(2, "John"),
new Employee(3, "Erick"),
new Employee(4, "Ahmet"),
new Employee(5, "Sun")
};
employees[0].Print();
Manager manager = new Manager(6, "Johnson", "London");
manager.Print();
Console.WriteLine("City: {0}", manager.GetCity());
}
}

If you want to add the employees to the catalog you can have an 'add' function that will add the employee to the catalog:
class Catalog : Employe
{
List<Employe> employes = new List<Employe>();
public void AddEmployee(int _age, string _firstName, string _lastName, int _id, string _job, int _salary) : base(_age, _firstName, _lastName, _id, _job, _salary)
{
Employe p1 = new Employe(_age, _firstName, _lastName, _id, _job, _salary);
employes.Add(p1);
}
public void CatalogLog()
{
for(int i = 0; i < employes.Count(); i++)
Console.WriteLine(employes[i]);
}
}
class TestInheritence
{
public static void Main(string[] args)
{
Catalog catalog = new Catalog();
catalog.AddEmployee(14, "Mark", "James", 124151, "Coder", 4000);
// Add more employees.
}
}
But I think that this is the wrong use case for inheritance. Usually you want to inherit when there is a 'Is-a' relationship between the types. But 'Catalog' is not type of 'Employee'

Related

Printing out list to console

I am trying to output the whole list to console, but all I end up getting is the message "Stack overflow 19277 times". Can someone please help me out? I have now added the rest of the code. As you can see, the list wont print to console. I have tried many ways. The ideal solution would be a PrintAllEmployees-method to console under the company class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
{
public class Person
{
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public Person(string name, int age)
{
name = Name;
age = Age;
}
}
public class Employee : Person
{
public string hireDate;
public Employee(string name, int age, string hireDate) : base(name, age)
{
hireDate = HireDate;
}
public string HireDate
{
get { return hireDate; }
set { hireDate = value; }
}
}
public class Company
{
public List<Person> employeesList = new List<Person>();
public string companyName
{
get { return companyName; }
set { companyName = value; }
}
public int employeeCount
{
get { return employeeCount; }
set { employeeCount = value; }
}
public Company(string CompanyName, int EmployeeCount)
{
EmployeeCount = employeeCount;
CompanyName = companyName;
}
}
}
using Entities;
namespace Checkpoint_2___Console_App
{
public class Program
{
static void Main(string[] args)
{
List<Person> employeesList = new List<Person>();
Person myPerson = new("Kari", 35);
Employee myEmployee = new("Ole", 35, "10.10.2000");
Company myCompany = new("Baker Hansen", 15);
employeesList.Add(myPerson);
employeesList.Add(myEmployee);
}
}
}
Look at this code:
public string companyName
{
get { return companyName; }
...
It says, "If you want to know the company name, you need to know the company name". That leads to stack overflow, because it keeps looping.
I guess what you meant was:
public string CompanyName
{
get { return companyName; }
...
Convention is that property names start with uppercase, private members with lower case.
The same goes for employeeCount.

I want to create a register method and store it in list, is this approach fine? C#

Hi there I am trying to create a terminal based application in C#. However I am getting confused. I have a list in memberrecord class, User is the parent class whereas admin & ProjectMember are its children. I want to be able to register a projectmember object by getting inputs from user; I want the new registered projectmember to be added in the list in memberrecord.
In the admin class I want to check if the register method is working fine and it is printing out the users list.
When I try to code it out and create a register method in User Parent class and try to print it, it is not printing and not showing up.
In which class should I create the regiter method? And also the print method?
public class UserRecord
{
private List<User> _users;
public UserRecord()
{
_users = new List<User>();
}
public List<User> Users
{
get { return _users; }
set { _users = value; }
}
public void AddUser(User u)
{
_users.Add(u);
}
public int NoOfUser()
{
return _users.Count;
}
public void PrintUser()
{
foreach (User u in _users)
{
Console.WriteLine(u.Id + u.Name);
}
}
public void RegisterUser()
{
ProjectMember projectMember = new ProjectMember();
System.Console.WriteLine("Enter ID:");
projectMember.Id = Convert.ToInt16(Console.ReadLine());
System.Console.WriteLine("Enter Name:");
projectMember.Name = Console.ReadLine();
System.Console.WriteLine("Type of member");
int input;
input = Convert.ToInt16(Console.ReadLine());
if (input == 1)
{
projectMember.MemberType = MemberType.Manager;
}
else
{
projectMember.MemberType = MemberType.Member;
}
Console.WriteLine(projectMember.Id + projectMember.Name);
UserRecord userRecord = new UserRecord();
userRecord.AddUser(projectMember);
}
}
and enum MemberType:
public enum MemberType
{
Manager, Member
}
And ProjectMember class:
public class ProjectMember : User
{
private MemberType _memberType;
public MemberType MemberType
{
get { return _memberType; }
set { _memberType = value; }
}
public ProjectMember(int id, string name, MemberType memberType) : base(id, name)
{
_memberType = MemberType;
}
public ProjectMember(){ }
}
and Admin class:
public class Admin : User
{
public Admin(int id, string name) : base(id, name)
{ }
}
and User class:
public abstract class User
{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public User(int id, string name)
{
_id = id;
_name = name;
// _users = new List<User>();
}
public User(){ }
}
and Program class:
class Program
{
public static void Main(string[] args)
{
UserRecord userRecord = new UserRecord();
userRecord.RegisterUser();
userRecord.NoOfUser();
userRecord.PrintUser();
}
}
I am developing it further along. So I know I have to work in while loop in the main, but I wanted to test it along the way

C# Base class constructor arguments

I learning C#. I want to see what is the best way to implement inheritance. I have a Employee base class and a PartTime derived class. Employee class only receives First and Last name and has a method to print full name.
I want to know what is the proper way to pass First and last name so that when I just call PartTime class I should be also able to print full name from the calling program. At the moment it is showing blank as full name:
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee("John", "Doe");
// emp.PrintFullName();
PartTime pt = new PartTime();
float pay=pt.CalcPay(10, 8);
pt.PrintFullName();
Console.WriteLine("Pay {0}", pay);
Console.ReadKey();
}
}
public class Employee
{
string _firstName;
string _last_name;
public Employee(string FName, string LName)
{
_firstName = FName;
_last_name = LName;
}
public Employee() { }
public void PrintFullName()
{
Console.WriteLine("Full Name {0} {1} ", _firstName, _last_name);
}
}
public class PartTime : Employee
{
public float CalcPay(int hours, int rate)
{
return hours * rate;
}
}
You can call the base class constructor from you derived class like this:
public class PartTime : Employee
{
public PartTime(string FName, string Lname)
: base(FName, LName)
{ }
}
and then create it,
PartTime pt = new PartTime("Part", "Time");
Try this:
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
//method implementations removed for clarity
}
public class PartTime:Employee
{
public PartTime(string firstName, string lastName)
: base(firstName, lastName)
{
}
}
Note that your base constructor will run before any code in your derived constructor, should you need further initialization logic in the PartTime class.
You want to add a constructor to PartTime that will pass along the first and last name to the base constructor
public PartTime(string fName, string lName) : base(fName, lName) {
}
Or you could make first and last name public properties on Employee which would be inherited by PartTime. Then you can initialize them when creating instances of either without having to maintain the PartTime constructor.
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee { FirstName = "John", LastName = "Doe" };
emp.PrintFullName();
PartTime pt = new PartTime { FirstName = "Jane", LastName = "Doe" };
float pay=pt.CalcPay(10, 8);
pt.PrintFullName();
Console.WriteLine("Pay {0}", pay);
Console.ReadKey();
}
}
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public void PrintFullName()
{
Console.WriteLine("Full Name {0} {1} ", FirstName, LastName);
}
}
public class PartTime : Employee
{
public float CalcPay(int hours, int rate)
{
return hours * rate;
}
}

How do i use this class in the main function? Dictionaries and indexers (Collections)

I am trying to add entries in dictionary array list but i don't know which arguments to set in the People Class in the main function.
public class People : DictionaryBase
{
public void Add(Person newPerson)
{
Dictionary.Add(newPerson.Name, newPerson);
}
public void Remove(string name)
{
Dictionary.Remove(name);
}
public Person this[string name]
{
get
{
return (Person)Dictionary[name];
}
set
{
Dictionary[name] = value;
}
}
}
public class Person
{
private string name;
private int age;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
using this seem to give me error
static void Main(string[] args)
{
People peop = new People();
peop.Add("Josh", new Person("Josh"));
}
Error 2 No overload for method 'Add' takes 2 arguments
This peop.Add("Josh", new Person("Josh"));
should be this
var josh = new Person() // parameterless constructor.
{
Name = "Josh" //Setter for name.
};
peop.Add(josh);//adds person to dictionary.
The class People has the method Add which only takes one argument: a Person object. The Add on the people class method will take care of adding the it to the dictionary for you and supplying both the name (string) argument and the Person argument.
Your Person class only has a parameterless constructor, which means that you need to set your Name in the setter. You can do this when you instantiate the object like above.
For your design this would solve the problem:
public class People : DictionaryBase
{
public void Add(string key, Person newPerson)
{
Dictionary.Add(key , newPerson);
}
public void Remove(string name)
{
Dictionary.Remove(name);
}
public Person this[string name]
{
get
{
return (Person)Dictionary[name];
}
set
{
Dictionary[name] = value;
}
}
}
public class Person
{
private string name;
private int age;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
}
And in Main:
People peop = new People();
peop.Add("Josh", new Person() { Name = "Josh" });

C# to Java Conversion

I'm having trouble converting especially the getter and setter.
public class CartItem : IEquatable<CartItem>
{
#region Attributes
public int Quantity { get; set; }
private int _productId;
public int ProductId
{
get { return _productId; }
set
{
_product = null;
_productId = value;
}
}
private Product _product = null;
public Product Prod
{
get
{
if (_product == null)
{
_product = new Product(ProductId);
}
return _product;
}
}
public string Name
{
get { return Prod.ProductName; }
}
public string Description
{
get { return Prod.Description; }
}
public float UnitPrice
{
get { return Prod.UnitPrice; }
}
public float TotalPrice
{
get { return UnitPrice * Quantity; }
}
#endregion
#region Methods
public CartItem(int productId)
{
this.ProductId = productId;
}
public bool Equals(CartItem item)
{
return item.ProductId == this.ProductId;
}
#endregion
}
sample of getters and setters in Java:
public class Employee {
private int empId;
private String name;
private int age;
public Employee(int empId, String name, int age) {
this.empId = empId;
this.name = name;
this.age = age;
}
// getters & setters
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
using your code:
public class Sample {
private int _productId;
public int get_productId() {
return _productId;
}
public void set_productId(int productId) {
_productId = productId;
}
private Product _product = null;
public Product get_product() {
if (_product == null) {
_product = new Product();
}
return _product;
}
public void set_product(Product product) {
_product = product;
}
}
and something more:
public class Product {
String desription;
public String getDesription() {
return desription;
}
public void setDesription(String desription) {
this.desription = desription;
}
}
//this is your hidding delegation getter only in main class (Sample in my samples)
public String getDescription(){
return _product.getDesription();
}
Java getters and setters aren't as easy to use as C#'s. In Java, every getter and setter has to be explicitly defined, rather than using the shorthand you have there.
For example, for your code "public int ProductId", you would need a line defining the variable, in addition two methods (a getter and setter) as follows:
private int _productId;
public void setProductId(int anId)
{
_productId = anId;
}
public int getProductId()
{
return _productId;
}
You'd need to define similar variable declarations and getter/setter methods for each variable you have.

Categories