public static Account GetAccount(String ownerName, String PIN) { String record; Account gotAccount = new Account(); _Account account = null; var dbPath = gotAccount.DataBasePath; using (var sr = new StreamReader(dbPath)) { while ((record = sr.ReadLine()) != null) { account = JsonConvert.DeserializeObject <_Account>(record); if (account.OwnerName == ownerName && account.Pin == PIN) { return(new Account() { Id = account.Id, OwnerName = account.OwnerName, Pin = account.Pin, Balance = account.Balance, CreatedOn = account.CreatedOn, }); } } } return(null); }
public void SaveAsNewAccount() { var newOwner = this.OwnerName; var newPin = this.Pin; var exists = Account.GetAccount(newOwner, newPin); if (exists != null) { throw new ApplicationException("Account already exists"); } var toSave = new _Account() { Id = this.Id, Pin = this.Pin, // need to encrypt OwnerName = this.OwnerName, Balance = this.Balance, CreatedOn = DateTime.Now }; string json = JsonConvert.SerializeObject(toSave); Console.WriteLine($"Saved new account for: {newOwner}\n"); var dbPath = this.DataBasePath; // save to file using (StreamWriter accountDbFile = File.AppendText(dbPath)) { accountDbFile.WriteLine(json); } }
private void UpdateBalance(float newBalance) { String record; _Account account = null; var dbPath = this.DataBasePath; using (var sr = new StreamReader(dbPath)) { while ((record = sr.ReadLine()) != null) { account = JsonConvert.DeserializeObject <_Account>(record); if (account.Id == this.Id) { break; } } } account.Balance = newBalance; var newRecord = JsonConvert.SerializeObject(account); string text = File.ReadAllText(dbPath); text = text.Replace(record, newRecord); File.WriteAllText(dbPath, text); }