前期调查
使用子类与继承体现不同商品的特性:网上商城不同类别的商品有一些共同信息,比如商品名、价格。但有的信息并不相同。比如,图中的衣服品牌和食品重量
使用多态、DAO模式为商城增加不同的存储方法:商城可能使用文件存储商品数据、也可能使用列表存储数据。尝试使用DAO模式实现此功能,使得涉及到存储相关的代码可以轻松在两种存储模式之间进行切换。
系统功能结构图
系统描述:显示菜单,添加,删除或修改商品到商场中,判断是否符合条件,符合进行修改,不符合则返回菜单。显示菜单,添加,删除或修改商品到购物车中,判断是否符合条件,符合进行修改,不符合则返回菜单。
UML类图:
其中Goods有三个子类分别为
1.Clothing:特有属性为brand
2.Food:特有属性为weight
3.Electronic:特有属性为brand,size,memory
项目包结构与关键代码:
购物车商品类
package shopping2;
import java.math.BigDecimal;
import java.util.ArrayList;
public class Product {
private int productId; // 商品编号
private String productName;
private BigDecimal productPrice; // 商品单价
private int numb;
public Product() {
super();
}
public Product(int productId, String productName, BigDecimal productPrice,int numb) {
super();
this.productName = productName;
this.productId = productId;
this.productPrice = productPrice;
this.numb = numb;
}
@Override
public String toString() {
return "商品编号:" + productId + " 商品名称:" + productName + " 单价:" + productPrice + "";
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductName() {
return productName;
}
public void setProductId(int productId) {
this.productId = productId;
}
public int getProductId() {
return productId;
}
public int getProductNumb() {
return numb;
}
public void setProductNumb(int numb) { this.numb = numb; }
public void setPrice(BigDecimal price) {
this.productPrice = price;
}
public BigDecimal getPrice() {
return productPrice;
}
}
商场商品类
package shopping2;
import java.math.BigDecimal;
import java.util.*;
public class Goods {
private int goodsId; // 商品编号
private String goodsName;
private BigDecimal goodsPrice; // 商品单价
public Goods(int goodsId, String goodsName, BigDecimal goodsPrice) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
}
public int getGoodsId() {
return goodsId;
}
public void setGoodsId(int goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public BigDecimal getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(BigDecimal goodsPrice) {
this.goodsPrice = goodsPrice;
}
@Override
public String toString() {
return "Goods{" +
"goodsId=" + goodsId +
", goodsName='" + goodsName + '\'' +
", goodsPrice=" + goodsPrice +
'}';
}
}
购物车类
package shopping2;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Scanner;
public class Cart {
private BigDecimal sum=new BigDecimal(0);
public BigDecimal getSum() {
return sum;
}
public void setSum(BigDecimal sum) {
this.sum = this.sum.add(sum);
}
public static void showCart(ArrayList<Product> ListCart, Cart sum) {
if (ListCart.size() == 0) {
System.out.println("购物车是空的,请先添加再查询");
for (int i = 0; i < 3; i++) {
System.out.println(" ");
}
} else {
System.out.println("购物清单-----------------------------");
System.out.println("商品编号\t\t商品名称\t\t单价\t\t数量");
for (Product p : ListCart) {
System.out.println("\t\t" + p.getProductId() + "\t\t" + p.getProductName() + "\t\t" + p.getPrice() + "元" + "\t\t" + p.getProductNumb() + "件");
}
System.out.println("本次消费合计:" + sum.getSum() + "元");
}
}
public static void selectCart(Scanner sc, ArrayList<Goods> ListMall, ArrayList<Product> ListCart,Cart sum) {
System.out.println("请输入您要添加的商品名称:");
String name = sc.next();
int x = 0;
for (Goods g : ListMall) {
if (g.getGoodsName().equals(name)) {
System.out.println("您要添加的商品单价为:" + g.getGoodsPrice() + "元");
System.out.println("请输入您要添加的件数:");
int numb = sc.nextInt();
BigDecimal count = new BigDecimal(numb + "");
BigDecimal price = new BigDecimal(g.getGoodsPrice() + "");
sum.setSum(count.multiply(price));
Product p= new Product(g.getGoodsId(),g.getGoodsName(),g.getGoodsPrice(),numb);
// System.out.println("合计:"+Add+"元");
ListCart.add(p);
x++;
System.out.println("添加成功");
}
}
if (x == 0)
{
System.out.println("商店无该商品,请重新输入");
}
}
public static void delCart(Scanner sc, ArrayList<Product> ListCart,Cart sum) {
System.out.println("请输入你要删除的商品名:");
int x=0;
String name = sc.next();
for (Product p : ListCart) {
if (p.getProductName().equals(name)) {
BigDecimal a= new BigDecimal(-1*p.getProductNumb());
sum.setSum((p.getPrice()).multiply(a));
ListCart.remove(p);
System.out.println("删除成功!");
x++;
break;
}
}
if(x == 0){
System.out.println("没有找到该商品");
}
}
public static void modifyCart(Scanner sc, ArrayList<Product> ListCart,Cart sum){
System.out.println("请输入你要修改的商品名:");
int x=0;
String name = sc.next();
for (Product p : ListCart) {
if (p.getProductName().equals(name)) {
BigDecimal a= new BigDecimal(-1*p.getProductNumb());
sum.setSum((p.getPrice()).multiply(a));
System.out.println("请输入修改后的数量");
p.setProductNumb(sc.nextInt());
BigDecimal b= new BigDecimal(p.getProductNumb());
sum.setSum((p.getPrice()).multiply(b));
System.out.println("修改成功");
x++;
break;
}
}
if(x == 0){
System.out.println("没有找到该商品");
}
}
}
商场类
package shopping2;
import java.math.BigDecimal;
import java.util.*;
public class Mall {
public static void readMall(Scanner sc, ArrayList<Goods> ListMall){
System.out.println("请输入添加商品的信息 (格式:商品编码 商品名 商品价格)");
System.out.println("输入 end 结束");
while (true) {
String str = sc.next();
if ("end".equals(str)) {
break;
}
Goods pro = new Goods(Integer.valueOf(str), sc.next(), sc.nextBigDecimal());
ListMall.add(pro);
}
};
public static void delMall(Scanner sc, ArrayList<Goods> ListMall) {
System.out.println("请输入你要删除的商品名:");
String name = sc.next();
for (Goods g : ListMall) {
if (g.getGoodsName().equals(name)) {
ListMall.remove(g);
System.out.println("删除成功!");
break;
}
}
}
public static void showMall(ArrayList<Goods> ListMall) {
if (ListMall.size() == 0) {
System.out.println("商场是空的,请先添加再查询");
for (int i = 0; i < 3; i++) {
System.out.println(" ");
}
} else {
System.out.println("购物清单-----------------------------");
System.out.println("商品编号\t\t商品名称\t\t单价");
for (Goods g : ListMall) {
System.out.println("\t\t" + g.getGoodsId() + "\t\t" + g.getGoodsName() + " \t\t" + g.getGoodsPrice() + "元" );
}
}
}
public static void modifyMall(Scanner sc, ArrayList<Goods> ListMall){
System.out.println("请输入你要修改的商品名:");
int x=0;
String name = sc.next();
for (Goods g : ListMall) {
if (g.getGoodsName().equals(name)) {
System.out.println("请输入修改后商品的编号,名称,单价");
g.setGoodsId(Integer.valueOf(sc.next()));
g.setGoodsName(sc.next());
g.setGoodsPrice(sc.nextBigDecimal());
System.out.println("修改成功");
x++;
break;
}
}
if(x == 0){
System.out.println("没有找到该商品");
}
}
}
食物类
public class Food extends Goods{
private String weight; // 食品重量
public Food(String category,String goodsId, String goodsName, BigDecimal goodsPrice, String weight) {
super(category,goodsId, goodsName, goodsPrice);
this.weight = weight;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
@Override
public String toString() {
return super.toString() + " 重量:" + weight;
}
}
电子类
public class Electronic extends Goods{
private String brand;
private String size;
private String memory;
public Electronic(String category,String goodsId, String goodsName, BigDecimal goodsPrice, String brand, String size, String memory) {
super(category,goodsId, goodsName, goodsPrice);
this.brand = brand;
this.size = size;
this.memory = memory;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getMemory() {
return memory;
}
public void setMemory(String memory) {
this.memory = memory;
}
@Override
public String toString() {
return super.toString() + " 品牌名:" + brand + " 大小:" + size + " 内存:" + memory;
}
}
衣服类
public class Clothing extends Goods {
private String brand; // 服装品牌
public Clothing(String categoryint,String goodsId, String goodsName, BigDecimal goodsPrice, String brand, String category) {
super(categoryint,goodsId, goodsName, goodsPrice);
this.brand = brand;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public String toString() {
return super.toString() + " 品牌名:" + brand;
}
}
DAO接口
public interface MallDao {
public int readMall(String goodscategory);
public void showMall();
public Goods getMall(String goodscategory);
}
读入TXT类
public class MallTXT implements MallDao {
File file = new File(Thread.currentThread().getContextClassLoader().getResource("test.txt").getFile());
@Override
public int readMall(String serialnumber) {
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String temp;
String[] strs;
while ((temp = in.readLine()) != null) {
serialnumber = serialnumber;
strs = temp.split(" ");
if (strs[0].equals(serialnumber))
return 1;
}
} catch (IOException e) {
}
return -1;
}
@Override
public void showMall() {
System.out.println("-------------------商城-------------------");
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String temp;
while ((temp = in.readLine()) != null) {
System.out.println(temp);
}
} catch (IOException e) {
}
System.out.println("------------------------------------------");
}
@Override
public Goods getMall(String goodscategory) {
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String temp;
String[] str;
while ((temp = in.readLine()) != null) {
str = temp.split(" ");
if (str[0].equals(goodscategory)) {
switch (goodscategory) {
case "Food": {
Goods goods = new Food(str[0], str[1], str[2],new BigDecimal(String.valueOf(str[3]))),str[4];
return goods;
}
case "Electronic": {
Goods goods = new Electronic(str[0], str[1], str[2], new BigDecimal(String.valueOf(str[3]))), str[4], str[5]);
return goods;
}
case "Clothing": {
Goods goods = new Clothing(str[0], str[1], str[2], str[3]);
return goods;
}
}
}
}
} catch (IOException e) {
}
//Goods Goods = new Toiletries("T005", "云南白药牙膏薄荷味", "30.90", "云南白药");
return null;
}
}