LAB 6: Sessions and Collections

Verified

Added on  2019/09/20

|4
|860
|356
Practical Assignment
AI Summary
This practical assignment, Lab 6: Working with Sessions and Collections, involves building several C# web applications using ASP.NET Web Forms. Part A requires building an application similar to one presented in lecture notes and commenting code from other provided applications. Part B focuses on creating a "Basket of Fruit" application using a Fruit class and sessions to store a list of fruits. Part C modifies a previous lab's application to handle multiple people's details using sessions and a list. Finally, Part D challenges students to create a "Beverage" class to calculate beverage costs based on fruit selection, quantity, and additional costs, integrating it with the "Basket of Fruit" application. The assignment emphasizes understanding and utilizing sessions and collections in a web application context, along with object-oriented programming principles.
Document Page
LAB 6 Working with Sessions and Collections
Please upload Part B, C and D applications from this lab in your weekly upload
as part of your logbook. Delete the “packages” file from your solution folder
before compressing and uploading the applications.
Important note: The .NET Web Forms applications are big in size and you will
run out of memory space on your drives if you create many projects of this
type. Please zip up your applications and keep them as compressed files in
your folders (delete the unzipped ones).
Part A – playing with code
The following tasks are compulsory before you attempt part B:
1. Build a similar application as the one presented in the lecture notes,
slides16-23. If you get stuck the solution is available on your course (“Using
Sessions” application).
2. Open all the other applications available on the course page for this week
and comment the C# code
Part B - Basket of Fruit application
Study the following code:
01 public class Fruit {
02 private string fName;
03 private int grams, calsPerGram;
04 private bool edible;
05 public Fruit(string n,int g, int c, bool e) {
06 grams = g;
07 calsPerGram = c;
08 edible = e;
09 fName = n;
10 }
11 public int totalCalories() {
12 return grams * calsPerGram;
13 }
14 public string getFruitInfo() {
15 string s;
16 if (edible == true) {
17 s = fName + " is yummy and it has " + totalCalories()
+
"calories";
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
18 }
19 else {
20 s = "Hands off! Not edible";
21 }
22 return s;
23 }
24 }
Document Page
25 public partial class _Default : System.Web.UI.Page {
26 List<Fruit> myBasket;
27 protected void Page_Load(object sender, EventArgs e) {
28 if (!Page.IsPostBack) {
29 myBasket = new List<Fruit>();
30 Session["listSession"] = myBasket;
31 }
32 }
33 protected void Button1_Click(object sender, EventArgs e) {
34 ListBox1.Items.Clear();
35 Fruit f = new Fruit(TbxName.Text,
int.Parse(TbxWeight.Text),
int.Parse(TbxCal.Text),
CheckBox1.Checked);
36 myBasket = (List<Fruit>)Session["listSession"];
37 myBasket.Add(f);
38 foreach (var item in myBasket) {
39 ListBox1.Items.Add(item.getFruitInfo());
40 }
41 }
42 }
Implement a web application using the code above. The folowing lines should be
commented with detailed explanations of what they do
Line 1 - 4
Lines 5-10
Line 14
Line 17
Line 26
Line 28 - 29
Line 36
Lines 38-40
When you run the application, it should look similar to this:
Document Page
Part C - People application
You are going to modify your application from lab 5, Part B (the
one that allows the user to enter the details of people on one page)
Implement the following changes:
Your webpage application should allow the user to enter the details for any
number of people on one page, one by one (so you enter the details of the person
and click submit, than another person and click submit and so on)
Another page (you can use the About page) should display the details of all people
you submitted.
Hint 1: To achieve this use a generic list or an array list to hold the instances
of the person objects that you are creating by clicking the button and use a
session to store the list. Don’t forget about boxing and unboxing.
Hint 2: You can use a listbox on the About page and you can add elements
through the Items property
For example: ListBox1.Items.Add(inputString )
Part D - For the brave!
Improve the Basket of Fruit application
1. Create a new class called “Beverage”. Use this class to calculate the cost of a
beverage made of fruit – for example wine, cider, juice.
2. On the “About” page the user should be able to choose from dropDownList a
particular fruit created through the Default page. Also, the user should be
able to input how many kilos of fruit are to be processed into a beverage, the
cost of the fruit per kilo and other extra costs. Use the class “Beverage” to
calculate the beverage cost. If the fruit is not edible the application should not
allow the beverage to be created and a suitable message should be displayed.
You can go extra wild and take it as far as you want – perhaps create smoothies, etc
chevron_up_icon
1 out of 4
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]