1.人员分工
类1、类2、类3、类4,罗晨珊
2.前期调查
先在商城(Mall)中,浏览所有商品或查找某一商品,然后将商品加入购物车(Cart)中,可以在购物车内进行商品数量的增加、减少、清空购物车及商品结算。
编辑
编辑
3.系统功能结构图。
编辑
4.系统描述:一段用来描述系统功能与流程的文字,用红色字代表可能的对象(名词)或属性,用蓝色字代表可能的方法(动词)。
用户可在商城(Mall)中,浏览所有商品或查找某一商品,然后可以在购物车内进行商品的增加、减少、清空购物车及商品结算。
5.类设计说明:类概述(有什么用)、类的属性与方法、类与类之间的关系。可选:类与类之间的关系可使用UML类图描述。建议使用ProcessOn回值。。
Mall类:初始化商城商品,展示商城商品,搜索商城商品
Commodity类:String name,double price,int num
Cart类:增加商品,删除商品,清空购物车,展示购物车,结算
6.本系统哪里体现了面向对象的封装性?可选:哪里体现了继承与多态?
封装性:
编辑继承:
编辑方便在购物车内加入、删除商品是对商城内商品余量进行增减
7.项目包结构(package的划分)与关键代码:项目的包结构(为什么要这样设计包结构),主要功能(如网购物车添加、删除商品)的流程图与关键代码。
class Commodity {
private String name;
private double price;
private int num;
public Commodity() {
}
public Commodity(String name, double price, int num) {
this.setName(name);
this.setPrice(price);
this.setNum(num);
}
public Commodity(String name, int num) {
this.setName(name);
this.setNum(num);
}
@Override
public String toString() {// 方法的功能越单一越好
return "Commodity [name=" + getName() + ", price=" + getPrice() + ", num=" + getNum() + "]";
}
}
功能:存储商品信息,名称、价格、数量
class Mall {
public ArrayList<Commodity> commodities = new ArrayList<>();
{// 初始化商场物品
commodities.add(new Commodity("Apple", 5.8, 50));
commodities.add(new Commodity("Dress", 210, 20));
commodities.add(new Commodity("Computer", 5299, 35));
commodities.add(new Commodity("Car", 16999, 10));
commodities.add(new Commodity("Shoe", 169, 100));
commodities.add(new Commodity("Earphone", 129, 80));
commodities.add(new Commodity("Schoolbag", 99, 200));
commodities.add(new Commodity("Cup", 29, 12));
commodities.add(new Commodity("Soap", 16.9, 37));
}
public void search(String name) {
int flag = 1;
for (int i = 0; i < commodities.size(); i++) {
if (commodities.get(i).getName().equals(name)) {
System.out.println("找到该商品");
System.out.println("name=" + commodities.get(i).getName() + " price=" + commodities.get(i).getPrice()
+ " num=" + commodities.get(i).getNum());
flag = 0;
}
}
if (flag == 1) {
System.out.println("未找到该商品");
}
}
public void disp() {
for (Commodity e : commodities) {
System.out.println("name=" + e.getName() + " price=" + e.getPrice() + " num=" + e.getNum());
}
}
}
功能:初始化商城内商品数据; 通过商品名称,搜索商城内商品;展示商城内所有商品
class Cart extends Mall {
private ArrayList<Commodity> shopList = new ArrayList<>();
public void addToCart(Commodity c) {
// 改进:唯一性判断
int flag1 = 1;
for (int j = 0; j < super.commodities.size(); j++) {
// 减去商城中物品的数量
if (super.commodities.get(j).getName().contentEquals(c.getName())) {
if (super.commodities.get(j).getNum() >= c.getNum()) {
int flag = 1;// 控制条件,唯一性判断,如果flag==1则添加新商品
for (int i = 0; i < shopList.size(); i++) {
if (shopList.get(i).getName().contentEquals(c.getName())) {
c.setNum(shopList.get(i).getNum() + c.getNum());
shopList.set(i, c);
flag = 0;
}
}
if (flag == 1) {
shopList.add(c);
}
super.commodities.get(j).setNum(super.commodities.get(j).getNum() - c.getNum());
} else {
System.out.print("该商品余量不足,当前余量为:");
System.out.println(super.commodities.get(j).getNum());
}
flag1 = 0;
}
}
if (flag1 == 1) {
System.out.println("对不起,该商场中暂无此商品");
}
}
public void delete(String name, int num) {
int flag = 1;
for (int i = 0; i < shopList.size(); i++) {
if (shopList.get(i).getName().contentEquals(name)) {
if (shopList.get(i).getNum() >= num) {
shopList.get(i).setNum(shopList.get(i).getNum() - num);
for (int j = 0; j < super.commodities.size(); j++) {
if (super.commodities.get(j).getName().contentEquals(name)) {
super.commodities.get(j).setNum(super.commodities.get(j).getNum() + num);
}
}
if (shopList.get(i).getNum() == 0) {
shopList.remove(i);
}
} else {
System.out.println("购物车内该商品数量不足");
}
flag = 0;
}
}
if (flag == 1) {
System.out.println("未在购物车内找到该商品");
}
}
public void disp() {
int flag = 1;
for (Commodity e : shopList) {
flag = 0;
System.out.println("name=" + e.getName() + " num=" + e.getNum());
}
if (flag == 1) {
System.out.println("当前购物车为空");
}
}
public void clear() {
shopList.clear();
}
public double calc() {
double x = 0;
for (Commodity e : shopList) {
for (int i = 0; i < super.commodities.size(); i++) {
if (super.commodities.get(i).getName().contentEquals(e.getName())) {
x += super.commodities.get(i).getPrice() * e.getNum();
}
}
}
return x;
}
}
功能:添加删除购物车内商品,并对商城内商品余量进行修改;展示购物车;清空购物车;结算
public class Main {
public static void main(String[] args) {
Cart cart = new Cart();
Mall mall = new Mall();
Scanner sc = new Scanner(System.in);
System.out.println("欢迎来到购物商城");
Menu();
int n = sc.nextInt();
while (n >= 1 && n <= 8) {
switch (n) {
case 1:
mall.disp();
break;
case 2:
System.out.println("输入待查找商品名称");
mall.search(sc.next());
break;
case 3:
System.out.println("您需要添加几件商品?");
int num = sc.nextInt();
System.out.println("接下来请输入待添加的商品名称及数量");
for (int i = 0; i < num; i++) {
Commodity goods = new Commodity(sc.next(), sc.nextInt());
cart.addToCart(goods);
}
break;
case 4:
System.out.println("请输入待删除的商品名称及数量");
cart.delete(sc.next(), sc.nextInt());
break;
case 5:
cart.disp();
break;
case 6:
cart.clear();
System.out.println("购物车已清空");
break;
case 7:
double x = cart.calc();
System.out.println("总价=" + x);
break;
case 8:
System.exit(0);
break;
default:
System.out.println("暂无此功能,请重新检查您的输入");
break;
}
Menu();
n = sc.nextInt();
}
}
private static void Menu() {
System.out.println("输入1查看商城商品");
System.out.println("输入2查找商品");
System.out.println("输入3添加购物车");
System.out.println("输入4删除购物车内某件商品");
System.out.println("输入5展示购物车内所有商品");
System.out.println("输入6清空购物车");
System.out.println("输入7结算");
System.out.println("输入8退出商城");
}
}
功能:展示该商城所具有的功能并调用
标签:Commodity,get,int,程序,购物车,commodities,面向对象,getNum From: https://www.cnblogs.com/luocs/p/16738488.html