sashomaga

OOP

Feb 20th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public enum BatteryType
  5. {
  6.     LiIon, NiMH, NiCd
  7. }
  8.  
  9. public class Battery
  10. {
  11.     //fields
  12.     private string model;
  13.     private int hoursIdle;
  14.     private int hoursTalk;
  15.     private BatteryType currentBatteryType;
  16.     //properties
  17.     public string Model
  18.     {
  19.         set
  20.         {
  21.             Validation.ValidateString(value);
  22.             this.model = value;
  23.         }
  24.         get { return this.model; }
  25.     }
  26.     public int HoursIdle
  27.     {
  28.         set
  29.         {
  30.             Validation.ValidateInt(value);
  31.             this.hoursIdle = value;
  32.         }
  33.         get { return this.hoursIdle; }
  34.     }
  35.     public int HoursTalk
  36.     {
  37.         set
  38.         {
  39.             Validation.ValidateInt(value);
  40.             this.hoursTalk = value;
  41.         }
  42.         get { return this.hoursTalk; }
  43.     }
  44.     public string CurrentBatteryType
  45.     {
  46.         set
  47.         {
  48.             Validation.ValidateEnum(value);
  49.             this.currentBatteryType = (BatteryType)Enum.Parse(typeof(BatteryType), value);
  50.         }
  51.         get { return this.currentBatteryType.ToString(); }
  52.     }
  53.    
  54.  
  55.     //constructors
  56.     public Battery()
  57.     {
  58.  
  59.     }
  60.  
  61.     public Battery(string model)
  62.         : this(model, 0)
  63.     {
  64.        
  65.     }
  66.  
  67.     public Battery(string model, int hoursIdle)
  68.         : this(model, hoursIdle, 0)      
  69.     {
  70.  
  71.     }
  72.  
  73.     public Battery(string model, int hoursIdle, int hoursTalk)
  74.         : this(model, hoursIdle, hoursTalk, "LiIon")
  75.     {
  76.  
  77.     }
  78.  
  79.     public Battery(string model, int hoursIdle, int hoursTalk, string currentBatteryTypeString)
  80.     {        
  81.         this.Model = model;
  82.         this.HoursIdle = hoursIdle;
  83.         this.HoursTalk = hoursTalk;
  84.         this.CurrentBatteryType = currentBatteryTypeString;
  85.     }
  86.  
  87. }
  88. /***********************************************************************************************/
  89. using System;
  90. using System.Collections.Generic;
  91.  
  92. public class Display
  93. {
  94.     private int size;
  95.     private int numberOfColors;
  96.  
  97.     public int NumberOfColors
  98.     {
  99.         set
  100.         {
  101.             Validation.ValidateInt(value);
  102.             this.numberOfColors = value;
  103.         }        
  104.         get { return this.numberOfColors; }
  105.     }
  106.  
  107.     public int Size
  108.     {
  109.         set
  110.         {
  111.             Validation.ValidateInt(value);
  112.             this.size = value;
  113.         }
  114.         get { return this.size; }
  115.     }
  116.    
  117.     //constructors
  118.     public Display()
  119.     {
  120.  
  121.     }
  122.  
  123.     public Display(int size) : this(size, 0)
  124.     {
  125.  
  126.     }
  127.  
  128.     public Display(int size, int numberOfColors)
  129.     {
  130.         this.Size = size;
  131.         this.NumberOfColors = numberOfColors;
  132.     }
  133.    
  134. }
  135. /***********************************************************************************************/
  136. using System;
  137. using System.Collections.Generic;
  138.  
  139. public class GSM
  140. {
  141.     //fields
  142.     private string model;
  143.     private string manufacturer;
  144.     private int price;
  145.     private string owner;
  146.     private Display currentDisplay;
  147.     private Battery currentBattery;
  148.     private List<Call> callHistory;
  149.     // static fields
  150.     private static string iPhone4S;
  151.     //properties
  152.     public string Model
  153.     {
  154.         set
  155.         {
  156.             Validation.ValidateString(value);
  157.             this.model = value;
  158.         }
  159.         get {return this.model;}
  160.     }
  161.     public string Manufacturer
  162.     {
  163.         set
  164.         {
  165.             Validation.ValidateString(value);
  166.             this.manufacturer = value;
  167.         }
  168.         get { return this.manufacturer; }
  169.     }
  170.     public int Price
  171.     {
  172.         set
  173.         {
  174.             Validation.ValidateInt(value);
  175.             this.price = value;
  176.         }
  177.         get { return this.price; }
  178.     }
  179.     public string Owner
  180.     {
  181.         set
  182.         {
  183.             Validation.ValidateString(value);
  184.             this.owner = value;
  185.         }
  186.         get { return this.owner; }
  187.     }  
  188.     public List<Call> CallHistory
  189.     {
  190.         get { return callHistory;}
  191.     }
  192.     //static properties
  193.     public static string IPhone4S
  194.     {
  195.         set
  196.         {
  197.             Validation.ValidateString(value);
  198.             GSM.iPhone4S = value;
  199.         }
  200.         get { return GSM.iPhone4S; }
  201.     }
  202.     //constructors
  203.     public GSM(string model, string manufacturer)
  204.         : this(model, manufacturer, 0)
  205.     {
  206.  
  207.     }
  208.  
  209.     public GSM(string model, string manufacturer, int price)
  210.         : this(model, manufacturer, price, null)
  211.     {
  212.  
  213.     }
  214.  
  215.     public GSM(string model, string manufacturer, int price, string owner)
  216.         : this(model, manufacturer, price, owner, null)
  217.     {
  218.  
  219.     }
  220.  
  221.     public GSM(string model, string manufacturer, int price, string owner, Display currentDisplay)
  222.         : this(model, manufacturer, price, owner, currentDisplay, null)
  223.     {
  224.  
  225.     }
  226.  
  227.     public GSM(string model, string manufacturer, int price, string owner, Display currentDisplay, Battery currentBattery)
  228.     {
  229.         this.Model = model;
  230.         this.Manufacturer = manufacturer;
  231.         this.Price = price;
  232.         this.Owner = owner;
  233.         this.currentDisplay = currentDisplay;
  234.         this.currentBattery = currentBattery;
  235.         this.callHistory = new List<Call>();
  236.     }
  237.     //methods
  238.     public void DeleteCall(Call currentCall)
  239.     {
  240.         if (this.callHistory.Contains(currentCall))
  241.         {
  242.             this.callHistory.Remove(currentCall);
  243.         }
  244.         else
  245.         {
  246.             throw new ArgumentException("element don't exist in collection");
  247.         }
  248.     }
  249.     public void AddCall(Call currentCall)
  250.     {
  251.         this.callHistory.Add(currentCall);
  252.     }
  253.     public void ClearCallHistory()
  254.     {
  255.         this.callHistory = new List<Call>();
  256.     }
  257.     public double CalcTotalPriseOfCalls(double minuteCost)
  258.     {
  259.         double result = 0;
  260.         foreach (Call item in this.callHistory)
  261.         {
  262.             result += item.Duration;
  263.         }
  264.         return (result / 60d * minuteCost);
  265.     }
  266.  
  267.     //override ToString
  268.     public override string ToString()
  269.     {
  270.         return string.Format("Model: {0}, Manufactor: {1}, Price: {2}, owner: {3}, display-size: {4}, display-number of colors {5}, battery-model: {6}, battery-hoursIdle {7}, battery-hoursTalk {8}, battery-type {9}"
  271.             , this.model, this.manufacturer, this.price, this.owner, this.currentDisplay.NumberOfColors, this.currentDisplay.Size,this.currentBattery.Model,this.currentBattery.HoursIdle,
  272.             this.currentBattery.HoursTalk,this.currentBattery.CurrentBatteryType);
  273.  
  274.     }
  275.  
  276. }
  277. /***********************************************************************************************/
  278. using System;
  279. using System.Collections.Generic;
  280. using System.Linq;
  281. using System.Text;
  282. //Write a class GSMCallHistoryTest to test the call history functionality of the GSM class.
  283. //Create an instance of the GSM class.
  284. //Add few calls.
  285. //Display the information about the calls.
  286. //Assuming that the price per minute is 0.37 calculate and print the total price of the calls in the history.
  287. //Remove the longest call from the history and calculate the total price again.
  288. //Finally clear the call history and print it
  289. public class GSMCallHistoryTest
  290. {
  291.     static void Main()
  292.     {
  293.         try
  294.         {
  295.             Battery testBattery = new Battery("toshiba", 4, 5, "NiCd");
  296.             Display testDisplay = new Display(10, 8);
  297.             GSM testGsm = new GSM("n52", "Nokia", 500, "Sasho", testDisplay, testBattery);
  298.  
  299.             Call currentCall = new Call("12.02.2013", "12:45:10", "0888123456", 30);
  300.             testGsm.AddCall(currentCall);
  301.             currentCall = new Call("12.02.2013", "13:45:10", "0888123456", 200);
  302.             testGsm.AddCall(currentCall);
  303.             currentCall = new Call("15.12.2013", "14:45:10", "0888888888", 500);
  304.             testGsm.AddCall(currentCall);
  305.  
  306.             DisplayInformationForCalls(testGsm.CallHistory);
  307.  
  308.             Console.WriteLine("Total price:");
  309.             Console.WriteLine("{0:c}",testGsm.CalcTotalPriseOfCalls(0.37));
  310.  
  311.             var sorted = testGsm.CallHistory.OrderByDescending(x => x.Duration).ToList();
  312.             testGsm.DeleteCall(sorted[0]);
  313.  
  314.             Console.WriteLine("Total price:");
  315.             Console.WriteLine("{0:c}", testGsm.CalcTotalPriseOfCalls(0.37));
  316.  
  317.             DisplayInformationForCalls(testGsm.CallHistory);
  318.             testGsm.ClearCallHistory();
  319.  
  320.  
  321.         }
  322.         catch (ArgumentException err)
  323.         {
  324.             Console.WriteLine(err.Message);
  325.         }
  326.     }
  327.  
  328.     private static void DisplayInformationForCalls(List<Call> calls)
  329.     {
  330.         Console.WriteLine("All calls:");
  331.         foreach (Call item in calls)
  332.         {
  333.             Console.WriteLine("Date: {0}, Time: {1}, Number: {2}, Duration {3} sec", item.Date.ToShortDateString(), item.Time.TimeOfDay, item.DialedPhoneNumber, item.Duration);
  334.         }
  335.     }
  336. }
  337. /***********************************************************************************************/
  338. using System;
  339. using System.Collections.Generic;
  340. using System.Linq;
  341. using System.Text;
  342.  
  343.     public class GSMTest
  344.     {
  345.         static void _Main() //rename to test
  346.         {
  347.             try
  348.             {
  349.                 ////test ToString()
  350.                 //Battery testBattery = new Battery("toshiba", 4, 5, "NiCd");
  351.                 //Display testDisplay = new Display(-1, 8);
  352.                 //GSM testGsm = new GSM("n52","Nokia",500,"Sasho",testDisplay,testBattery);
  353.                 //Console.WriteLine(testGsm.ToString());
  354.  
  355.                
  356.                
  357.                 //    Battery testBattery = new Battery();
  358.                 //    testBattery.CurrentBatteryType = "LiIon"; //BatteryType.LiIon                
  359.  
  360.                 Battery testBattery = new Battery("toshiba", 4, 5, "NiCd");
  361.                 Display testDisplay = new Display(10, 8);
  362.                 GSM testGsm = new GSM("n52", "Nokia", 500, "Sasho", testDisplay, testBattery);
  363.  
  364.                 //GSM[] gsmArray = new GSM[3];
  365.                 //gsmArray[0] = testGsm;
  366.                 //gsmArray[1] = new GSM("n1", "Sony", 1500, "Gosho", testDisplay);
  367.                 //gsmArray[2] = new GSM("n99", "NEC");
  368.                 //GSM.IPhone4S = "best IPhone4S";
  369.  
  370.                 Call currentCall = new Call("12.02.2013", "12:45:10", "0888123456", 30);
  371.                 testGsm.AddCall(currentCall);
  372.                 currentCall = new Call("12.02.2013", "13:45:10", "0888123456", 200);
  373.                 testGsm.AddCall(currentCall);
  374.                 Console.WriteLine(testGsm.CalcTotalPriseOfCalls(1.20));
  375.  
  376.             }
  377.             catch(ArgumentException err)
  378.             {
  379.                 Console.WriteLine(err.Message);
  380.             }
  381.            
  382.  
  383.  
  384.         }
  385.     }
  386. /***********************************************************************************************/
  387. using System;
  388. using System.Collections.Generic;
  389. using System.Linq;
  390. using System.Text;
  391.  
  392. public static class Validation
  393. {
  394.     //validation int
  395.     public static void ValidateInt(params int[] value)
  396.     {
  397.         foreach (int item in value)
  398.         {
  399.             if (item < 0)
  400.             {
  401.                 throw new ArgumentException("Value must be positive");
  402.             }
  403.         }
  404.     }
  405.     //validation string
  406.     public static void ValidateString(params string[] value)
  407.     {
  408.         foreach (string item in value)
  409.         {
  410.             if (item!=null && item.Length < 1 && item.Length > 30)
  411.             {
  412.                 throw new ArgumentException("Value lenght must be > 1 and < 30");
  413.             }
  414.         }
  415.     }
  416.     //validation enum
  417.     public static void ValidateEnum(string value)
  418.     {
  419.         BatteryType enumValue;
  420.         if (Enum.TryParse(value, true, out enumValue))
  421.         {
  422.             if (Enum.IsDefined(typeof(BatteryType), enumValue))
  423.             {
  424.                 return;
  425.             }
  426.             else
  427.             {
  428.                 throw new ArgumentException("Value not a member of the BatteryType enumeration");
  429.             }          
  430.         }
  431.         else
  432.         {
  433.             throw new ArgumentException("Value not a member of the BatteryType enumeration");
  434.         }
  435.  
  436.     }
  437. }
  438. /***********************************************************************************************/
  439. using System;
  440. using System.Collections.Generic;
  441. using System.Linq;
  442. using System.Text;
  443. //Create a class Call to hold a call performed through a GSM. It should contain date, time, dialed phone number and duration (in seconds).
  444.  
  445. public class Call
  446. {
  447.     private DateTime date;
  448.     private DateTime time;
  449.     private string dialedPhoneNumber;
  450.     private int duration;
  451.  
  452.     public int Duration
  453.     {
  454.         get { return this.duration; }
  455.     }
  456.     public DateTime Date
  457.     {
  458.         get { return this.date; }
  459.     }
  460.     public DateTime Time
  461.     {
  462.         get { return this.time; }
  463.     }
  464.     public string DialedPhoneNumber
  465.     {
  466.         get { return this.dialedPhoneNumber; }
  467.     }
  468.     public Call(string date, string time, string dialedPhoneNumber, int duration)
  469.     {
  470.         this.date = DateTime.Parse(date);
  471.         this.time = DateTime.Parse(time);
  472.         this.dialedPhoneNumber = dialedPhoneNumber;
  473.         this.duration = duration;
  474.     }
  475. }
Advertisement
Add Comment
Please, Sign In to add comment