Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Your Own Class. We will build a simple instance class for an inventory control application and a main program to test it. We need an Item class.

Similar presentations


Presentation on theme: "Building Your Own Class. We will build a simple instance class for an inventory control application and a main program to test it. We need an Item class."— Presentation transcript:

1 Building Your Own Class

2 We will build a simple instance class for an inventory control application and a main program to test it. We need an Item class that will hold information about each of a number of items sold by the XYZ Corporation. The Item class will hold: itemName itemNumberInStock itemRetailPrice The accessors for these three private variables will be called: GetName GetQuantity GetPrice Specifications for inventoryApp

3 Creating a new Console Application inventoryApp

4 Adding an Instance Class

5 using System; namespace inventoryApp { class Program { static void Main(string[] args) { string name; int quantity; double price; Console.Write("Enter item name...................................... "); name = Console.ReadLine(); Console.Write("Enter initial number of items........................ "); quantity = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter retail price................................... "); price = Convert.ToDouble(Console.ReadLine()); Item myItem = new Item(name, quantity, price); Console.WriteLine("Item Name = " + myItem.GetName() + " Price = " + myItem.GetPrice()); Console.ReadKey(); } A Test Program for inventoryApp

6 namespace inventoryApp { class Item { private string itemName; private int itemInStock; private double itemRetailPrice; public Item() { } public Item(string iName, int iInStock, double iRetailPrice) { itemName = iName; itemInStock = iInStock; itemRetailPrice = iRetailPrice; } inventoryApp Class without Accessors

7 public string GetName() { return itemName; } public int GetQuantity() { return itemInStock; } public double GetPrice() { return itemRetailPrice; } Accessors for the inventoryApp Class

8 Test Run for inventoryApp (Build 0)

9 Extending the inventoryApp Creating and naming items individually is not efficient. We need to extend the functions of our inventoryApp to allow us to generate a list of items. For this we will introduce a generic list List. Your first task is to add a generic list of type "Item" to the main test program and load the list with mulitple items... The result of your multiple item test should look similar to the following:

10 Review the ShapesDemoFixed program posted on the Course Web Page for examples of how to use Generic Lists. using System; using System.Collections.Generic; namespace ShapesDemo { public partial class FormMain : Form { List shapes = new List (); timerAnim.Start(); } public void genShapes() { for (int i = 0; i < 10; i++) { shapes.Add(new Square((double)(rnd.Next(100) + 25), (double)(rnd.Next(w) - 100), (double)(rnd.Next(h) - 100), (double)rnd.Next(-1000, 1000) / 100.0, (double)rnd.Next(-100, 100) / 500.0, } private void updateShapes() { foreach (Shape s in shapes) { s.move(); s.Bounce(picBox.Width,picBox.Height); } generic list for type Shape new Squares are being Added to the list shapes the foreach( ) construct is used to look at every shape s in the list shapes.

11 using System; using System.Collections.Generic; namespace inventoryApp { class Program { static void Main(string[] args) { List items = new List (); items.Add(new Item("Widget", 10, 19.95)); items.Add(new Item("Geegaw", 100, 9.99)); items.Add(new Item("Doodad", 1000, 0.99)); foreach (Item itm in items) { Console.WriteLine(itm.GetName() + " " + itm.GetQuantity() + " " + itm.GetPrice()); } Console.ReadKey(); } A Simplified Version of the Main Test Program for inventoryApp Tired of retyping the data every time you run the program? Just hardwire the creation of a list of products into the main test program. This is OK here because the main program is only being used to debug the Item class. Later we will learn how to read and write text files using C#...(see example on next slide).

12 static void Main(string[] args) { string name; int quantity; double price; List items = new List (); string textline = ""; StreamReader tr = new StreamReader("products.txt"); while (true) { textline = tr.ReadLine(); if (textline == "eof") break; string[] str = textline.Split(' '); quantity = int.Parse(str[0]); price = double.Parse(str[1]); name = str[2]; items.Add(new Item(name, quantity, price)); } tr.Close(); foreach (Item itm in items) { Console.WriteLine(itm.GetName() + " " + itm.GetQuantity() + " " + itm.GetPrice()); } Console.ReadKey(); } Reading from a Textfile 10 19.95 Widget 100 9.99 Geegaw 1000 0.99 Doodad eof products.txt This text file is placed in the Debug directory of the project along with the executable (.exe) program. Note: This is for future reference only. You are not being asked to create a program that reads a textfile at this time.

13 Next Iteration We are continuing to build our Item.cs class. In the next iteration, add methods to perform the following operations on an item: Submit the updated version of your Item class by start of class. A boolean function that returns true only if a requested quantity of a particular time is Available A Purchase method that subtracts the number of times purchased from the number in stock and returns the total cost of purchasing the specified number of items A Restock method that adds the number of items that has been added to the inventory. A PriceChange method that enbles a change in price of an item after it has been created.

14 while (true) { name = ""; Console.Write("Enter item name...................................... "); name = Console.ReadLine(); if (name == "") break; Console.Write("Enter initial number of items........................ "); quantity = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter retail price................................... "); price = Convert.ToDouble(Console.ReadLine()); items.Add(new Item(name, quantity, price)); } Adding New Items to the Inventory With the while(true) loop the user can add new items to the inventory list until an empty string is entered for an item name. If the string name is equal to the empty string then the break command causes the program to break out of the loop and jump to the first instruction beyond the loop. Notice: The while(true) loop is not a very elegant control structure. However, this one is simple and will do for now. if this conditional is true control jumps to here

15 StreamWriter tw = new StreamWriter("products.txt"); foreach(Item itm in items) { tw.WriteLine(itm.GetQuantity() + " " + itm.GetPrice() + " " + itm.GetName()); } tw.WriteLine("eof"); tw.Close(); Writing a Text File to the Hard Drive We have learned how to read a text file using StreamReader...Now we look at how to write a text file using StreamWriter. Both of these are part of the System.IO collection of tools. It is very important that you close the file. This will ensure that any data left in the output buffer is written to the hard drive and the file is properly terminated with an end-of-file marker.

16

17

18

19

20

21


Download ppt "Building Your Own Class. We will build a simple instance class for an inventory control application and a main program to test it. We need an Item class."

Similar presentations


Ads by Google