/// <summary>///Goods is a commodity class/// </summary> Public classgoods{ PublicGoods () {// //TODO: Add constructor logic here// } PublicGoods (stringNamedecimalpricedecimal) { This. Pricedecimal =Pricedecimal; This. Name =name; } PublicGoods (stringNamedecimalPricedecimal,stringHtmlurl) { This. Pricedecimal =Pricedecimal; This. Name =name; Htmlurl=Htmlurl; } Public decimalPricedecimal {Get;Set; } Public stringName {Get;Set; } Public stringHtmlurl {Get;Set; }}
First write a product class, there is a product name, price, page link address (this example is useless).
usingSystem.Collections.Generic;usingSystem.Text;/// <summary>///Order/// </summary> Public classIndent:list<goods>{ PublicIndent () {// //TODO: Add constructor logic here// } /// <summary> ///Number of orders/// </summary> Public intNumber {Get{returnCount;} } /// <summary> ///Order Amount/// </summary> Public decimalGetmoneydecimal () {decimalMoney =0; foreach(Goods Goodsinch This) { money+=goods. Pricedecimal; } returnMoney ; } /// <summary> ///Print Order/// </summary> /// <returns></returns> Public Override stringToString () {StringBuilder sb=NewStringBuilder (); Sb. Append ("Order Details"); foreach(Goods Goodsinch This) {sb. Append (goods. Name). Append ("Unit Price:"). Append (goods. Pricedecimal). Append (", "); } sb. Append ("Total:"). Append ( This. Getmoneydecimal ()); returnsb. ToString (); }}
Then there is an order class, the order is made up of goods, with list<goods>.
protected voidButton1_Click (Objectsender, EventArgs e) {Indent myindent=NewIndent (); Goods goods1=NewGoods ("Sickle",2.8m); Goods Goods2=NewGoods ("Axe",5.2m); Myindent.add (GOODS2); Myindent.add (GOODS1); stringStr=myindent.tostring (); Response.Write ("<script>alert ('"+ str +"') </script>"); }
The last button test, just fine, haha ^_^
Note: Do this toss me for more than half an hour, is because the last alert, the middle of the string is not a newline character, I used to stringbuilder.appendline (); This method causes the string to have a newline, has been unable to alert out.
Self-study "2. Web pop-up calculation of commodity prices