前言
在经过了连续几周的Java学习之后,我对java的认识已由当初的对于这些和Java有关的知识什么都不懂,到现在已经初窥门径了,在老师及同学的指导下初步完成了购物车系统的实现,虽然还有着一些漏洞。
团队介绍:
小组成员 | 成员分工 |
---|---|
张小荣 | 博客制作,面向对象设计,代码规范,类 |
徐可 | 前期调查,功能函数 |
一、前期调查
用户现在购物车界面中游览商品,然后将想要购买的商品放入购物车中。
二、购物车
功能结构图
uml图
一 类
Goods类
public class Goods{
private int id;
private String name;
private double price;
public Goods(int id,String name,double price) {
super();
this.id=id;
this.name = name;
this.price = price;
}
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;
}
}
功能
1.对商城的界面初始化
2.展示出所售卖商品的信息
Goodsinformation类
public class Goodsinformation {
private Goods product;
private int count;
public double totalMoney(){
double price=product.getPrice();
return price*count;
}
public Goodsinformation() {
super();
}
public Goodsinformation(Goods product, int count) {
super();
this.product = product;
this.count = count;
}
public Goods getProduct() {
return product;
}
public void setProduct(Goods product) {
this.product = product;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
功能
1.对于要购买的商品进行统计并算出总价
Menu类
class Menu{
public class Menu {
public static void showMenu(){
System.out.println(" 商品如下");
System.out.println(" 名称 售价");
System.out.println(" 101 计算机网络 45");
System.out.println(" 102 Java程序设计 59元");
System.out.println(" 103 数据结构 97元");
System.out.println(" 104 卫衣 128元");
System.out.println(" 105 手机 2999元");
}
public static void showFunction() {
System.out.println("------------------------------");
System.out.println("------- 欢迎光临! -------");
System.out.println("------------------------------");
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(" 0 退出购物车");
}
public static void endMenu() {
System.out.println("------------------------------");
System.out.println("------- 谢谢惠顾 -------");
System.out.println("------------------------------");
}
}
二 购物车的部分功能模块
以下为ShoppingCart类方法图解
Map接口中定义的常用方法及功能
方法声明 | 功能 |
---|---|
boolean containsKey() | 判断集合是否包含指定的键 |
Object get() | 从map中读取数据,若不包含该键的映射关系,返回null |
void put() | 将指定的值与此映射指定的键相关联 |
void clear() | 移除所有的键值对应的元素 |
Collection values() | 获取集合中所有值的集合 |
添加商品
public void addProduct(Goods p){
int productId=p.getId();
if(map.containsKey(productId)){
Goodsinformation productItem=map.get(productId);
Scanner sc = new Scanner(System.in);
System.out.println("请输入还要添加商品的数量:");
int n=sc.nextInt();
productItem.setCount(productItem.getCount()+n);
System.out.println("添加商品成功");
}else{
Scanner sc = new Scanner(System.in);
System.out.println("请输入添加商品的数量:");
int n=sc.nextInt();
map.put(productId, new Goodsinformation(p,n));
System.out.println("添加商品成功");
}
}
修改商品个数
public boolean modifyProduct(int productId,int count){
if(map.containsKey(productId)){
if(count>=1){
Goodsinformation productItem = map.get(productId);
productItem.setCount(count);
return true;
}else if(count==0){
map.remove(productId);
return true;
}
}
return false;
}
三 面向对象的封装性
public class Goods{
private int id;
private String name;
private double price;
在商品Goods类中的public,private体现了对象的封装性,封装起来可以把类内部的信息统一保护起来,外部只能通过setter和getter来对他们进行操作,保证数据的安全性,保证外部尽可能正确地使用这个类。这样就可以保证商品的信息不会吧轻易的篡改,更加具有安全性。
四 总结
在这次购物车系统的制作中,我发现了许多的小问题,都是由于对Java知识的不全面的理解造成的,所以这次也让我明白了Java的学习应该要多去练习编码,不要只停留在理论知识上。