重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用eclipse的可视化插件或者netbeans可视化做吧,简单的很快。
沅江网站建设公司创新互联公司,沅江网站设计制作,有大型网站制作公司丰富经验。已为沅江上千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的沅江做网站的公司定做!
仅供参考~
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;igs.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("总价格为:"+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;
}
@Override
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
@Override
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;
}
@Override
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
}
public class ShopCar {
private Goods goods[]=null;
private int foot;
//数组的大小由程序外部决定
public ShopCar(int len) {
if(len0){
goods=new Goods[len];
}else{
goods=new Goods[1];
}
}
//判断数组的内容是否已满,未满,则添加
public boolean add(Goods g){
if(this.footthis.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;ithis.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;ithis.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;
}
}
小兄弟、你的程序也不算大程序、但是要做,至少也得3个小时、都在上班、哪有那么多时间给你做啊。知识是学着自己用的、你要是把这个项目做出来了、你就会进步、会明白很多、其实java真的不难,何不自己试试呢?一步一步的做,加油,别低估了JAVA。