用java数组模拟购物商城功能与实现
实体类1(商品):
package mall.model;
public class goods {
private int shoppingID; // 商品编号
private String shoppingName;// 商品名
private int price; // 商品价格
public goods(int shoppingID, String shoppingName, int price) {
this.shoppingID = shoppingID;
this.shoppingName = shoppingName;
this.price = price;
}
public goods() {
super();
}
public int getShoppingID() {
return shoppingID;
}
public void setShoppingID(int shoppingID) {
this.shoppingID = shoppingID;
}
public String getShoppingName() {
return shoppingName;
}
public void setShoppingName(String shoppingName) {
this.shoppingName = shoppingName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
实体类2(购物车)
package mall.model;
public class shoppingCart {
private int shoppingID; // 商品编号
private String shoppingName; // 商品名
private int price; // 商品价格
public shoppingCart() {
super();
}
public shoppingCart(int shoppingID, String shoppingName, int price) {
super();
this.shoppingID = shoppingID;
this.shoppingName = shoppingName;
this.price = price;
}
public int getShoppingID() {
return shoppingID;
}
public void setShoppingID(int shoppingID) {
this.shoppingID = shoppingID;
}
public String getShoppingName() {
return shoppingName;
}
public void setShoppingName(String shoppingName) {
this.shoppingName = shoppingName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
实体类3(用户)
package mall.model;
/**
* @author shixi1809 用户类
*/
public class user {
private String userName; // 用户名
private String passWord; // 用户密码
private int type; // 用户类型,1位管理员,2为会员
public user(String userName, String passWord, int type) {
super();
this.userName = userName;
this.passWord = passWord;
this.type = type;
}
public user() {
super();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public void show1() {
String ttt = null;
if (getType() == 1) {
ttt = "管理员";
} else if (getType() == 2) {
ttt = "会员";
}
System.out.println("用户名:" + getUserName() + ",密码:" + getPassWord()
+ ",用户类型:" + ttt);
}
}
测试类
package mall.controller;
import mall.function.service;
public class test {
public static void main(String[] args) {
System.out.println("请登录:");
service service = new service();
service.login();
}
}
功能实现
package mall.function;
import java.util.Scanner;
import mall.model.goods;
import mall.model.shoppingCart;
import mall.model.user;
public class service {
private static user user[] = new user[5];
private static goods good[] = new goods[5];
private static shoppingCart cart[] = new shoppingCart[5];
private static Integer num; // 登录的用户id
static {
for (int i = 0; i < good.length; i++) {
good[i] = new goods();
}
for (int i = 0; i < good.length; i++) {
cart[i] = new shoppingCart();
}
}
// 登录
@SuppressWarnings("resource")
public void login() {
// 给用户添加两个值,1为管理员,2位用户
user[0] = new user("admin", "admin", 1);
user[1] = new user("zhangsan", "zhangsan", 2);
Scanner scanner1 = new Scanner(System.in);
System.out.println("请输入用户名:");
String str1 = scanner1.next();
Scanner scanner2 = new Scanner(System.in);
System.out.println("请输入密码:");
String str2 = scanner2.next();
for (int i = 0; i < user.length; i++) {
if (user[i] != null) {
if (str1.equals(user[i].getUserName())
&& str2.equals(user[i].getPassWord())) {
if (user[i].getType() == 1) {
System.out.println("欢迎进入管理员界面!!");
while (true) {
System.out
.println("1.增加商品\t 2.删除商品\t 3.修改商品\t 4.商品显示\t 5.切换用户\t 0.退出系统");
Scanner scanner = new Scanner(System.in);
System.out.println("请选择功能:");
int sel = scanner.nextInt();
switch (sel) {
case 1:
add();
break;
case 2:
Scanner scan1 = new Scanner(System.in);
System.out.println("请输入要删除的商品编号:");
int sttr1 = scan1.nextInt();
System.out.println("是否确定要删除该商品?");
Scanner scan2 = new Scanner(System.in);
System.out
.println("请输入商品名(确认请输入'y',取消请输入'n'):");
String sttr2 = scan2.next();
if (sttr2.equals("y")) {
remove(sttr1);
} else if (sttr2.equals("n")) {
break;
} else {
System.out.println("输入有误!");
}
break;
case 3:
Scanner scan3 = new Scanner(System.in);
System.out.println("请输入商品编号:");
int strr = scan3.nextInt();
modify(strr);
break;
case 4:
show();
break;
case 5:
login();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("输入有误!");
break;
}
}
} else {
System.out.println("欢迎进入用户界面!!");
num = i; // 登录的用户id
System.out.println(num);
while (true) {
System.out
.println("1.购买商品\t 2.商品搜索\t 3.商品展示\t 4.切换用户\t 0.退出系统");
Scanner scanner = new Scanner(System.in);
System.out.println("请选择功能:");
int sel = scanner.nextInt();
switch (sel) {
case 1:
Scanner scannerBuy = new Scanner(System.in);
System.out.println("请输入商品编号:");
int buyNo = scannerBuy.nextInt();
buy(buyNo);
break;
case 2:
Scanner sele = new Scanner(System.in);
System.out.println("请输入商品名:");
String selName = sele.next();
select(selName);
break;
case 3:
show();
break;
case 4:
login();
break;
case 0:
System.exit(0);
break;
default:
System.out.println("输入有误!");
break;
}
}
}
}
}
}
System.out.println("用户名与密码不符,请重新输入!");
}
// 加入购物车
@SuppressWarnings("resource")
public void cartAdd(int no) {
for (int i = 0; i < cart.length; i++) {
if (cart[i].getShoppingID() == 0) {
cart[i] = new shoppingCart(good[no].getShoppingID(),
good[no].getShoppingName(), good[no].getPrice());
System.out.println("购物车添加成功!");
System.out.println("请选择继续购物或进入购物车:1.继续购物\t 2.进入购物车 ");
Scanner scanner1 = new Scanner(System.in);
System.out.println("请选择:");
int str1 = scanner1.nextInt();
switch (str1) {
case 1:
show();
break;
case 2:
cartShow();
break;
default:
System.out.println("输入有误!!");
break;
}
break;
}
}
}
// 搜索商品
@SuppressWarnings("resource")
public void select(String name) {
for (int i = 0; i < good.length; i++) {
if (good[i].getShoppingName() != null
&& good[i].getShoppingName() != "") {
if (good[i].getShoppingName().indexOf(name) != -1) {
for (int j = 0; j < good.length; j++) {
if (good[i].getShoppingName().equals(
good[j].getShoppingName())) {
System.out.println("商品编号:"
+ good[i].getShoppingID() + ",商品名:"
+ good[i].getShoppingName() + ",价格:"
+ good[i].getPrice());
System.out
.println("请选择购买商品或加入购物车:1.购买该商品\t 2.加入购物车\t 3.继续购物\t 4.进入购物车 ");
Scanner scanner1 = new Scanner(System.in);
System.out.println("请选择:");
int str1 = scanner1.nextInt();
switch (str1) {
case 1:
buy(j);
break;
case 2:
cartAdd(j);
break;
case 3:
show();
break;
case 4:
cartShow();
break;
default:
System.out.println("输入有误!!");
break;
}
}
}
}
}
}
}
// 购买商品
@SuppressWarnings("resource")
public void buy(int no) {
for (int i = 0; i < good.length; i++) {
if (no == good[i].getShoppingID()) {
System.out.println("商品编号:" + good[i].getShoppingID() + ",商品名:"
+ good[i].getShoppingName() + ",价格:"
+ good[i].getPrice());
System.out.println("是否确认购买(请回复y/n)?");
Scanner scan2 = new Scanner(System.in);
String sttr2 = scan2.next();
if (sttr2.equals("y")) {
System.out.println("商品购买成功!");
} else if (sttr2.equals("n")) {
System.out.println("商品购买失败!");
} else {
System.out.println("输入有误!");
}
}
}
}
// 添加商品
@SuppressWarnings("resource")
public void add() {
Scanner scanner1 = new Scanner(System.in);
System.out.println("请输入商品编号:");
int str1 = scanner1.nextInt();
Scanner scanner2 = new Scanner(System.in);
System.out.println("请输入商品名:");
String str2 = scanner2.next();
Scanner scanner3 = new Scanner(System.in);
System.out.println("请输入商品价格:");
int str3 = scanner3.nextInt();
for (int i = 0; i < good.length; i++) {
if (good[i].getShoppingID() != 0) {
if (good[i].getShoppingID() == str1) {
System.out.println("该商品已存在!!!");
return;
}
}
}
for (int i = 0; i < good.length; i++) {
if (good[i].getShoppingID() == 0) {
good[i] = new goods(str1, str2, str3);
System.out.println("商品添加成功!");
break;
}
}
}
// 移除商品
public void remove(int no) {
for (int i = 0; i < good.length; i++) {
if (good[i].getShoppingID() != 0) {
if (no == good[i].getShoppingID()) {
good[i] = new goods();
System.out.println("商品已删除!");
} else {
System.out.println("不存在该商品!");
}
}
}
}
// 商品修改
@SuppressWarnings("resource")
public void modify(int no) {
for (int i = 0; i < good.length; i++) {
if (good[i].getShoppingID() != 0) {
if (no == good[i].getShoppingID()) {
Scanner scanner2 = new Scanner(System.in);
System.out.println("请输入商品名:");
String str2 = scanner2.next();
Scanner scanner3 = new Scanner(System.in);
System.out.println("请输入商品价格:");
int str3 = scanner3.nextInt();
good[i] = new goods(no, str2, str3);
System.out.println("商品已修改。。。");
return;
}
}
}
System.out.println("商品未找到。。。");
}
// 显示商品
public void show() {
for (int i = 0; i < good.length; i++) {
if (good[i].getShoppingID() != 0) {
System.out.println("商品编号:" + good[i].getShoppingID() + ",商品名:"
+ good[i].getShoppingName() + ",价格:"
+ good[i].getPrice());
}
}
}
// 进入购物车
public void cartShow() {
System.out.println(user[num].getUserName() + "的购物车:");
for (int i = 0; i < cart.length; i++) {
if (cart[i].getShoppingID() != 0) {
System.out.println("商品编号:" + cart[i].getShoppingID() + ",商品名:"
+ cart[i].getShoppingName() + ",价格:"
+ cart[i].getPrice());
}
}
}
}
用java数组模拟购物商城功能与实现的更多相关文章
- Java数组模拟队列 + 优化
队列介绍 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则. 即:先存入队列的数据,要先取出.后存入的要后取出 示意图:(使用数组模拟队列示意图) 数组模拟队列 队列本身是有序列表 ...
- Java数组模拟栈
一.概述 注意:模拟战还可以用链表 二.代码 public class ArrayStack { @Test public void test() { Stack s = new Stack(5); ...
- 用java数组模拟登录和注册功能
package com.linkage.login; import java.util.Scanner; public class user { // 存储用户名和密码 public static S ...
- Java数组模拟队列
队列 先进先出 什么意思呢? 我的理解:队列就是一个数组(不包含链表),然后我们给它施加一个存数据和取数据的规则 当只允许从一端存数据,从另一端取数据的数组,就是队列,我们要做的就是给这个数组施加我们 ...
- Java数组模拟环形队列
2.环形队列 (上一篇队列:https://www.cnblogs.com/yxm2020/p/12676323.html) 百度百科 1.假溢出 系统作为队列用的存储区还没有满,但队列却发生了溢 ...
- Python实战之网上银行及购物商城
前言:这是初学时写的小项目,觉得有意思就写来玩玩,也当是巩固刚学习的知识.现在看来很不成熟,但还是记录一下做个纪念好了~ 1.名称:网上网上银行及购物商城 2.项目结构: 当时刚接触python啦,哪 ...
- 编写Java程序,模拟网上商城购物,当用户选好物品提交订单时,每笔订单会自动生成一个唯一的订单编号。
查看本章节 查看作业目录 需求说明: 模拟网上商城购物,当用户选好物品提交订单时,每笔订单会自动生成一个唯一的订单编号.而部分电子商务网站在数据高峰期时,一毫秒可能需要处理近千笔的订单 现在简单模拟 ...
- java 购物商城小项目训练
java web 模拟购物车练习(项目一) 首页(index.jsp) <div align="center" class="index"> < ...
- 模拟实现ATM+购物商城程序
流程图: 需求: ATM:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%支持多账户登录支持账户间转账记录每月日常消 ...
随机推荐
- apply与call简单用法以及判断数组的坑
1 typeof 和 instanceof var array = [];平时如果判断一个对象是否为数组,可能你会用 typeof array,但是输出为“object”. typeof 一般只能返回 ...
- Android中Handler的使用
当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无响应,如果时间过长,程序还会挂掉.Ha ...
- redis订阅与发布(把redis作为消息中间件)
订阅频道127.0.0.1:6379> subscribe chat1Reading messages... (press Ctrl-C to quit)1) "subscribe&q ...
- c# 将秒数转换成时,分,秒的方法
TimeSpan ts = , ,Convert.ToInt32( duration)); string str = ""; ) { str = ts.Hours.ToString ...
- USTCCourseCommunity 项目介绍
我们的项目名为USTCCourseCommunity,科大课程社区,主要提供课表管理.课程资源管理.课程信息管理.智能排课.轻松评课等方面的服务,旨在为科大师生提供便捷. 科大现有课程服务形式存在的问 ...
- leetCode题解之删除单链表中指定的元素
1.问题描述 Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
- 探讨Oracle分区表
一年又一年,又到年底了,对于数据库的分区表需要检查一下,有无最大分区,次分区是否需要追加分区,如果程序不是自动追加分区的话,那么年中结算的时候,就会报错. 1.oracle分区主要有五种类型 (1)R ...
- Allure 安装及使用
linux下安装方法 Allure requires Java 8 or higher npm install -g allure-commandline --save-dev (如果npm不能 ...
- Build path entry is missing: config 引起的 The project: configwhich is referenced by the classpath, does not exist.
运行Junit的时候报错, The project: XXXX which is referenced by the classpath, does not exist. 在Java Build Pa ...
- java中常用Redis操作
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向 ...