结合超市售卖商品的情景,思考商品类以及各个子类应该有的属性和方法,以及构造器
做一个购物车,购物车(最多放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");
}
}
运行结果: