java大作业_Java大作业——购物车

这是一个使用Java编写的购物车系统,包括商品展示、添加、删除、显示和结算功能。用户可以输入商品ID和数量进行操作,系统会根据商品ID进行相应的增删查改,并在结算时计算总价。
摘要由CSDN通过智能技术生成

成员:詹毅斌,王昕,罗吉洋。

前期调查

e2ed5154d7d196f3f29592ab1c7cdddf.png

系统功能结构图

c4e577055f9a1cac35b3a02e2db51e8a.png

系统描述

系统打印出所有商品,然后根据现有的商品的ID,可以将商品添加到购物车里,也可以进行删除,显示购物车里的商品信息,能清空购物车里的商品,最后结算购物车计算出总价。

UML类图

00ea389e4a0d31bc75b5a014b5369d20.png

代码

1.Main类

mian方法,进入商品目录界面和进行选择操作。

package Shopping;

import java.awt.AWTException;

import java.util.Scanner;

public class Main {

public static void main(String[] args) throws AWTException {

Scanner sc = new Scanner(System.in);

int n,num,id,flat = 0;

Mall mall = new Mall();

Cart cart = new Cart();

while(true) {

mall.showGoods();

Menu.menu();

n = Integer.valueOf(sc.nextLine());

switch (n) {

case 1:

System.out.println("请输入要增加的商品ID:");

id = Integer.valueOf(sc.nextLine());

System.out.println("请输入要增加的商品数量:");

num = Integer.valueOf(sc.nextLine());

if(id>5||id<=0)

{

System.out.println("不存在此商品");

System.out.println("请按任意键继续");

String s=sc.nextLine();

break;

}

for(int i = 0; i < num; i++) {

cart.add(mall.searchById(id));

}

System.out.println("请按任意键继续");

String s=sc.nextLine();

// Menu.clear_all();

break;

case 2:

if(cart.Len()==0) {

System.out.println("购物车为空");

System.out.println("请按任意键继续");

s=sc.nextLine();

// Menu.clear_all();

break;

}

System.out.println("请输入要删除的商品ID:");

id = Integer.valueOf(sc.nextLine());

System.out.println("请输入要减少的商品数量:");

num = Integer.valueOf(sc.nextLine());

if(cart.find(id)==0) {

System.out.println("请按任意键继续");

s=sc.nextLine();

// Menu.clear_all();

break;

}

for(int i = 0; i < num; i++) {

cart.remove(id);

}

System.out.println("删除成功");

System.out.println("请按任意键继续");

s=sc.nextLine();

// Menu.clear_all();

break;

case 3:

cart.diplayAll();

System.out.println("请按任意键继续");

s=sc.nextLine();

// Menu.clear_all();

break;

case 4:

cart.clear();

System.out.println("已清空");

System.out.println("请按任意键继续");

s=sc.nextLine();

// Menu.clear_all();

break;

case 5:

cart.settleaccounts();

System.out.println("请按任意键继续");

s=sc.nextLine();

// Menu.clear_all();

break;

case 0:

flat = 1;

break;

default:

break;

}

if(flat == 1) {

System.out.println("成功退出!");

break;

}

}

sc.close();

}

}

2.Commodity类

定义商品属性,商品的ID,商品名,商品价格,描述,数量

package Shopping;

