-6

I need some idea for the Design of the following Program/Function. My Program calculates Prices for Tools. Every Tool has a Diameter and a List of Productionsteps to build this tool. To get the Production Steps the program reads a XML File with 56 Productionsteps at the begin and creates an object for every Step. After the Creation it adds that object in a List.
The Konstruktor of the Tool knows what Steps are required to build the tool and get them out of the List of the Main Program and adds them to the List of the Tool.

Pseudo Code:

main.cs

List<ProductionStep> listOfAllProductionstep = readAllPS(../pfad/);
Tool abc = new Tool();

tool.cs

private List<ProductionStep> psList;
public Tool() {
     foreach (ProductionStep ps in listOfAllProductionstep) {
          switch(ps.Name) {
              case "Abc":
                 psList.Add(ps);
                 break;
              ...
           }
      }
 }

The Problem is now, each of the 56 Productionstep has it own formular to calculate the cost for this step. My first idea was to use a Switch Case construct in the calculatePrice Function of the tool. But i have to check every step with the 56 availiable Steps to get the Right Formular for this step. Now my Question. Is there a better solution to this?

Martin G
  • 17,357
  • 9
  • 82
  • 98
Kevin
  • 785
  • 2
  • 10
  • 32
  • You could use different functions and an array with all the function calls so you could do a for each to find out what to call. Like a list, where the key is "Abc" ect. and the value the void function call – SamY Aug 25 '15 at 11:44
  • 1
    Are those formulas complicated? Can you give an example? – piotrwest Aug 25 '15 at 11:45
  • you would still have to hardcode all the calls but like dictonary list = {...} and void func1(){...} void func2(){...}. I am not sure if a dictionary would be right to use. – SamY Aug 25 '15 at 11:48
  • piotrwest: The Formulas are some kind of diameter/1.0 + length oder length * 0.5 + length * 2.6. All some basic math operations – Kevin Aug 25 '15 at 11:49
  • http://stackoverflow.com/questions/4233536/c-sharp-store-functions-in-a-dictionary – SamY Aug 25 '15 at 11:50
  • SamY: Like a Dictionary? The key is the step and the Value the function call for the calculation? – Kevin Aug 25 '15 at 11:50
  • I don't know your precise requirements, but you may also store formulas in XML, read them, parse them and evaluate them. This is the most complicated solution so consider it carefully. Here is a library which you would probably need in that solution: http://www.c-sharpcorner.com/UploadFile/mgold/CodeDomCalculator08082005003253AM/CodeDomCalculator.aspx – piotrwest Aug 25 '15 at 12:13

1 Answers1

0

Mabe you are looking for something like this:

class ProductionStep
{
  public string name;
  public decimal calculatCosts()
  { 
    return 100;
  }
}

List<ProductionStep> listOfAllProductionstep;

private void button5_Click(object sender, EventArgs e)
{
  List<ProductionStep> psList = new List<ProductionStep>();
  psList.Add(listOfAllProductionstep.Find(o => o.name == "Abc"));
  psList.Add(listOfAllProductionstep.Find(o => o.name == "Def"));

  decimal totalCosts = 0;
  foreach (ProductionStep ps in psList)
    totalCosts += ps.calculatCosts();
}
user1027167
  • 4,320
  • 6
  • 33
  • 40