Program code:
Public InterfaceGoods { PublicString getName (); Public DoubleGetPrice ();} Public classEatfoodImplementsgoods{PrivateString name;Private DoublePrice ; PublicEatfood () {} PublicEatfood (String name,DoublePrice ) {Super(); This. Name =name; This. Price =Price ;} Public DoubleGetPrice () {return This. Price; Public voidSetprice (DoublePrice ) { This. Price =Price ;} Public voidsetName (String name) { This. Name =name;} PublicString GetName () {return This. Name;}} Public classWashgoodsImplementsgoods{PrivateString name;Private DoublePrice ; Publicwashgoods () {} PublicWashgoods (String name,DoublePrice ) {Super(); This. Name =name; This. Price =Price ;} Public DoubleGetPrice () {return This. Price; Public voidSetprice (DoublePrice ) { This. Price =Price ;} Public voidsetName (String name) { This. Name =name;} PublicString GetName () {return This. Name;}} Public classShopcar {PrivateGoods goods[]=NULL;Private intfoot;//the size of the array is determined outside the program PublicShopcar (intLen) {if(len>0) {Goods=NewGoods[len];}Else{Goods=NewGoods[1];}}//To determine whether the contents of the array are full or not full, add Public BooleanAdd (Goods g) {if( This.foot< This. Goods.length) { This. goods[foot]=G;foot++;return true;}Else{return false;}} //keyword search Publicgoods[] Search (String keyword) {Goods go[]=NULL; intCount=0; for(inti=0;i< This. goods.length;i++){ if(goods[i]!=NULL){ if( This. Goods[i].getname (). INDEXOF (keyword)!=-1) {Count++; }}}go=NewGoods[count]; intF=0; for(inti=0;i< This. goods.length;i++){ if(goods[i]!=NULL){if( This. Goods[i].getname (). INDEXOF (keyword)!=-1) {Go[f]= This. Goods[i];f++;}}}returngo;}//get all the information Publicgoods[] Getgoods () {return This. Goods; }} Public classJavasix {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubShopcar s1=NewShopcar (5); S1.add (NewEatfood ("Bread", 12.1)); S1.add (NewEatfood ("Spicy strip", 2.4)); S1.add (NewEatfood ("Biscuit", 22.3)); S1.add (NewWashgoods ("Shampoo", 32.5)); S1.add (NewWashgoods ("toilet paper", 22.8)); Print (S1.search (Cookies)); System.out.println ("============="); Print (S1.getgoods ()); } Public Static voidprint (Goods gs[]) {DoubleSum=0; for(inti=0;i<gs.length;i++){ if(gs[i]!=NULL){ //System.out.println (p[i]+ ",");System.out.println (Gs[i].getname () + "," +Gs[i].getprice ()); Sum=sum+Gs[i].getprice (); }} System.out.println ("Total Price is:" +sum); } }
Java Seventh time job