Advertisement
Guest User

Unit Testing - 16 August OOP Exam

a guest
Aug 26th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Computers
  6. {
  7. public class ComputerManager
  8. {
  9. private const string CanNotBeNullMessage = "Can not be null!";
  10.  
  11. private readonly List<Computer> computers;
  12.  
  13. public ComputerManager()
  14. {
  15. this.computers = new List<Computer>();
  16. }
  17.  
  18. public IReadOnlyCollection<Computer> Computers => this.computers.AsReadOnly();
  19.  
  20. public int Count => this.computers.Count;
  21.  
  22. public void AddComputer(Computer computer)
  23. {
  24. this.ValidateNullValue(computer, nameof(computer), CanNotBeNullMessage);
  25.  
  26. if (this.computers.Any(c => c.Manufacturer == computer.Manufacturer && c.Model == computer.Model))
  27. {
  28. throw new ArgumentException("This computer already exists.");
  29. }
  30.  
  31. this.computers.Add(computer);
  32. }
  33.  
  34. public Computer RemoveComputer(string manufacturer, string model)
  35. {
  36. Computer computer = this.GetComputer(manufacturer, model);
  37.  
  38. this.computers.Remove(computer);
  39. return computer;
  40. }
  41.  
  42. public Computer GetComputer(string manufacturer, string model)
  43. {
  44. this.ValidateNullValue(manufacturer, nameof(manufacturer), CanNotBeNullMessage);
  45. this.ValidateNullValue(model, nameof(model), CanNotBeNullMessage);
  46.  
  47. Computer computer = this.computers
  48. .FirstOrDefault(c => c.Manufacturer == manufacturer && c.Model == model);
  49.  
  50. if (computer == null)
  51. {
  52. throw new ArgumentException("There is no computer with this manufacturer and model.");
  53. }
  54.  
  55. return computer;
  56. }
  57.  
  58. public ICollection<Computer> GetComputersByManufacturer(string manufacturer)
  59. {
  60. this.ValidateNullValue(manufacturer, nameof(manufacturer), CanNotBeNullMessage);
  61.  
  62. ICollection<Computer> computers = this.computers
  63. .Where(c => c.Manufacturer == manufacturer)
  64. .ToList();
  65.  
  66. return computers;
  67. }
  68.  
  69. private void ValidateNullValue(object variable, string variableName, string exceptionMessage)
  70. {
  71. if (variable == null)
  72. {
  73. throw new ArgumentNullException(variableName, exceptionMessage);
  74. }
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement