/* * * Send a GET request to the server to get the ATM state. * */ private async Task <ATM> GetATMBills() { ATM atm = null; using (HttpResponseMessage response = await client.GetAsync($"api/atm")) { if (response.IsSuccessStatusCode) { // get data as a JSON string string data = await response.Content.ReadAsStringAsync(); // deserialize response to User class atm = JsonConvert.DeserializeObject <ATM>(data); } } return(atm); }
/* * * Send a POST request to update the ATM state. * */ private async Task UpdateATM(ATM atm) { // create a JSON representing the ATM. string userRequest = "{\"Pennies\":\"" + atm.Pennies + "\"," + "\"Nickels\":\"" + atm.Nickels + "\"," + "\"Dimes\":\"" + atm.Dimes + "\"," + "\"Quarters\":\"" + atm.Quarters + "\"," + "\"Ones\":\"" + atm.Ones + "\"," + "\"Fives\":\"" + atm.Fives + "\"," + "\"Tens\":\"" + atm.Tens + "\"," + "\"Twenties\":\"" + atm.Twenties + "\"," + "\"Fifties\":\"" + atm.Fifties + "\"}"; // convert the JSON string to httpContent so we can send it using HttpResponseMessage var httpContent = new StringContent(userRequest, Encoding.UTF8, "application/json"); using (HttpResponseMessage response = await client.PostAsync($"api/atm", httpContent)) { // nothing } }
/* * * Update the ATM balance label to reflect the state of the given ATM. * */ private void setLblATMBalance(ATM atm) { lblATMBalance.Text = "ATM Balance: " + "$0.01 - " + atm.Pennies + " | $0.05 - " + atm.Nickels + " | 0.10 - " + atm.Dimes + " | 0.25 - " + atm.Quarters + " | $1 - " + atm.Ones + " | $5 - " + atm.Fives + " | $10 - " + atm.Tens + " | $20 - " + atm.Twenties + " | $50 - " + atm.Fifties; }
/* * * Called when the user clicks the "deposit" button from the deposit form. * Initiates the deposit process. * */ protected void Deposit_Click(object sender, EventArgs e) { clearAlerts(); Button btn = (Button)sender; string id = btn.ID; ATM atm = (ATM)Session["atm"]; decimal depositAmt = 0; // update the amount of bills the ATM contains switch (id) { case "btn_01": depositAmt = 0.01M; atm.Pennies++; break; case "btn_05": depositAmt = 0.05M; atm.Nickels++; break; case "btn_010": depositAmt = 0.10M; atm.Dimes++; break; case "btn_025": depositAmt = 0.25M; atm.Quarters++; break; case "btn_1": depositAmt = 1; atm.Ones++; break; case "btn_5": depositAmt = 5; atm.Fives++; break; case "btn_10": depositAmt = 10; atm.Tens++; break; case "btn_20": depositAmt = 20; atm.Twenties++; break; case "btn_50": depositAmt = 50; atm.Fifties++; break; default: break; } if (depositAmt > 0) { // update the user to their new balance amount ((User)Session["loggedInUser"]).Balance += depositAmt; // POST the updated user balance to the server UpdateUser((User)Session["loggedInUser"]).GetAwaiter(); // POST the udpated ATM balance to the server UpdateATM((ATM)Session["atm"]).GetAwaiter(); // display success message formDepositSuccess.Visible = true; } }