Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public enum BatteryType
- {
- LiIon, NiMH, NiCd
- }
- public class Battery
- {
- //fields
- private string model;
- private int hoursIdle;
- private int hoursTalk;
- private BatteryType currentBatteryType;
- //properties
- public string Model
- {
- set
- {
- Validation.ValidateString(value);
- this.model = value;
- }
- get { return this.model; }
- }
- public int HoursIdle
- {
- set
- {
- Validation.ValidateInt(value);
- this.hoursIdle = value;
- }
- get { return this.hoursIdle; }
- }
- public int HoursTalk
- {
- set
- {
- Validation.ValidateInt(value);
- this.hoursTalk = value;
- }
- get { return this.hoursTalk; }
- }
- public string CurrentBatteryType
- {
- set
- {
- Validation.ValidateEnum(value);
- this.currentBatteryType = (BatteryType)Enum.Parse(typeof(BatteryType), value);
- }
- get { return this.currentBatteryType.ToString(); }
- }
- //constructors
- public Battery()
- {
- }
- public Battery(string model)
- : this(model, 0)
- {
- }
- public Battery(string model, int hoursIdle)
- : this(model, hoursIdle, 0)
- {
- }
- public Battery(string model, int hoursIdle, int hoursTalk)
- : this(model, hoursIdle, hoursTalk, "LiIon")
- {
- }
- public Battery(string model, int hoursIdle, int hoursTalk, string currentBatteryTypeString)
- {
- this.Model = model;
- this.HoursIdle = hoursIdle;
- this.HoursTalk = hoursTalk;
- this.CurrentBatteryType = currentBatteryTypeString;
- }
- }
- /***********************************************************************************************/
- using System;
- using System.Collections.Generic;
- public class Display
- {
- private int size;
- private int numberOfColors;
- public int NumberOfColors
- {
- set
- {
- Validation.ValidateInt(value);
- this.numberOfColors = value;
- }
- get { return this.numberOfColors; }
- }
- public int Size
- {
- set
- {
- Validation.ValidateInt(value);
- this.size = value;
- }
- get { return this.size; }
- }
- //constructors
- public Display()
- {
- }
- public Display(int size) : this(size, 0)
- {
- }
- public Display(int size, int numberOfColors)
- {
- this.Size = size;
- this.NumberOfColors = numberOfColors;
- }
- }
- /***********************************************************************************************/
- using System;
- using System.Collections.Generic;
- public class GSM
- {
- //fields
- private string model;
- private string manufacturer;
- private int price;
- private string owner;
- private Display currentDisplay;
- private Battery currentBattery;
- private List<Call> callHistory;
- // static fields
- private static string iPhone4S;
- //properties
- public string Model
- {
- set
- {
- Validation.ValidateString(value);
- this.model = value;
- }
- get {return this.model;}
- }
- public string Manufacturer
- {
- set
- {
- Validation.ValidateString(value);
- this.manufacturer = value;
- }
- get { return this.manufacturer; }
- }
- public int Price
- {
- set
- {
- Validation.ValidateInt(value);
- this.price = value;
- }
- get { return this.price; }
- }
- public string Owner
- {
- set
- {
- Validation.ValidateString(value);
- this.owner = value;
- }
- get { return this.owner; }
- }
- public List<Call> CallHistory
- {
- get { return callHistory;}
- }
- //static properties
- public static string IPhone4S
- {
- set
- {
- Validation.ValidateString(value);
- GSM.iPhone4S = value;
- }
- get { return GSM.iPhone4S; }
- }
- //constructors
- public GSM(string model, string manufacturer)
- : this(model, manufacturer, 0)
- {
- }
- public GSM(string model, string manufacturer, int price)
- : this(model, manufacturer, price, null)
- {
- }
- public GSM(string model, string manufacturer, int price, string owner)
- : this(model, manufacturer, price, owner, null)
- {
- }
- public GSM(string model, string manufacturer, int price, string owner, Display currentDisplay)
- : this(model, manufacturer, price, owner, currentDisplay, null)
- {
- }
- public GSM(string model, string manufacturer, int price, string owner, Display currentDisplay, Battery currentBattery)
- {
- this.Model = model;
- this.Manufacturer = manufacturer;
- this.Price = price;
- this.Owner = owner;
- this.currentDisplay = currentDisplay;
- this.currentBattery = currentBattery;
- this.callHistory = new List<Call>();
- }
- //methods
- public void DeleteCall(Call currentCall)
- {
- if (this.callHistory.Contains(currentCall))
- {
- this.callHistory.Remove(currentCall);
- }
- else
- {
- throw new ArgumentException("element don't exist in collection");
- }
- }
- public void AddCall(Call currentCall)
- {
- this.callHistory.Add(currentCall);
- }
- public void ClearCallHistory()
- {
- this.callHistory = new List<Call>();
- }
- public double CalcTotalPriseOfCalls(double minuteCost)
- {
- double result = 0;
- foreach (Call item in this.callHistory)
- {
- result += item.Duration;
- }
- return (result / 60d * minuteCost);
- }
- //override ToString
- public override string ToString()
- {
- 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}"
- , this.model, this.manufacturer, this.price, this.owner, this.currentDisplay.NumberOfColors, this.currentDisplay.Size,this.currentBattery.Model,this.currentBattery.HoursIdle,
- this.currentBattery.HoursTalk,this.currentBattery.CurrentBatteryType);
- }
- }
- /***********************************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //Write a class GSMCallHistoryTest to test the call history functionality of the GSM class.
- //Create an instance of the GSM class.
- //Add few calls.
- //Display the information about the calls.
- //Assuming that the price per minute is 0.37 calculate and print the total price of the calls in the history.
- //Remove the longest call from the history and calculate the total price again.
- //Finally clear the call history and print it
- public class GSMCallHistoryTest
- {
- static void Main()
- {
- try
- {
- Battery testBattery = new Battery("toshiba", 4, 5, "NiCd");
- Display testDisplay = new Display(10, 8);
- GSM testGsm = new GSM("n52", "Nokia", 500, "Sasho", testDisplay, testBattery);
- Call currentCall = new Call("12.02.2013", "12:45:10", "0888123456", 30);
- testGsm.AddCall(currentCall);
- currentCall = new Call("12.02.2013", "13:45:10", "0888123456", 200);
- testGsm.AddCall(currentCall);
- currentCall = new Call("15.12.2013", "14:45:10", "0888888888", 500);
- testGsm.AddCall(currentCall);
- DisplayInformationForCalls(testGsm.CallHistory);
- Console.WriteLine("Total price:");
- Console.WriteLine("{0:c}",testGsm.CalcTotalPriseOfCalls(0.37));
- var sorted = testGsm.CallHistory.OrderByDescending(x => x.Duration).ToList();
- testGsm.DeleteCall(sorted[0]);
- Console.WriteLine("Total price:");
- Console.WriteLine("{0:c}", testGsm.CalcTotalPriseOfCalls(0.37));
- DisplayInformationForCalls(testGsm.CallHistory);
- testGsm.ClearCallHistory();
- }
- catch (ArgumentException err)
- {
- Console.WriteLine(err.Message);
- }
- }
- private static void DisplayInformationForCalls(List<Call> calls)
- {
- Console.WriteLine("All calls:");
- foreach (Call item in calls)
- {
- Console.WriteLine("Date: {0}, Time: {1}, Number: {2}, Duration {3} sec", item.Date.ToShortDateString(), item.Time.TimeOfDay, item.DialedPhoneNumber, item.Duration);
- }
- }
- }
- /***********************************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class GSMTest
- {
- static void _Main() //rename to test
- {
- try
- {
- ////test ToString()
- //Battery testBattery = new Battery("toshiba", 4, 5, "NiCd");
- //Display testDisplay = new Display(-1, 8);
- //GSM testGsm = new GSM("n52","Nokia",500,"Sasho",testDisplay,testBattery);
- //Console.WriteLine(testGsm.ToString());
- // Battery testBattery = new Battery();
- // testBattery.CurrentBatteryType = "LiIon"; //BatteryType.LiIon
- Battery testBattery = new Battery("toshiba", 4, 5, "NiCd");
- Display testDisplay = new Display(10, 8);
- GSM testGsm = new GSM("n52", "Nokia", 500, "Sasho", testDisplay, testBattery);
- //GSM[] gsmArray = new GSM[3];
- //gsmArray[0] = testGsm;
- //gsmArray[1] = new GSM("n1", "Sony", 1500, "Gosho", testDisplay);
- //gsmArray[2] = new GSM("n99", "NEC");
- //GSM.IPhone4S = "best IPhone4S";
- Call currentCall = new Call("12.02.2013", "12:45:10", "0888123456", 30);
- testGsm.AddCall(currentCall);
- currentCall = new Call("12.02.2013", "13:45:10", "0888123456", 200);
- testGsm.AddCall(currentCall);
- Console.WriteLine(testGsm.CalcTotalPriseOfCalls(1.20));
- }
- catch(ArgumentException err)
- {
- Console.WriteLine(err.Message);
- }
- }
- }
- /***********************************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public static class Validation
- {
- //validation int
- public static void ValidateInt(params int[] value)
- {
- foreach (int item in value)
- {
- if (item < 0)
- {
- throw new ArgumentException("Value must be positive");
- }
- }
- }
- //validation string
- public static void ValidateString(params string[] value)
- {
- foreach (string item in value)
- {
- if (item!=null && item.Length < 1 && item.Length > 30)
- {
- throw new ArgumentException("Value lenght must be > 1 and < 30");
- }
- }
- }
- //validation enum
- public static void ValidateEnum(string value)
- {
- BatteryType enumValue;
- if (Enum.TryParse(value, true, out enumValue))
- {
- if (Enum.IsDefined(typeof(BatteryType), enumValue))
- {
- return;
- }
- else
- {
- throw new ArgumentException("Value not a member of the BatteryType enumeration");
- }
- }
- else
- {
- throw new ArgumentException("Value not a member of the BatteryType enumeration");
- }
- }
- }
- /***********************************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //Create a class Call to hold a call performed through a GSM. It should contain date, time, dialed phone number and duration (in seconds).
- public class Call
- {
- private DateTime date;
- private DateTime time;
- private string dialedPhoneNumber;
- private int duration;
- public int Duration
- {
- get { return this.duration; }
- }
- public DateTime Date
- {
- get { return this.date; }
- }
- public DateTime Time
- {
- get { return this.time; }
- }
- public string DialedPhoneNumber
- {
- get { return this.dialedPhoneNumber; }
- }
- public Call(string date, string time, string dialedPhoneNumber, int duration)
- {
- this.date = DateTime.Parse(date);
- this.time = DateTime.Parse(time);
- this.dialedPhoneNumber = dialedPhoneNumber;
- this.duration = duration;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment