4.22
2016-04-22 13:52
453 查看
public class ShopGoodsDemo {
public static void main(String[] args) {
ShopCar s1=new ShopCar(5);
s1.add(new EatFood("面包",12.1));
s1.add(new EatFood("辣条",2.4));
s1.add(new EatFood("饼干",22.3));
s1.add(new WashGoods("洗发水",32.5));
s1.add(new WashGoods("卫生纸",22.8));
print(s1.search("饼干"));
System.out.println("=============");
print(s1.getGoods());
}
public static void print(Goods gs[]){
double sum=0;
for(int i=0;i<gs.length;i++){
if(gs[i]!=null){
System.out.println(gs[i].getName()+","+gs[i].getPrice());
sum=sum+gs[i].getPrice();
}
}
System.out.println("总价格为:"+sum);
}
}
public interface Goods {
public String getName();
public double getPrice();
}
public class EatFood implements Goods{
private String name;
private double price;
public EatFood() {
}
public EatFood(String name, double price) {
super();
this.name = name;
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class WashGoods implements Goods{
private String name;
private double price;
public WashGoods() {
}
public WashGoods(String name, double price) {
super();
this.name = name;
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class ShopCar {
private Goods goods[]=null;
private int foot;
public ShopCar(int len) {
if(len>0){
goods=new Goods[len];
}else{
goods=new Goods[1];
}
}
public boolean add(Goods g){
if(this.foot<this.goods.length){
this.goods[foot]=g;
foot++;
return true;
}else{
return false;
}
}
public Goods[] search(String keyword){
Goods go[]=null;
int count=0;
for(int i=0;i<this.goods.length;i++){
if(goods[i]!=null){
if(this.goods[i].getName().indexOf(keyword)!=-1){
count++;
}
}
}
go=new Goods[count];
int f=0;
for(int i=0;i<this.goods.length;i++){
if(goods[i]!=null){
if(this.goods[i].getName().indexOf(keyword)!=-1){
go[f]=this.goods[i];
f++;
}
}
}
return go;
}
public Goods[] getGoods(){
return this.goods;
}
}
public static void main(String[] args) {
ShopCar s1=new ShopCar(5);
s1.add(new EatFood("面包",12.1));
s1.add(new EatFood("辣条",2.4));
s1.add(new EatFood("饼干",22.3));
s1.add(new WashGoods("洗发水",32.5));
s1.add(new WashGoods("卫生纸",22.8));
print(s1.search("饼干"));
System.out.println("=============");
print(s1.getGoods());
}
public static void print(Goods gs[]){
double sum=0;
for(int i=0;i<gs.length;i++){
if(gs[i]!=null){
System.out.println(gs[i].getName()+","+gs[i].getPrice());
sum=sum+gs[i].getPrice();
}
}
System.out.println("总价格为:"+sum);
}
}
public interface Goods {
public String getName();
public double getPrice();
}
public class EatFood implements Goods{
private String name;
private double price;
public EatFood() {
}
public EatFood(String name, double price) {
super();
this.name = name;
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class WashGoods implements Goods{
private String name;
private double price;
public WashGoods() {
}
public WashGoods(String name, double price) {
super();
this.name = name;
this.price = price;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class ShopCar {
private Goods goods[]=null;
private int foot;
public ShopCar(int len) {
if(len>0){
goods=new Goods[len];
}else{
goods=new Goods[1];
}
}
public boolean add(Goods g){
if(this.foot<this.goods.length){
this.goods[foot]=g;
foot++;
return true;
}else{
return false;
}
}
public Goods[] search(String keyword){
Goods go[]=null;
int count=0;
for(int i=0;i<this.goods.length;i++){
if(goods[i]!=null){
if(this.goods[i].getName().indexOf(keyword)!=-1){
count++;
}
}
}
go=new Goods[count];
int f=0;
for(int i=0;i<this.goods.length;i++){
if(goods[i]!=null){
if(this.goods[i].getName().indexOf(keyword)!=-1){
go[f]=this.goods[i];
f++;
}
}
}
return go;
}
public Goods[] getGoods(){
return this.goods;
}
}
相关文章推荐
- IOS的 testflight测试设置
- jvm笔记
- 对象相等性和同一性
- centos7 安装nfs报错
- dubbo 体验
- ios app开发简单实例--源代码管理的基本操作
- 激励自己继续netty学习
- Linux虚拟地址空间布局
- MarkDown常用语法
- 面向对象编程的三个基本特征
- 浅析Yii2 gridview实现批量删除教程
- 在java中,形参的修改
- cookie 和session 的区别详解
- 文件系统vs对象存储——选型和趋势
- 一组绝对有益于科研的随想录 [转]
- 基于jquery.uploadify.js上传的测试环境
- HTTP0.9 vs. HTTP1.0 vs. HTTP1.1 vs. HTTPng
- js javascript jquery区别和联系
- Interpolation particles In Katana
- printf()函数