成员 | 任务 |
---|---|
付浩帆 | 面向对象设计,功能设计,编码规范,前期调查 |
吕威 | 博客制作 |
用户进入主菜单可以查看自己购物车内的商品,删除购物车内不想要的商品,添加自己想要的商品,且可以增加所需商品的数量,还可以搜索程序所原代的商品,还可以添加新商品。
关于一些实际app里的场景:
从实际应用场景中可以看出。购物车中,有商品的名称,可以添加删除商品,还可以修改要购买商品的数量,也可以看到商品的价格,最后能显示出商品的总价。
[https://blog.csdn.net/qq_51001259/article/details/120896707]
设计实现的功能有如下:
1.查找商品
2.删除购物车内商品
3.添加商品到购物车
4.添加新商品
5.计算购物车内商品总价
6.修改购物车内商品数量
通过不同颜色的箭头,来表示不同流程进行的方向,使流程图看起来更简单。
用户进入主菜单,根据主菜单可以显示商品信息,查询商品,将商品加入购物车,也可以删除购物车的商品,也可以修改加入购物车中的商品的数量,能显示购物车中商品的总价格付款。
1、Menu类
package 购物车程序的面向对象设计;
public class Menu {//菜单显示
public void display() {
System.out.println("=============================");
System.out.println(" 菜单界面 ");
System.out.println(" a.购物车商品显示 ");
System.out.println(" b.购物车添加商品 ");
System.out.println(" c.删除购物车商品 ");
System.out.println(" d.结算购物车商品 ");
System.out.println(" e.修改(添加/删除)购物车商品数量 ");
System.out.println(" f.查找购物车内商品 ");
System.out.println(" g.清空购物车内商品 ");
System.out.println("==========请输入选项: ");
}
}
2、Commidity类
package 购物车程序的面向对象设计;
public class Commidity {
private int id;//商品编号
private String name;//商品名称
private double price;//商品价格
private int number;//商品数量
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return number;
}
public void setNum(int num) {
this.number = num;
}
public String toString() {
return "Commidity [id=" + id + ", name=" + name + ", price=" + price + ", num=" + number + "]";
}
public Commidity(int id, String name, double price, int num) {
this.id = id;
this.name = name;
this.price = price;
this.number = num;
}
}
3、Cart类
package 购物车程序的面向对象设计;
import java.util.ArrayList;
import java.util.Scanner;
import java.math.BigDecimal;
public class Cart {
public static ArrayList<Commidity> shoplist = new ArrayList<>();
public static void putCart(Commidity c) {
if(c==null) {
return;
}
Commidity goods = findGoods(c);
if(goods==null) {
shoplist.add(c);
}
else {
ConsoleGUI.putCartshow();
return;
}
}
public static boolean delete(Integer id) {//直接从购物车列表里删除
if(id==null) {
return false;
}
Commidity index = findById(id);
if(index==null) {
return false;
}
else {
shoplist.remove(index);
}
return true;
}
/*for (Commidity commidity : shoplist) {
if(commidity.getName().equals(ProductName)) {
if(commidity.getNum()<=1) {
int index = commidity.getId();
shoplist.remove(index);
}
else {
String Number = in.nextLine();
int num = Integer.parseInt(Number);
commidity.setNum(commidity.getNum() + num);
if(commidity.getNum()<=0) {
shoplist.remove(commidity);
}
}
}
}
return true;*/
public static Commidity findByName(String name) {//按商品品名搜索
for (int i = 0;i < shoplist.size();i++) {
if(name.equals(shoplist.get(i).getName())) {
return shoplist.get(i);
}
}
return null;
}
public static Commidity findById(int id) {//按商品编号搜索
//int flag = 0;
for (int i=0;i<shoplist.size();i++) {
if(shoplist.get(i).getId()==id) {
return shoplist.get(i);
}
}
return null;
}
private static Commidity findGoods(Commidity c) {//配合添加购物车商品检索购物车内是否已存在该商品
if(c==null) {
return null;
}
for(int i=0;i<shoplist.size();i++) {
Commidity e = shoplist.get(i);
if(c.getName().equals(e.getName())){
return shoplist.get(i);
}
}
return null;
}
public static void modifyNum(String m,int num) {//修改商品数量 增加or减少
for (Commidity c : shoplist) {
if(m.equals(c.getName()))
c.setNum(c.getNum() + num);
if(c.getNum()<=0) {
shoplist.remove(c);
ConsoleGUI.showModifyresult();
return;
}
}
}
public static void addCommidity(int id,String name,double price,int num) {
Commidity c = new Commidity(id,name, price, num);
putCart(c);
}
public static void displayAll() {//显示购物车所有商品
ConsoleGUI.displayAll(shoplist);
}
public static double totalPrice() {//计算购物车内所有商品的总价
double allPrice = 0;
for (int i = 0;i<shoplist.size();i++) {
double p = shoplist.get(i).getPrice();//商品价格
int n = shoplist.get(i).getNum();//商品数量
String s = n + "";
BigDecimal Price = new BigDecimal(p);
BigDecimal num = new BigDecimal(s);
allPrice += Price.multiply(num).doubleValue();
}
return allPrice;
/*double price = 0;
for (int i = 0;i
}
/*public static void display(int index) {//显示方法
System.out.println("【商品编号 = "+shoplist.get(index).getId()+" 商品名称 = "+shoplist.get(index).getName()+" 商品价格 = "+shoplist.get(index).getPrice()+" 商品数量 = "+shoplist.get(index).getNum()+"】");
}*/
public static void clearCart() {//清空购物车
shoplist.clear();
ConsoleGUI.clearShow();
return;
}
}
**
4、ConsoleGUI类
package 购物车程序的面向对象设计;
import java.util.ArrayList;
public class ConsoleGUI {
public static void showMenu() {
Menu m = new Menu();
m.display();
}
public static void showCommidityByid(Commidity c) {//按商品id查找显示界面
System.out.println("【商品编号 = "+c.getId()+" 商品名称 = "+c.getName()+" 商品价格 = "+c.getPrice()+" 商品数量 = "+c.getNum()+"】");
}
public static void showCommidityByName(Commidity c) {//按商品品名查找显示界面
System.out.println("【商品编号 = "+c.getId()+" 商品名称 = "+c.getName()+" 商品价格 = "+c.getPrice()+" 商品数量 = "+c.getNum()+"】");
}
public static void showModifyresult() {//修改结果界面
System.out.println("===========该商品已在购物车内清空!!!===========");
}
public static void displayAll(ArrayList<Commidity> shoplist) {//购物车内所有商品显示界面
if(shoplist == null) {
System.out.println("=========购物车内无商品!!!=========");
}
for (Commidity commidity : shoplist) {
if(commidity == null) {
System.out.println("=========购物车内无该商品!!!=========");
}
System.out.println("【商品编号 = " + commidity.getId() + " 商品名称 = " + commidity.getName() +" 商品价格 = " + commidity.getPrice() + " 商品数量 = " + commidity.getNum()+ "】");
}
}
public static void putCartshow() {//放入购物车显示界面
System.out.println("===========购物车内已有此商品===========");
}
public static void priceShow(double TOTAL_PRICE) {//价格显示界面
System.out.println("========商品总价钱 = " + TOTAL_PRICE +"======== ");
}
public static void clearShow() {//购物车清空界面
System.out.println("========购物车清空成功!!!========");
}
}
package 购物车程序的面向对象设计;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String n;
String m;
Cart cart = new Cart();
Menu menu = new Menu();
Commidity c1 = new Commidity(1,"IPhonex",3690,1);
Commidity c2 = new Commidity(2,"IPhone11",4199,1);
Commidity c3 = new Commidity(3,"IPhone12",4699,1);
Commidity c4 = new Commidity(4,"IPhone13",5299,1);
Commidity c5 = new Commidity(5,"IPhone14",5999,1);
cart.putCart(c1);
cart.putCart(c2);
cart.putCart(c3);
cart.putCart(c4);
cart.putCart(c5);
menu.display();
while(in.hasNextLine()) {
String choice = in.nextLine();
ConsoleGUI.showMenu();
switch(choice)
{
case"a":
Cart.displayAll();
break;
case"b":
addCommidity(cart,in);
break;
case"c":
//a = in.nextInt();
//cart.delete(a);
delete(cart, in);
break;
case"d":
Price();
break;
case"e":
Modify(cart,in);
break;
case"f":
Search(cart,in);
break;
case"g":
Clear(cart);
break;
}
}
in.close();
}
public static void addCommidity(Cart cart,Scanner in) {
System.out.println("========请输入新加商品编号:");
int ID = in.nextInt();
in.nextLine();
System.out.println("========请输入新加商品品名:");
String name = in.nextLine();
System.out.println("========请输入新加商品价格:");
double price = in.nextDouble();
System.out.println("========请输入新加商品数量:");
int Num = in.nextInt();
Cart.addCommidity( ID , name , price , Num );
}
public static void Search(Cart cart,Scanner in) {
System.out.println("========<1> 按商品编号搜索========");
System.out.println("========<2> 按商品品名搜索========");
int option = in.nextInt();
if(option==1) {
System.out.println("========请输入商品编号:");
int id = in.nextInt();
Commidity c = cart.findById(id);
if(c==null) {
System.out.println("========未找到该商品========");
System.out.println("========a:返回主菜单界面========\n========b:继续搜索商品========");
String o = in.nextLine();
if(o.equals("a")) {
ConsoleGUI.showMenu();
}
if(o.equals("b")) {
Search(cart,in);
}
}
else {
System.out.println("========查找到商品如下========");
ConsoleGUI.showCommidityByid(c);
}
}
if(option==2) {
in.nextLine();
System.out.println("========请输入商品品名:");
String name = in.nextLine();
Commidity c = cart.findByName(name);
if(c == null) {
System.out.println("========未找到该商品========");
System.out.println("========a:返回主菜单界面========\n========b:继续搜索商品========");
String o = in.nextLine();
if(o.equals("a")) {
ConsoleGUI.showMenu();
}
if(o.equals("b")) {
Search(cart,in);
}
}
else {
System.out.println("========查找到商品如下========");
ConsoleGUI.showCommidityByName(c);
}
}
}
public static void delete(Cart cart,Scanner in) {
System.out.println("===========请输入要删除商品的编号:");
Integer id = in.nextInt();
if(cart.delete(id)) {
System.out.println("===========购物车商品删除成功===========");
}
else {
System.out.println("===========购物车无此商品===========");
}
}
public static void Price() {
double TOTAL_PRICE = Cart.totalPrice();
ConsoleGUI.priceShow(TOTAL_PRICE);
}
public static void Modify(Cart cart,Scanner in) {
System.out.println("========请输入想要修改的商品名:");
String name = in.nextLine();
System.out.println("========请输入想要修改商品的商品数量:");
int num = in.nextInt();
cart.modifyNum(name,num);
System.out.println("========修改商品数量成功========");
}
public static void Clear(Cart cart) {
cart.clearCart();
//System.out.println("========购物车清空成功!!!========");
}
/*public static void Join1() {
for(int i=0;i
/*public static Commidity[] input1(Scanner in) {
Cart cart = new Cart();
int n = in.nextInt();
Commidity[] s = new Commidity[n];
for(int i =0;i
}
5、面向对象设计的全部代码块
package 购物车程序的面向对象设计;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String n;
String m;
Cart cart = new Cart();
Menu menu = new Menu();
Commidity c1 = new Commidity(1,"IPhonex",3690,1);
Commidity c2 = new Commidity(2,"IPhone11",4199,1);
Commidity c3 = new Commidity(3,"IPhone12",4699,1);
Commidity c4 = new Commidity(4,"IPhone13",5299,1);
Commidity c5 = new Commidity(5,"IPhone14",5999,1);
cart.putCart(c1);
cart.putCart(c2);
cart.putCart(c3);
cart.putCart(c4);
cart.putCart(c5);
menu.display();
while(in.hasNextLine()) {
String choice = in.nextLine();
ConsoleGUI.showMenu();
switch(choice)
{
case"a":
Cart.displayAll();
break;
case"b":
addCommidity(cart,in);
break;
case"c":
//a = in.nextInt();
//cart.delete(a);
delete(cart, in);
break;
case"d":
Price();
break;
case"e":
Modify(cart,in);
break;
case"f":
Search(cart,in);
break;
case"g":
Clear(cart);
break;
}
}
in.close();
}
public static void addCommidity(Cart cart,Scanner in) {
System.out.println("========请输入新加商品编号:");
int ID = in.nextInt();
in.nextLine();
System.out.println("========请输入新加商品品名:");
String name = in.nextLine();
System.out.println("========请输入新加商品价格:");
double price = in.nextDouble();
System.out.println("========请输入新加商品数量:");
int Num = in.nextInt();
Cart.addCommidity( ID , name , price , Num );
}
public static void Search(Cart cart,Scanner in) {
System.out.println("========<1> 按商品编号搜索========");
System.out.println("========<2> 按商品品名搜索========");
int option = in.nextInt();
if(option==1) {
System.out.println("========请输入商品编号:");
int id = in.nextInt();
Commidity c = cart.findById(id);
if(c==null) {
System.out.println("========未找到该商品========");
System.out.println("========a:返回主菜单界面========\n========b:继续搜索商品========");
String o = in.nextLine();
if(o.equals("a")) {
ConsoleGUI.showMenu();
}
if(o.equals("b")) {
Search(cart,in);
}
}
else {
System.out.println("========查找到商品如下========");
ConsoleGUI.showCommidityByid(c);
}
}
if(option==2) {
in.nextLine();
System.out.println("========请输入商品品名:");
String name = in.nextLine();
Commidity c = cart.findByName(name);
if(c == null) {
System.out.println("========未找到该商品========");
System.out.println("========a:返回主菜单界面========\n========b:继续搜索商品========");
String o = in.nextLine();
if(o.equals("a")) {
ConsoleGUI.showMenu();
}
if(o.equals("b")) {
Search(cart,in);
}
}
else {
System.out.println("========查找到商品如下========");
ConsoleGUI.showCommidityByName(c);
}
}
}
public static void delete(Cart cart,Scanner in) {
System.out.println("===========请输入要删除商品的编号:");
Integer id = in.nextInt();
if(cart.delete(id)) {
System.out.println("===========购物车商品删除成功===========");
}
else {
System.out.println("===========购物车无此商品===========");
}
}
public static void Price() {
double TOTAL_PRICE = Cart.totalPrice();
ConsoleGUI.priceShow(TOTAL_PRICE);
}
public static void Modify(Cart cart,Scanner in) {
System.out.println("========请输入想要修改的商品名:");
String name = in.nextLine();
System.out.println("========请输入想要修改商品的商品数量:");
int num = in.nextInt();
cart.modifyNum(name,num);
System.out.println("========修改商品数量成功========");
}
public static void Clear(Cart cart) {
cart.clearCart();
//System.out.println("========购物车清空成功!!!========");
}
/*public static void Join1() {
for(int i=0;i
/*public static Commidity[] input1(Scanner in) {
Cart cart = new Cart();
int n = in.nextInt();
Commidity[] s = new Commidity[n];
for(int i =0;i
}
class Commidity {
private int id;//商品编号
private String name;//商品名称
private double price;//商品价格
private int number;//商品数量
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return number;
}
public void setNum(int num) {
this.number = num;
}
public String toString() {
return "Commidity [id=" + id + ", name=" + name + ", price=" + price + ", num=" + number + "]";
}
public Commidity(int id, String name, double price, int num) {
this.id = id;
this.name = name;
this.price = price;
this.number = num;
}
}
class Menu {//菜单显示
public void display() {
System.out.println("=============================");
System.out.println(" 菜单界面 ");
System.out.println(" a.购物车商品显示 ");
System.out.println(" b.购物车添加商品 ");
System.out.println(" c.删除购物车商品 ");
System.out.println(" d.结算购物车商品 ");
System.out.println(" e.修改(添加/删除)购物车商品数量 ");
System.out.println(" f.查找购物车内商品 ");
System.out.println(" g.清空购物车内商品 ");
System.out.println("==========请输入选项: ");
}
}
class Cart {
public static ArrayList<Commidity> shoplist = new ArrayList<>();
public static void putCart(Commidity c) {
if(c==null) {
return;
}
Commidity goods = findGoods(c);
if(goods==null) {
shoplist.add(c);
}
else {
ConsoleGUI.putCartshow();
return;
}
}
public static boolean delete(Integer id) {//直接从购物车列表里删除
if(id==null) {
return false;
}
Commidity index = findById(id);
if(index==null) {
return false;
}
else {
shoplist.remove(index);
}
return true;
}
/*for (Commidity commidity : shoplist) {
if(commidity.getName().equals(ProductName)) {
if(commidity.getNum()<=1) {
int index = commidity.getId();
shoplist.remove(index);
}
else {
String Number = in.nextLine();
int num = Integer.parseInt(Number);
commidity.setNum(commidity.getNum() + num);
if(commidity.getNum()<=0) {
shoplist.remove(commidity);
}
}
}
}
return true;*/
public static Commidity findByName(String name) {//按商品品名搜索
for (int i = 0;i < shoplist.size();i++) {
if(name.equals(shoplist.get(i).getName())) {
return shoplist.get(i);
}
}
return null;
}
public static Commidity findById(int id) {//按商品编号搜索
//int flag = 0;
for (int i=0;i<shoplist.size();i++) {
if(shoplist.get(i).getId()==id) {
return shoplist.get(i);
}
}
return null;
}
private static Commidity findGoods(Commidity c) {//配合添加购物车商品检索购物车内是否已存在该商品
if(c==null) {
return null;
}
for(int i=0;i<shoplist.size();i++) {
Commidity e = shoplist.get(i);
if(c.getName().equals(e.getName())){
return shoplist.get(i);
}
}
return null;
}
public static void modifyNum(String m,int num) {//修改商品数量 增加or减少
for (Commidity c : shoplist) {
if(m.equals(c.getName()))
c.setNum(c.getNum() + num);
if(c.getNum()<=0) {
shoplist.remove(c);
ConsoleGUI.showModifyresult();
return;
}
}
}
public static void addCommidity(int id,String name,double price,int num) {
Commidity c = new Commidity(id,name, price, num);
putCart(c);
}
public static void displayAll() {//显示购物车所有商品
ConsoleGUI.displayAll(shoplist);
}
public static double totalPrice() {//计算购物车内所有商品的总价
double allPrice = 0;
for (int i = 0;i<shoplist.size();i++) {
double p = shoplist.get(i).getPrice();//商品价格
int n = shoplist.get(i).getNum();//商品数量
String s = n + "";
BigDecimal Price = new BigDecimal(p);
BigDecimal num = new BigDecimal(s);
allPrice += Price.multiply(num).doubleValue();
}
return allPrice;
/*double price = 0;
for (int i = 0;i
}
/*public static void display(int index) {//显示方法
System.out.println("【商品编号 = "+shoplist.get(index).getId()+" 商品名称 = "+shoplist.get(index).getName()+" 商品价格 = "+shoplist.get(index).getPrice()+" 商品数量 = "+shoplist.get(index).getNum()+"】");
}*/
public static void clearCart() {//清空购物车
shoplist.clear();
ConsoleGUI.clearShow();
return;
}
}
class ConsoleGUI {
public static void showMenu() {
Menu m = new Menu();
m.display();
}
public static void showCommidityByid(Commidity c) {//按商品id查找显示界面
System.out.println("【商品编号 = "+c.getId()+" 商品名称 = "+c.getName()+" 商品价格 = "+c.getPrice()+" 商品数量 = "+c.getNum()+"】");
}
public static void showCommidityByName(Commidity c) {//按商品品名查找显示界面
System.out.println("【商品编号 = "+c.getId()+" 商品名称 = "+c.getName()+" 商品价格 = "+c.getPrice()+" 商品数量 = "+c.getNum()+"】");
}
public static void showModifyresult() {//修改结果界面
System.out.println("===========该商品已在购物车内清空!!!===========");
}
public static void displayAll(ArrayList<Commidity> shoplist) {//购物车内所有商品显示界面
if(shoplist == null) {
System.out.println("=========购物车内无商品!!!=========");
}
for (Commidity commidity : shoplist) {
if(commidity == null) {
System.out.println("=========购物车内无该商品!!!=========");
}
System.out.println("【商品编号 = " + commidity.getId() + " 商品名称 = " + commidity.getName() +" 商品价格 = " + commidity.getPrice() + " 商品数量 = " + commidity.getNum()+ "】");
}
}
public static void putCartshow() {//放入购物车显示界面
System.out.println("===========购物车内已有此商品===========");
}
public static void priceShow(double TOTAL_PRICE) {//价格显示界面
System.out.println("========商品总价钱 = " + TOTAL_PRICE +"======== ");
}
public static void clearShow() {//购物车清空界面
System.out.println("========购物车清空成功!!!========");
}
}
1 .购物车商品显示
2 .购物车添加商品
3 .删除购物车商品
4 .结算购物车商品
5 .添加购物车商品数量
6 .商品搜索与初始化
7 .清空购物车
在Cart类中建立了一个commidity下的shoplist的一个动态数组来进行购物车各功能实现的一个操作
Eclipse