public class Commodity { //商品

private Integer id;

private String name;

private Double price;

private String description;

public Commodity() {

}

public Commodity(Integer id, String name, Double price, String description) {

super();

this.id = id;

this.name = name;

this.price = price;

this.description = description;

}

public Integer getId() {

return id;

}

public void setId(Integer 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 String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

@Override

public String toString() {

return "商品 [ID:" + id + ", 名称:" + name + ", 单价:" + price + ", 类型:" + description + "]";

}

}

3.CartDAO类

package Shopping;

public interface CartDAO {

public void add(Commodity e);

public boolean remove(Integer id);

public void diplayAll();

public int findById(Integer id);

public void clear();

public void settleaccounts();

}

4.Cart类

对购物车进行添加,删除,查看,结算,清空等操作

package Shopping;

import java.util.ArrayList;

import java.util.List;

public class Cart implements CartDAO{

private List itemList;

public Cart(){

itemList = new ArrayList<>();

}

public int Len() {//购物车商品条目数

return itemList.size();

}

public int find(int id)//判断购物车是否存在该商品

{

int index = findById(id);

if(index==-1)

{

System.out.println("不存在该商品");

return 0;

}

return 1;

}

public void add(Commodity e){//添加商品

if (e == null){

return ;

}

int index = findById(e.getId());

if (index == -1){//如果不包含该商品的条目

itemList.add(new ItemEntry(e));

}else{

itemList.get(index).increase();

}

}

public boolean remove(Integer id){//删除商品

if(itemList.size()==0) {

System.out.println("购物车为空");

return false;

}

if (id==null)

return false;

int index = findById(id);

if (index == -1){//未找到

return false;

}else{

ItemEntry entry = itemList.get(index);

if (entry.getQty() <= 1){//移除相关条目qty<=0,则删除条目

itemList.remove(index);

}else{

entry.decrease();

}

}

return true;

}

public void diplayAll(){

if(itemList.size()==0)

System.out.println("购物车为空");

for (ItemEntry itemEntry : itemList) {

System.out.println(itemEntry);

}

}

public int findById(Integer id){//在购物车中寻找商品

for (int i = 0; i < itemList.size(); i++) {

if (itemList.get(i).getItem().getId().equals(id))

return i;

}

return -1;

}

public void clear() {//清空购物车

itemList.clear();

}

public void settleaccounts() {//结算购物车

double sum=0;

int num=0;

for (int i=0;i

sum=sum+itemList.get(i).totalPrice;

num=num+itemList.get(i).qty;

}

System.out.println("共"+itemList.size()+"种商品,共"+num+"件商品");

System.out.println("总计"+sum+"元");

this.clear();

}

private class ItemEntry{ //内部类:购物车条目类

Commodity item;//商品

Integer qty;//数量

private double totalPrice=0.0;

public ItemEntry(Commodity item) {

this.item = item;

qty = 1;

totalPrice=item.getPrice();

}

//添加

public void increase(){

qty++;

totalPrice=item.getPrice()*qty;

}

//减少

public void decrease(){

qty--;

totalPrice=item.getPrice()*qty;

}

public Commodity getItem() {

return item;

}

public Integer getQty() {

return qty;

}

@Override

public String toString() {

return item + ", 商品数量=" + qty + ", 小计=" + totalPrice + "]";

}

}

}

5.Mall类

打印商品目录和查找商品

package Shopping;

import java.util.ArrayList;

import java.util.List;

public class Mall {

private List commodities = new ArrayList();

{

commodities.add(new Commodity(1, "iphone 12", 8199.00,"手机"));

commodities.add(new Commodity(2, "华为 Mate40 Pro", 8199.00,"手机"));

commodities.add(new Commodity(3, "茅台飞天", 1499.00,"酒"));

commodities.add(new Commodity(4, "华硕VG259Q", 1449.00,"显示器"));

commodities.add(new Commodity(5, "java学习笔记", 94.10, "书"));

}

public void showGoods() {// 展示商品

for (Commodity e : commodities) {

System.out.println(e);

}

}

public Commodity searchById(int id) {// 按编号搜索商品

int i = 0;

for (i = 0; i < commodities.size(); i++) {

if (commodities.get(i).getId() == (id)) {

return commodities.get(i);

}

}

return null;

}

}

6.Menu类

操作选项

package Shopping;

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

public class Menu {

public static void clear_all() throws AWTException {

Robot r = new Robot();

r.mousePress(InputEvent.BUTTON3_DOWN_MASK); //按下鼠标右键

r.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); //释放鼠标右键

r.keyPress(KeyEvent.VK_CONTROL); // 按下Ctrl键

r.keyPress(KeyEvent.VK_R); // 按下R键

r.keyRelease(KeyEvent.VK_R); // 释放R键

r.keyRelease(KeyEvent.VK_CONTROL); // 释放Ctrl键

r.delay(10);

}

public static void menu() {

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("[0]:退出");

}

}

Gitee

用来完成大作业的。文档内容: 1 Java技术体系 1.1 Java语言 1.2 Java平台 1.3 Java应用领域 2 Java语言的技术特点 2.1 1 2.2 2 2.3 3 3 Java语言与C++的异同分析总结。 4 选用C和java语言时编程算法程序有什么不同,有什么优势和劣势。 5 自己编程学习的级别和状态。以及自己以后的编程学习的计划和想法。 6 下面3道题目中选一道,给出算法分析和程序。 1)“黄金分割数”在我们的生活中很常见,但是在不同的应用领域,要求的精度也不一样。 例如:三位小数是0.618 现在我们需要你能求出保留100位小数的黄金分割数,采用的算法为“分层计算法”: 黄金数= 1 --------------- 1+ 1 ------------- 1+ 1 ----------- 1+ 1 --------- ..... 注意,计算出的结果,如果第100位为0也需要保留。 2)已知一个数列: 5,2,4,3,7,6 那么,在这个数列中存在这样一些“连续数”,例如:5,2,4,3这个子数列排序后是连续的。同样2,4,3也是连续的,为了方便表示 我们使用下标来标识,这样,这个数列中存在以下“连续数”: [1,1] [1,4] [1,6] [2,2] [2,4] [3,3] [3,4] [4,4] [5,5] [5,6] [6,6] 这样,他就存在11个“连续数”。现在需要你在用户找出一个数组中所有的“连续数”。 要求: 1、用户输入一个整数N,表示下面数组的个数 2、用户每输入一行作为一个数组 如: 用户输入: 1 5,2,4,3,7,6 程序输出: 11 3)有一种数我们称之为幸运数,它的特点是这样的,首先,由自然数按顺序排列: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 … 这样,1比较特殊, 1为第一个幸运数,那么,我们移除掉序号能被2整除的数(注意:是序号,而不是数本身,每次移除后都重新排序)就剩下: 1 3 5 7 9 11 13 15 17 19… 3为第二个幸运数,那么我们需要去掉序号能被3(下一次是除4,然后是5,每次加1)整除的数,5 11 17...剩下: 1 3 7 9 13 15 19… 那么7为第三个幸运数,后面的幸运数,依此类推,移除之后剩下的数字都是幸运数。 现在我们需要你求出给定的m和n之间的幸运数的个数: 例如:给定1 20,那么个数为:5(5个幸运数分别是1,3,7,13,19) 现在要求用户输入两个数m和n(m<n<=1000*1000),输出幸运数的个数。 例如: 用户输入: 1 20 程序输出: 5 格式:小四,1.5倍行距
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值