多态练习题---设计商品类(Product,有条码、价格)及其子类(水果、纺织用品、玩具),水果下又有子类(苹果、香蕉、榴莲等),纺织用品有子类(毛巾、被子),玩具有子类(玩具车、玩具飞机)

结合超市售卖商品的情景,思考商品类以及各个子类应该有的属性和方法,以及构造器

做一个购物车,购物车(最多放10件商品)添加方法、移除、结算


Product父类

package com.Shop.java;

public class Product {

	private String barcode;
	private double price;
	private String name;
	private int count;
	
	public Product() {
		super();
	}
	public Product(String barcode, double price, String name, int count) {
		super();
		this.barcode = barcode;
		this.price = price;
		this.name = name;
		this.count = count;
	}
	public String getBarcode() {
		return barcode;
	}
	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
}

Fruit水果类:

public class Fruit extends Product {


	private int days;   //保质期还剩多少天


	public int getDays() {
		return days;
	}


	public void setDays(int days) {
		this.days = days;
	}
}

Apple苹果类:

public class Apple extends Fruit {


	public Apple(String name, double price, int days) {
		setName(name);
		setPrice(price);
		setDays(days);
	}
}

Banana香蕉类:

public class Banana extends Fruit {

	public Banana(String name, double price, int days) {
		setName(name);
		setPrice(price);
		setDays(days);
	}
}

Durian榴莲类:

public class Durian extends Fruit {


	public Durian(String name, double price, int days) {
		setName(name);
		setPrice(price);
		setDays(days);
	}
}

纺织用品类和玩具类及其子类省略,写法与水果类相似


做一个购物车,购物车(最多放10件商品)添加方法、移除、结算

public class ShopCart {
	Product[] products = new Product[10];
	private int count = 0;      // 商品数量
    private double price=0.0;  // 购物车总价格
	
	//往购物车添加商品
	public void add(Product p) {
		if(count == products.length){
	           System.out.println("购物车类最多方十件商品");
	        }
			products[count] = p;
	        price += products[count].getPrice();
	        count++;
	        return;
	}
	
	//计算购物车中所有商品的总金额
	public double getSum() {
		return price;
		
	}
	//移除购物车中的商品
	 public Product remove(String No) {
	        int i;
	        Product p=null;
	        for (i = 0; i < products.length; i++) {
	            if(products[i].getBarcode().equals(No)){
	            	p = products[i];
	                price -= products[i].getPrice();
	                for (int j = i; j < products.length-1; j++) {
	                	products[j] = products[j+1];	                	
	                }
	                count--;
	                System.out.println("***购物车剩余商品***");
	                for (int k = 0; k < count; k++) {  
	                    System.out.println( products[k].getBarcode()+"\t" 
	                    					+products[k].getPrice() +"\t" 
	                    					+ products[k].getName()+"\t" 
	                    					+products[k].getCount() );
	                }
	                System.out.println("剩余商品的金额为:" + price);
	                break;
	            }
	        }
	        if(i == products.length){
	            System.out.println("无此商品,无法删除。");
	        }
	        return p;
	    }
	
	
	/**
     * 输出购物车信息
     */
    public void print() { 
    	System.out.println("***购物车清单***");
        for (int i = 0; i < count; i++) {  
        System.out.println(products[i].getBarcode()+"\t" +products[i].getPrice() +"\t" + products[i].getName()+"\t" +products[i].getCount());
        }
    }
}

测试类Test:

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ShopCart shop = new ShopCart();
		//barcode, price, name,count
		shop.add(new Product("111",25,"苹果",1));
		shop.add(new Product("222",11,"毛巾",1));
		shop.add(new Product("333",36,"榴莲",1));
		shop.add(new Product("444",45,"遥控车",1));
		shop.add(new Product("555",101,"玩具飞机",1));
		System.out.println("商品总金额为:"+shop.getSum());
		shop.print();
		shop.remove("111");
	}

}


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值