Advertisement
Guest User

Test ting

a guest
Dec 20th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Bonuses
  7. {
  8.     class Program
  9.     {
  10.  
  11.         private class Bonus
  12.         {
  13.  
  14.             #region Private Properties
  15.  
  16.             private int min;
  17.             private int max;
  18.             private double percentage;
  19.  
  20.             #endregion // Private Properties
  21.  
  22.             #region Ctor
  23.  
  24.             public Bonus(int min, int max, double percentage)
  25.             {
  26.                 this.min = min;
  27.                 this.max = max;
  28.                 this.percentage = percentage;
  29.             }
  30.  
  31.             #endregion // Ctor
  32.  
  33.             #region Public Properties
  34.  
  35.             public int Min
  36.             {
  37.                 get { return this.min; }
  38.             }
  39.  
  40.             public int Max
  41.             {
  42.                 get { return this.max; }
  43.             }
  44.  
  45.             public double Percentage
  46.             {
  47.                 get { return this.percentage; }
  48.             }
  49.  
  50.             #endregion // Public Properties
  51.  
  52.         }
  53.  
  54.         private class Category
  55.         {
  56.             #region Private Properties
  57.  
  58.             private string description;
  59.             private double price;
  60.             private List<Bonus> bonuses;
  61.  
  62.             #endregion // Private Properties
  63.  
  64.             #region Ctor
  65.  
  66.             public Category(string description, double price)
  67.             {
  68.                 this.description = description;
  69.                 this.price = price;
  70.                 this.bonuses = new List<Bonus>();
  71.             }
  72.  
  73.             #endregion // Ctor
  74.  
  75.             #region Public properties
  76.  
  77.             public string Description
  78.             {
  79.                 get { return this.description; }
  80.  
  81.                 set
  82.                 {
  83.                     this.description = value;
  84.                 }
  85.             }
  86.  
  87.             public double Price
  88.             {
  89.                 get { return this.price; }
  90.  
  91.                 set
  92.                 {
  93.                     this.price = value;
  94.                 }
  95.             }
  96.  
  97.  
  98.             public List<Bonus> Bonuses
  99.             {
  100.                 get { return this.bonuses; }
  101.             }
  102.  
  103.             #endregion // Public Properties
  104.  
  105.             #region Public Methods
  106.  
  107.  
  108.             /// <summary>
  109.             /// addBonus() :
  110.             /// </summary>
  111.             /// <param name="min"></param>
  112.             /// <param name="max"></param>
  113.             /// <param name="percentage"></param>
  114.             public void addBonus(int min, int max, double percentage)
  115.             {
  116.                 this.bonuses.Add(new Bonus(min, max, percentage));
  117.             }
  118.  
  119.             /// <summary>
  120.             /// findBonusLevel() :
  121.             /// returns the bonus percentage for the correct level
  122.             /// </summary>
  123.             /// <param name="num"></param>
  124.             /// <returns></returns>
  125.             public double findBonusLevel(int num)
  126.             {
  127.  
  128.                 foreach (Bonus b in this.Bonuses)
  129.                 {
  130.                     if (num >= b.Min && num <= b.Max)
  131.                     {
  132.                         return b.Percentage;
  133.                     }
  134.                    
  135.                 }
  136.  
  137.                 return 0;
  138.             }
  139.  
  140.             #endregion // Public Methods
  141.  
  142.         }
  143.  
  144.  
  145.         /// <summary>
  146.         /// main() :
  147.         /// Application entry point
  148.         /// </summary>
  149.         /// <param name="args"></param>
  150.         static void Main(string[] args)
  151.         {
  152.             // create software categories and bonuses
  153.             // using int.MaxValue as a lazy infinity
  154.             List<Category> categories = new List<Category>();
  155.  
  156.             Category tmp = new Category("Servers", 10000);
  157.             tmp.addBonus(1, 5, .01);
  158.             tmp.addBonus(6, 10, .02);
  159.             tmp.addBonus(11, int.MaxValue, .05);
  160.             categories.Add(tmp);
  161.  
  162.             tmp = new Category("Data Management", 5000);
  163.             tmp.addBonus(1, 5, .01);
  164.             tmp.addBonus(6, 10, .02);
  165.             tmp.addBonus(11, int.MaxValue, .05);
  166.             categories.Add(tmp);
  167.  
  168.             tmp = new Category("Office Automation", 3000);
  169.             tmp.addBonus(1, 10, .02);
  170.             tmp.addBonus(11, 20, .04);
  171.             tmp.addBonus(21, int.MaxValue, .05);
  172.             categories.Add(tmp);
  173.  
  174.  
  175.             tmp = new Category("Development", 1500);
  176.             tmp.addBonus(1, 3, .01);
  177.             tmp.addBonus(4, 7, .03);
  178.             tmp.addBonus(8, int.MaxValue, .05);
  179.             categories.Add(tmp);
  180.  
  181.  
  182.             // Main Menu
  183.             while (true)
  184.             {
  185.                 Console.Clear();
  186.                 Console.WriteLine("Main Menu" + Environment.NewLine);
  187.                 Console.WriteLine("1) Display Categories and Bonuses" + Environment.NewLine);
  188.                 Console.WriteLine("2) Enter Order info to calculate bonus" + Environment.NewLine);
  189.                 Console.WriteLine("0) Exit" + Environment.NewLine);
  190.                
  191.                 string result = Console.ReadLine();
  192.  
  193.                 switch (result)
  194.                 {
  195.                     case "1":
  196.  
  197.                         Console.Clear();
  198.  
  199.                         foreach (Category c in categories)
  200.                         {
  201.                             Console.WriteLine(c.Description + Environment.NewLine);
  202.  
  203.                             foreach (Bonus b in c.Bonuses)
  204.                             {
  205.                                 if (b.Max < int.MaxValue)
  206.                                 {
  207.                                     Console.Write(String.Format("{0:P1}", b.Percentage) + " (" + b.Min + " - " + b.Max + ")\t");
  208.                                 }
  209.                                 else
  210.                                 {
  211.                                     Console.Write(String.Format("{0:P1}", b.Percentage) + " (" + b.Min + "+)");
  212.                                 }
  213.                             }
  214.  
  215.                             Console.WriteLine(Environment.NewLine);
  216.                         }
  217.  
  218.                         Console.WriteLine(Environment.NewLine + "Press any key to continue.");
  219.                         Console.ReadKey();
  220.  
  221.  
  222.                         break;
  223.  
  224.                     case "2":
  225.  
  226.                         Console.Clear();
  227.  
  228.                         Dictionary<string, int> order = new Dictionary<string, int>();
  229.  
  230.                         foreach (Category c in categories)
  231.                         {
  232.                             Console.WriteLine("Please enter number sold in category " + c.Description + ": " + Environment.NewLine +
  233.                                 "(non-numeric characters will default to 0)");
  234.  
  235.                             string s = Console.ReadLine();
  236.  
  237.                             int num;
  238.                            
  239.                             int.TryParse(s, out num);
  240.                            
  241.                             // ignore empty categories to save iterations
  242.                             if (num != 0)
  243.                             {
  244.                                 order.Add(c.Description, num);
  245.                             }
  246.  
  247.                             Console.WriteLine(Environment.NewLine);
  248.                         }
  249.  
  250.                         double finalBonus = 0;
  251.  
  252.                         // Calculate bonuses
  253.                         foreach (KeyValuePair<string, int> entry in order)
  254.                         {
  255.                             double perc = categories.Find(n => n.Description.Equals(entry.Key)).findBonusLevel(entry.Value);
  256.  
  257.                             if (perc != 0)
  258.                             {
  259.                                 finalBonus += entry.Value * categories.Find(n => n.Description.Equals(entry.Key)).Price * perc;
  260.                             }
  261.  
  262.                         }
  263.  
  264.                         Console.WriteLine("Final bonus total for this order: " + "$" + finalBonus + Environment.NewLine);
  265.                         Console.WriteLine("Press any key to continue.");
  266.                         Console.ReadKey();
  267.  
  268.                         break;
  269.  
  270.                     case "0":
  271.  
  272.                         return;
  273.  
  274.                     default:
  275.  
  276.                         break;
  277.  
  278.  
  279.                 }
  280.             }
  281.  
  282.         }
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement