Reputation: 5
For a few days now I've been researching and studying about this particular lesson at .NET tutorials located at the Microsoft site. LINK
As you already may know there is a "homework" at the end of each lesson. This time I should upgrade the given project with some of the new features and implement Encapsulation and Interfaces.
New features should include:
Customers have a property exposing their historic Orders
// update: implemented
Customers expose a method for adding an Order
Trying to add a null Order should do nothing
Trying to add an Order with an existing OrderNumber should replace the existing Order (not add a duplicate)
// the only thing I succeeded is removing the previous "orders" with the same name instead of just replacing it.
Orders should expose an OrderDate (which can be read/write)
Trying to add an order with an OrderDate in the future should do nothing
I did manage to add most of these features. My project currently(updated):
namespace ConsoleApp2
{
class Program
{
static void Main()
{
Customer customer1 = new Customer("John");
Customer customer2 = new Customer("George");
var customers = new List<Customer>() { customer1, customer2 };
customer1.AddOrder("car", "12/7/1999"); // will be removed due to same name
customer1.AddOrder("vase", "20/6/2024");// will not be added to the list because of the future time
customer1.AddOrder("car", "3/12/2014");
customer1.AddOrder("headphones", "3/12/2022");// will not be added to the list because of the future time
customer2.AddOrder("headphones", "10/3/2002");
customer2.AddOrder("", "");// will not be added to the list due to empty values
//print customers
foreach (var customer in customers)
{
customer.Print();
}
}
}
public class Customer
{
public string Name { get; }
private List<Order> orders = new List<Order>();
private List<Order> ordersHistory = new List<Order>();
public Customer(string name)
{
Name = name;
}
public void AddOrder(string name, string date)
{
if (name == null) { return; }
else if (name == "" || date == "") { return; }
else
{
AddHistoricOrder(name, date);
AddRegularOrder(name, date);
}
}
public void Print()
{
Console.WriteLine(Name);
Console.Write("Orders: ");
orders.ForEach(Console.Write);
Console.WriteLine();
Console.Write("Historic Orders: ");
ordersHistory.ForEach(Console.Write);
Console.WriteLine();
Console.WriteLine($"Order Count: {orders.Count}");
Console.WriteLine();
Console.WriteLine();
}
private void AddRegularOrder(string name, string date)
{
if (DateTime.Parse(date) > DateTime.Now) { return; }
else
{
for (int i = 0; i < orders.Count; i++)
{
if (orders[i].OrderName == name)
{
orders.RemoveAt(i);
}
}
orders.Add(new Order(name, date));
}
}
private void AddHistoricOrder(string name, string date)
{
ordersHistory.Add(new Order(name, date));
}
public override string ToString()
{
return $"{Name}";
}
}
public class Order
{
public string OrderName { get; }
public DateTime OrderDate { get; set; }
public Order(string orderName, string date)
{
OrderName = orderName;
OrderDate = DateTime.Parse(date);;
}
public override string ToString()
{
return $"{OrderName} ({OrderDate.ToShortDateString()}), ";
}
}
}
Even tho I searched and watched various videos for encapsulation and interfaces I'm still not sure how should I implement those in here. Could you help me making my code more efficient ?
I did not implement a property for exposing historic Orders (I'm just not sure what that is supposed to do)
Also I did not really understand one part of the lesson called "New is Glue" which says we should avoid adding new keyword to our code and showed some example using interfaces. I failed to find any info outside of the given LINK How am I suppose to avoid creating new instances in this particular project? Thanks in advance!
Upvotes: 0
Views: 982
Reputation: 2034
Customers have a property exposing their historic Orders: Just add a getter to expose the order list.
public List Orders { get;} = new List();
Trying to add a null Order should do nothing:
I think the required functionality here is to ignore null objects
public void AddOrder(Order o){
if (o == null){
return;
}
//rest of your implememntaton
}
You are adding orders by using the name and date instead try to pass an Order object to the function, and for the simplicity assume the name is the unique identifier.
New is Glue
Dependency Injection aside(its just a buzz word for you now anyways).
What they are trying to emphasize is that "new" keyword creates a dependency on the specific class you creating, so should you decide in the future to change the class who takes care of the order/customer etc. you will have to come back to your code and modify it,possibly in multiple locations.
For now with the limited tools at your disposal what you can do instead is to add a function which creates the object for you, even if inside of the function you are using "new".
for example:
public Order CreateNewOrder(string name, string date) {
return new Order (name, date);
}
Upvotes: 1
Reputation: 817
There is no a perfect way of getting something done, there is always a better way of doing it so don't worry about having the perfect code, especially when you are getting started.
I have read the tutorial and I have checked your code I think you are doing great so far.
About the property for exposing historic Orders I believe this can be interpret in different ways because is too general. For instance I could make another list adding every order that the costumer request without worrying about the date, name or if it's empty.
Another approach would be adding another list as the main one with the exception that if you add an order with the same name it would store it without removing the previous one because it's a history.
You could do something like this.
public class Customer
{
public string Name { get; }
private List<Order> orders = new List<Order>();
public List<Order> historicOrders = new List<Order>();
public Customer(string name)
{
Name = name;
}
public void AddOrder(string name, string date)
{
if (name == "" || date == "" ) ;
else if (DateTime.Parse(date) > DateTime.Now) ;
else
{
for (int i = 0; i < orders.Count; i++)
{
if (orders[i].OrderName == name)
{
orders.RemoveAt(i);
}
}
orders.Add(new Order(name, date));
historicOrders.Add(new Order(name, date));
}
}
public void Print()
{
Console.WriteLine(Name);
Console.Write("Orders: ");
orders.ForEach(Console.Write);
Console.WriteLine();
Console.WriteLine($"Order Count: {orders.Count}");
Console.WriteLine();
Console.WriteLine();
}
public override string ToString()
{
return $"{Name}";
}
}
Upvotes: 0