Design of the class
The first kind: the class of commodity, the parent class is an abstract class;
The second kind: The Class of discount, the parent class is also an abstract class
The class diagram is as follows;
Use skills
- Using the inherited abstract class to realize polymorphism, using polymorphism to implement Factory mode;
- Using the reflection mechanism to implement the polymorphic and factory pattern of the construction instance;
- Factory mode can increase the scalability of the system;
- Use the list collection in dictionary to reduce the amount of code;
Source
Warehouse Class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;//The advantages of this program, when the increase in the type of goods, do not need to modify the local code, only need to call addshelf to add a shelf, add the goods in accordance with the name of the goods added, and in the addition of goods in the use of the reflection mechanism;Namespace _33_ Supermarket cashier system {class Cangku {//storage of goods in the warehouse, need to meet: sorting requirements, so need shelves; Publicdictionary<string, list<productfather>> List =Newdictionary<string, list<productfather>> ();//Can operate the object shelves, goods, operation mode: additions and deletions; // <summary> /// Add a shelf for the warehouse; // </summary> // <param name= "name" > shelf name to add </param> // <returns> If the shelf already exists return add failed, otherwise add shelf success </returns> Public BOOL addshelf(stringName) {if(list. ContainsKey (name))return false; list[name]=NewList<productfather> ();return true; }// <summary> /// Delete a shelf; // </summary> // <param name= "name" > shelf name to remove </param> // <param name= "Reason" > success is a successful deletion, the failure is the reason for the return failure, including the failure of the shelves and goods, and the failure of the shelf that does not exist </ Param> /// <returns> Indicates whether the deletion was successful </returns> Public BOOL delshelf(stringName out stringReason) {if(list. ContainsKey (name)) {if(List[name]. count==0) {list. Remove (name); Reason ="successfully deleted";return true; }Else{reason ="shelves and goods.";return false; } }Else{reason ="There is no such shelf";return false; } }// <summary> /// Add a count of protype types of goods to the shelves; // </summary> /// <param name= "Protype" > cargo Type, through the reflection of this string find the constructor of the object, implement Factory mode </param> // <param name= "Count" > quantity of goods to be added </param> // <param name= "price" > goods prices </param> /// <returns></returns> Public BOOL addproducts(stringProtype,intCountDoublePrice) {if(!list. ContainsKey (Protype)) {return false; }stringstr ="_33_ Supermarket cashier system."+protype; for(inti =0; I < count; i++) {//The reflection mechanism is used here;List[protype]. ADD ((Productfather) activator.createinstance (System.Type.GetType (str), Price, Protype, Guid.NewGuid (). ToString ())); }return true; }// <summary> /// Remove some goods from the warehouse; // </summary> /// <param name= "Protype" > type of goods to be removed </param> /// <param name= "Reqcount" > quantity to be removed </param> /// <param name= "GetCount" > actual amount removed </param> /// <returns> Returns the array of items removed </returns> PublicProductfather[]delproducts(stringProtype,intReqcount, out intGetCount) {productfather[] pros =NewProductfather[reqcount];//Run time to determine the size of the array; if(!list. ContainsKey (Protype))//There is no shelf for this cargo;{GetCount =0;return NULL; } for(inti =0; i < Reqcount; i++) {if(List[protype]. count==0)//If the goods have been exhausted;{GetCount = i;returnPros } Pros[i] = list[protype][0]; List[protype]. RemoveAt (0); } GetCount = Reqcount;returnPros }// <summary> /// Get information about the goods in the warehouse // </summary> /// <returns> Returns an array of formatted strings, a string of items that can be parsed out of the same protocol on the outside </returns> Public string[]GetInfo() {//Use formatted string to store shelf information; string[] Info =New string[List. Count];intI=0;foreach(varIteminchList. Keys) {Info[i] = Item. ToString (); Info[i] + ="|"+ List[item]. Count; Info[i] + ="|"+ list[item][0]. Price; }returnInfo } }}
Product Category
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.threading.tasks;namespace _33_ Supermarket cashier System {class Productfather { Public DoublePrice {Get;Set; } Public stringName {Get;Set; } Public stringID {Get;Set; } Public Productfather(DoublePricestringNamestringID) { This. Price = Price; This. name = name; This. id = ID; } }}
C # Practice Supermarket cashier System