小明逛超市

abstract class Goods{
private String name;
private float price;
private int count;
public Goods(String name,float price,int count){
this.setName(name);
this.setPrice(price);
this.setCount(count);
}
public void setName(String name){
this.name=name;
}
public void setPrice(float price){
this.price=price;
}
public void setCount(int count){
this.count=count;
}
public String getName(){
return this.name;
}
public float getPrice(){
return this.price;
}
public int getCount(){
return this.count;
}
public abstract String getlnfo();
public String getlnfo1(){
return"品名:"+this.getName()+" 单价:"+this.getPrice()+" 数量:"+this.getCount();
}
}
class Books extends Goods{
private String author;
private String publish;
public Books(String name,float price,int count,String author,String publish){
super(name,price,count);
this.setAuthor(author);
this.setPublish(publish);
}
public void setAuthor(String author){
this.author=author;
}
public String getAuthor(){
return this.author;
}
public void setPublish(String publish){
this.publish=publish;
}
public String getPublish(){
return this.publish;
}
public String getlnfo(){
return "书名:"+this.getName()+" 单价:"+this.getPrice()+" 作者:"+this.getAuthor()+" 出版社:"+this.getPublish()
+" 数量:"+this.getCount()+" 总价:"+this.getPrice()*this.getCount();
}
public String getlnfo1(){
return "品名:"+this.getName()+" 单价:"+this.getPrice()+" 数量:"+this.getCount();
}
}
class Cloths extends Goods{
private String title;
private String style;
public Cloths(String name,float price,int count,String title,String style){
super(name,price,count);
this.setTitle(title);
this.setStyle(style);
}
public void setTitle(String title){
this.title=title;
}
public String getTitle(){
return this.title;
}
public void setStyle(String style){
this.style=style;
}
public String getStyle(){
return this.style;
}
public String getlnfo(){
return "品名:"+this.getName()+" 单价:"+this.getPrice()+
" 品牌:"+this.getTitle()+" 款式:"+this.getStyle()+
" 数量:"+this.getCount()+"总价:"+this.getPrice()*this.getCount();
}
public String getlnfo1(){
return "品名:"+this.getName()+" 单价:"+this.getPrice()+" 数量:"+this.getCount();
}
}
class ShopCar{
private Goods[] goods;
private int foot;
public ShopCar(int len){
if(len>0){
this.goods=new Goods[len];
}else{
this.goods=new Goods[1];
}
}
public boolean add(Goods goods){
if(this.foot<this.goods.length){
this.goods[this.foot]=goods;
this.foot++;
return true;
}else{
return false;
}
}
public Goods[] getContent(){
return this.goods;
}
public Goods[] search(String keyWord){
Goods g[]=null;
int count=0;
for(int i=0;i<this.goods.length;i++){
if(this.goods[i]!=null){
if(this.goods[i].getName().indexOf(keyWord)!=-1){
count++;
}
}
}
g=new Goods[count];
int f=0;
for(int i=0;i<this.goods.length;i++){
if(this.goods[i]!=null){
if(this.goods[i].getName().indexOf(keyWord)!=-1){
g[f]=this.goods[i];
f++;
}
}
}
return g;
}
}
public class mai {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ShopCar sc=new ShopCar(6);
sc.add(new Books("xxx",69.9f,1,"sas","zx"));
sc.add(new Books("asdf",79.9f,5,"fdgd","qwe"));
sc.add(new Books("dg",69.9f,3,"asd","dfg"));
sc.add(new Cloths("ghj",69.9f,2,"rter","zxcs"));
sc.add(new Cloths("zfg",69.9f,2,"asfd","dfg"));
sc.add(new Cloths("qw",69.9f,1,"fdg","sad"));
System.out.println("=========已买到的商品清单=========");
print1(sc.getContent());
System.out.println("=========查询商品详细信息=========");
try
{
print(sc.search(args[0]));
}
catch(Exception e){
System.out.println("未输入要查询商品或输入错误:"+"格式为: java Test 商品名(或商品名中的任一字符) ");
}
}
public static void print1(Goods goods[]){
for(int i=0;i<goods.length;i++){
System.out.println(goods[i].getlnfo1());
}
}
public static void print(Goods goods[]){
for(int i=0;i<goods.length;i++){
System.out.println(goods[i].getlnfo());
}
}
}

原文地址:https://www.cnblogs.com/hell/p/5421435.html