博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小明逛超市
阅读量:5286 次
发布时间:2019-06-14

本文共 3801 字,大约阅读时间需要 12 分钟。

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("未输入要查询商品或输入错误:"+"格式为:\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());
}
}
}

转载于:https://www.cnblogs.com/hell/p/5421435.html

你可能感兴趣的文章
大道至简读后感(第四章)
查看>>
IDA IDC Tutorials: Additional Auto-Commenting
查看>>
k8s-存储卷1-十二
查看>>
在Android中Intent的概念及应用(二)——Intent过滤器相关选项
查看>>
第十六章 多态性(一)
查看>>
INSERT IGNORE INTO / REPLACE INTO
查看>>
Python数据类型-布尔/数字/字符串/列表/元组/字典/集合
查看>>
【刷题】SPOJ 705 SUBST1 - New Distinct Substrings
查看>>
IEEE 754浮点数表示标准
查看>>
declare 结构用来设定一段代码的执行指令
查看>>
图解算法读书笔记
查看>>
调试学习笔记
查看>>
解开lambda最强作用的神秘面纱
查看>>
Java基础:Object类中的equals与hashCode方法
查看>>
C#拦截Http请求
查看>>
图片下载器
查看>>
找不到docker.socket解决方法
查看>>
Activity生命周期
查看>>
HTML中head头结构
查看>>
sql server和mysql中分别实现分页功能
查看>>