public DTO performOperation(DTO dto) { DTO answer = null; if (dto is MoneyDTO) { MoneyDTO moneyDTO = dto as MoneyDTO; switch (dto.getOperationType()) { case 2: answer = Deposit(moneyDTO); break; case 3: answer = Withdraw(moneyDTO); break; case 4: answer = GetBalance(moneyDTO); break; default: throw new Exception("Incorrect operation type"); } } else { throw new Exception("Incorrect DTO"); } return(answer); }
public void connectionHandling() { Console.WriteLine("Tworze watek dla polaczenia"); int max = 65565; Byte[] bytes = new Byte[max]; int i; bool loged = false; AccConnectionController accConnectionController = new AccConnectionController(db); AccOperationController accOperationController = null; Account account = null; DTO answer; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { string className = ByteParser.byteToType(bytes); DtoFactory dtoFactory = new DtoFactory(); DTO dto = dtoFactory.getDTO(className); dto.fromByteArray(bytes); answer = null; if (dto != null) { if (loged == false) { answer = DTOUtil.Login(accConnectionController, ref account, ref loged, dto); } else if (loged == true && dto.getOperationType() == 1) { answer = DTOUtil.Login(accConnectionController, ref account, ref loged, dto); } else { if (dto.getOperationType() == 5) { answer = DTOUtil.Logout(dto, ref account, ref loged); } else { answer = DTOUtil.OperateOnAccount(accOperationController, account, dto); } } } byte[] data = answer.toByteArray(); stream.Write(data, 0, data.Length); } client.Close(); }
public static DTO Logout(DTO dto, ref Account account, ref bool loged) { DTO answer = null; account = null; loged = false; answer = new MessageDTO(dto.getOperationType(), "Loged out correctly", true); return(answer); }
public static DTO Login(AccConnectionController accConnectionController, ref Account account, ref bool loged, DTO dto) { MessageDTO messageDTO = null; if (dto is UserDTO && dto.getOperationType() == 1) { if (loged == true && dto.getOperationType() == 1) { messageDTO = new MessageDTO(dto.getOperationType(), "You are already logged", false); } try { account = accConnectionController.LogIn(dto); loged = true; messageDTO = new MessageDTO(dto.getOperationType(), "Welcome! You are now loged in.", true); } catch (Exception ex) { string msg = ex.Message; messageDTO = new MessageDTO(dto.getOperationType(), msg, false); } } else { messageDTO = new MessageDTO(dto.getOperationType(), "First you must log in!", false); } return(messageDTO); }