h4mme7
h4mme7

Reputation: 91

Access the data in the dictionary when the item is an object?

So I am lost on how to send data back to the object once it is added to the dictionary.

With this data structure that I made http://pastebin.com/HicZMzAt for full code

I have

public class Computer
{
    public Computer() { }
    public Computer(int _year)
    {
        dropOffDate = DateTime.Now;
        RepairFinished = false;
        Year = _year;
    }


    private DateTime dropOffDate;
    public bool RepairFinished;
    private readonly int Year;
    public static string Plate;
    private string make;

    public string Make
    {
        get { return make; }
        set { make = value; }
    }
    public string Model { get; set; }
    public string ComputerTicketId { get; set; }

    public bool IsLaptop { get; set; }

    public Location Location { get; set; }

    public int HoursWorked { get; set; }
    public double PartsCost { get; set; }
    public DateTime DateFinished { get; set; }
    // public virtual double TotalCost { get { TotalCost = (this.HoursWorked * 50) + PartsCost; } set; }



    public void ComputerPickUp()
    {
        Console.WriteLine("Cost is {0:C} ", this.HoursWorked);

        RepairFinished = true;
    }

where I want to calculate the different cost for repairs for each dropped of system.

public class Laptop : Computer
{

    public bool HasCharger { get; set; }

    public Laptop(int year, bool _HasCharger)
        : base(year)
    {
        HasCharger = _HasCharger;
    }

    //TODO overide for COST ! + 10

and I have a desktop class also were the cost of repair is cheaper for Desktop systems.

But I am using

public static class Repair
{

    public static Dictionary<string, object> RepairLog { get; set; }
}

to track the repairs and now I am lost in the UI part of the program to get the data to figure out the pricing.

public class RepairUI
   { 
....edited
  Repair.RepairLog = new Dictionary<string, object>();
 ....
 Computer = new Desktop(ComputerYear, HasLcd);

And that is how I am lost about the way to handle the data , the class data for each repair unit (desktop / NBK ) is organized in the dictionary and now I want to get the data and edit the repair cost of the object , but I can't seem to figure out how to reach the object.

So how could I ask upon pick up hours worked and calculate the info for the unit ?

Upvotes: 0

Views: 62

Answers (1)

Vlad274
Vlad274

Reputation: 6844

This sounds like a great moment to use an Interface!

public Interface IRepairable
{
    double GetRepairCost();
}

Then redefine Computer

public class Computer : IRepairable
{
    public double GetRepairCost()
    {
        return (this.HoursWorked * 50) + PartsCost;
    }
}

and Laptop

public class Laptop : Computer
{
    public new double GetRepairCost()
    {
        return base.GetRepairCost() + 10;
    }
}

and Repair

public static class Repair
{
    public static Dictionary<string, IRepairable> RepairLog { get; set; }
}

And now you have a dictionary of things that you can call GetRepairCost() on! These could be Computers or Laptops or a mix, it doesn't matter to the RepairLog!

Upvotes: 2

Related Questions