码迷,mamicode.com
首页 > 编程语言 > 详细

java7

时间:2016-04-28 19:45:25      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

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("爵迹",69.9f,1,"郭敬明","xx"));
sc.add(new Books("龙族",79.9f,5,"江南","xxx"));
sc.add(new Books("斗罗大陆",69.9f,3,"唐家三少","xxxx"));
sc.add(new Cloths("黑曼巴",69.9f,2,"xxxx","xxxx"));
sc.add(new Cloths("纪梵希",69.9f,2,"xxxxx","xxxxx"));
sc.add(new Cloths("vans",69.9f,1,"xxxx","xxxx"));
System.out.println("=========已买到的商品清单=========");
print1(sc.getContent());
System.out.println("=========查询商品详细信息=========");
try
{
print(sc.search(args[0]));
}
catch(Exception e){
System.out.println("未输入要查询商品或输入错误:"+"格式为:\njava 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());
}
}
}

技术分享

java7

标签:

原文地址:http://www.cnblogs.com/zp742601689/p/5443741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!