public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { var type = new ComputerType(); if (this.computers.Select(c => c.Id).Contains(id)) { throw new ArgumentException(ExceptionMessages.ExistingComputerId); } IComputer computer = null; if (!Enum.TryParse(computerType, out type)) { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } else if (type == ComputerType.Laptop) { computer = new Laptop(id, manufacturer, model, price); } else if (type == ComputerType.DesktopComputer) { computer = new DesktopComputer(id, manufacturer, model, price); } computers.Add(computer); return(string.Format(SuccessMessages.AddedComputer, id)); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { if (computers.Any(x => x.Id == id)) { throw new ArgumentException("Computer with this id already exists."); } IComputer computer = null; if (computerType == "DesktopComputer") { computer = new DesktopComputer(id, manufacturer, model, price); } else if (computerType == "Laptop") { computer = new Laptop(id, manufacturer, model, price); } else { throw new ArgumentException("Computer type is invalid."); } computers.Add(computer); return($"Computer with id {id} added successfully."); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { if (computers.Any(c => c.Id == id)) { throw new ArgumentException(ExceptionMessages.ExistingComputerId); } if (!IsValidComputer(computerType)) { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } IComputer computer = null; switch (computerType) { case "DesktopComputer": computer = new DesktopComputer(id, manufacturer, model, price); break; case "Laptop": computer = new Laptop(id, manufacturer, model, price); break; } computers.Add(computer); return(string.Format(SuccessMessages.AddedComputer, id)); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { if (computers.Any(x => x.Id == id)) { throw new ArgumentException(ExceptionMessages.ExistingComputerId); } IComputer currentComp = null; if (computerType == "DesktopComputer") { currentComp = new DesktopComputer(id, manufacturer, model, price); computers.Add(currentComp); return(string.Format(SuccessMessages.AddedComputer, currentComp.Id)); //return $"Computer with id {id} added successfully."; } else if (computerType == "Laptop") { currentComp = new Laptop(id, manufacturer, model, price); computers.Add(currentComp); return(string.Format(SuccessMessages.AddedComputer, currentComp.Id)); } else { //public const string InvalidComputerType = "Computer type is invalid."; throw new ArgumentException(ExceptionMessages.InvalidComputerType); } }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { ComputerType cType = new ComputerType(); IComputer computer = null; if (!DoesComputerExist(id)) { throw new ArgumentException("Computer with this id already exists."); } else if (!Enum.TryParse <ComputerType>(computerType, out cType)) { throw new ArgumentException("Computer type is invalid."); } else if (cType == ComputerType.DesktopComputer) { computer = new DesktopComputer(id, manufacturer, model, price); } else if (cType == ComputerType.Laptop) { computer = new Laptop(id, manufacturer, model, price); } computers.Add(computer); return($"Computer with id {computer.Id} added successfully."); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { IComputer comp = this.computers.FirstOrDefault(x => x.Id == id); if (comp != null) { throw new ArgumentException("Computer with this id already exists."); } if (computerType == "Laptop") { comp = new Laptop(id, manufacturer, model, price); } else if (computerType == "DesktopComputer") { comp = new DesktopComputer(id, manufacturer, model, price); } if (comp == null) { throw new ArgumentException("Computer type is invalid."); } if (this.computers.Contains(comp)) { throw new ArgumentException("Computer with this id already exists."); } this.computers.Add(comp); return($"Computer with id {id} added successfully."); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { if (this.computers.FirstOrDefault(x => x.Id == id) != null) { throw new ArgumentException(ExceptionMessages.ExistingComputerId); } IComputer computer; if (computerType == "Laptop") { computer = new Laptop(id, manufacturer, model, price); } else if (computerType == "DesktopComputer") { computer = new DesktopComputer(id, manufacturer, model, price); } else { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } this.computers.Add(computer); return(string.Format(SuccessMessages.AddedComputer, id)); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { IComputer computer = null; switch (computerType) { case nameof(DesktopComputer): computer = new DesktopComputer(id, manufacturer, model, price); break; case nameof(Laptop): computer = new Laptop(id, manufacturer, model, price); break; } if (computer == null) { throw new ArgumentException("Computer type is invalid."); } if (this.computers.Any(x => x.Id == computer.Id)) { throw new ArgumentException("Computer with this id already exists."); } this.computers.Add(computer); return($"Computer with id {id} added successfully."); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { IComputer computer = computers.FirstOrDefault(c => c.Id == id); if (computer != null) { throw new ArgumentException(string.Format(Common.Constants.ExceptionMessages.ExistingComputerId)); } IComputer comp = null; if (computerType == "Laptop") { comp = new Laptop(id, manufacturer, model, price); } else if (computerType == "DesktopComputer") { comp = new DesktopComputer(id, manufacturer, model, price); } else { throw new ArgumentException(string.Format(Common.Constants.ExceptionMessages.InvalidComputerType)); } computers.Add(comp); return(string.Format(Common.Constants.SuccessMessages.AddedComputer, id)); }
//No need to check is computer with Id exists public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { if (computers.Any(x => x.Id == id)) { string exeption = string.Format(ExceptionMessages.ExistingComputerId); throw new ArgumentException(exeption); } IComputer computer = null; if (computerType == "DesktopComputer") { computer = new DesktopComputer(id, manufacturer, model, price); } else if (computerType == "Laptop") { computer = new Laptop(id, manufacturer, model, price); } else { string exeption = string.Format(ExceptionMessages.InvalidComputerType); throw new ArgumentException(exeption); } computers.Add(computer); return(string.Format(SuccessMessages.AddedComputer, computer.Id)); }
public void ConstructorTest() { computer = new DesktopComputer(02, "dell", "P02", 100M); Assert.That(computer.Components.Count, Is.EqualTo(0)); Assert.That(computer.Peripherals.Count, Is.EqualTo(0)); }
public string AddComputer(string computerTypeName, int id, string manufacturer, string model, decimal price) { if (this.computers.Any(x => x.Id == id)) { throw new ArgumentException(ExceptionMessages.ExistingComputerId); } if (!Enum.TryParse(computerTypeName, out ComputerType computerType)) { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } IComputer computer = null; switch (computerType) { case ComputerType.DesktopComputer: computer = new DesktopComputer(id, manufacturer, model, price); break; case ComputerType.Laptop: computer = new Laptop(id, manufacturer, model, price); break; } computers.Add(computer); return(string.Format(SuccessMessages.AddedComputer, id)); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { IComputer computer = this.computers.FirstOrDefault(c => c.Id == id); //Big mistake. Forgot to make this validation and that caused NullReference Exception. if (computer != null) { throw new ArgumentException("Computer with this id already exists."); } if (computerType == Common.Enums.ComputerType.DesktopComputer.ToString()) { computer = new DesktopComputer(id, manufacturer, model, price); } else if (computerType == Common.Enums.ComputerType.Laptop.ToString()) { computer = new Laptop(id, manufacturer, model, price); } else { throw new ArgumentException("Computer type is invalid."); } this.computers.Add(computer); return($"Computer with id {id} added successfully."); }
public IActionResult Post([FromBody] DesktopComputer value) { if (ModelState.IsValid) { return(BadRequest(ModelState)); } _context.DesktopComputers.Add(value); _context.SaveChanges(); return(Ok()); }
public IComputer CreateComputer(string computerType, int id, string manufacturer, string model, decimal price) { IComputer computer = null; if (computerType == "DesktopComputer") { computer = new DesktopComputer(id, manufacturer, model, price); } else if (computerType == "Laptop") { computer = new Laptop(id, manufacturer, model, price); } return(computer); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } DesktopComputer = await _context.DesktopComputer.FirstOrDefaultAsync(m => m.Id == id); if (DesktopComputer == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> PutAsync(int id, [FromBody] DesktopComputer value) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_context.DesktopComputers.Any(d => d.Id == id)) { return(NotFound()); } _context.DesktopComputers.Update(value); await _context.SaveChangesAsync(); return(Ok()); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { bool IsValidType = Enum.TryParse <ComputerType>(computerType, out ComputerType compType); if (IsValidType) { IComputer computer = null; string message = string.Empty; switch (compType) { case ComputerType.DesktopComputer: computer = new DesktopComputer(price, model, manufacturer, id); if (computers.Any(c => c.Id == id)) { throw new ArgumentException(ExceptionMessages.ExistingComputerId); } else { computers.Add(computer); message = string.Format(SuccessMessages.AddedComputer, id); } break; case ComputerType.Laptop: computer = new Laptop(price, model, manufacturer, id); if (computers.Any(c => c.Id == id)) { throw new ArgumentException(ExceptionMessages.ExistingComputerId); } else { computers.Add(computer); message = string.Format(SuccessMessages.AddedComputer, id); } break; default: break; } return(message); } else { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } DesktopComputer = await _context.DesktopComputer.FindAsync(id); if (DesktopComputer != null) { _context.DesktopComputer.Remove(DesktopComputer); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
private static IComputer CreateComputer(string computerType, int id, string manufacturer, string model, decimal price) { IComputer computer; if (computerType == "Laptop") { computer = new Laptop(id, manufacturer, model, price); } else if (computerType == "DesktopComputer") { computer = new DesktopComputer(id, manufacturer, model, price); } else { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } return(computer); }
public IComputer CreateComputer(string computerType, int id, string manufacturer, string model, decimal price) { IComputer computer = null; if (computerType == ComputerType.DesktopComputer.ToString()) { computer = new DesktopComputer(id, manufacturer, model, price); } else if (computerType == ComputerType.Laptop.ToString()) { computer = new Laptop(id, manufacturer, model, price); } else { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } return(computer); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) //DON'T CHECK IF COMPUTER WITH ID EXISTS { IComputer computer = null; if (computerType == "DesktopComputer") { computer = new DesktopComputer(id, manufacturer, model, price); } else if (computerType == "Laptop") { computer = new Laptop(id, manufacturer, model, price); } else { throw new ArgumentException("Computer type is invalid."); } computers.Add(computer); return($"Computer with id {id} added successfully."); }
public string AddComputer(string computerType, int id, string manufacturer, string model, decimal price) { if (!Enum.IsDefined(typeof(ComputerType), 1) && !Enum.IsDefined(typeof(ComputerType), 2)) { throw new ArgumentException(ExceptionMessages.InvalidComputerType); } IComputer computer = null; if (computerType == "Laptop") { computer = new Laptop(id, manufacturer, model, price); } else if (computerType == "DesktopComputer") { computer = new DesktopComputer(id, manufacturer, model, price); } computers.Add(computer); return(string.Format(SuccessMessages.AddedComputer, id)); }
private IComputer CreateComputer(string computerType, int id, string manufacturer, string model, decimal price) { Type type = GetDefaultType(computerType); if (type == null) { throw new ArgumentException (ExceptionMessages.InvalidComputerType); } IComputer computer = null; if (computerType == ComputerType.DesktopComputer.ToString()) { computer = new DesktopComputer(id, manufacturer, model, price); } if (computerType == ComputerType.Laptop.ToString()) { computer = new Laptop(id, manufacturer, model, price); } return(computer); }
static void Main() { IComponent component = new Motherboard(1, "idik", "asd", 20m, 20, 1); IPeripheral peripheral = new Headset(1, "idik", "asd", 20m, 20, "idk"); IComputer computer = new DesktopComputer(2, "asd", "ddsds", 200); computer.AddComponent(component); computer.AddPeripheral(peripheral); computer.ToString(); string pathFile = Path.Combine("..", "..", "..", "output.txt"); File.Create(pathFile).Close(); IReader reader = new ConsoleReader(); IWriter writer = new ConsoleWriter(); ICommandInterpreter commandInterpreter = new CommandInterpreter(); IController controller = new Controller(); IEngine engine = new Engine(reader, writer, commandInterpreter, controller); engine.Run(); }
/// <summary> /// Create a new DesktopComputer object. /// </summary> /// <param name="brand">Initial value of Brand.</param> /// <param name="id">Initial value of Id.</param> public static DesktopComputer CreateDesktopComputer(string brand, int id) { DesktopComputer desktopComputer = new DesktopComputer(); desktopComputer.Brand = brand; desktopComputer.Id = id; return desktopComputer; }
public async Task AddDesktopComputer(DesktopComputer addItem) { var response = await _HttpClient.PostJsonAsync("/api/DesktopComputers", addItem); response.EnsureSuccessStatusCode(); }
public async Task PutDesktopComputer(DesktopComputer desktopComputer) { var response = await _HttpClient.PutJsonAsync($"/api/DesktopComputers/{desktopComputer.Id}", desktopComputer); response.EnsureSuccessStatusCode(); }