用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%支持多账户登录支持账户间转账记录每月日常消 ...
随机推荐
- 语义化的HTML及其目的
一.什么是语义化的HTML? 语义化的HTML就是写出的HTML代码,符合内容的结构化(内容语义化),选择合适的标签(代码语义化),能够便于开发者阅读和写出更优雅的代码的同时让浏览器的爬虫和机器很好地 ...
- 浏览器根对象document之字符串属性
1.1 停止使用的属性 fgColor.linkColor.vlinkColor.alinkColor.bgColor. 1.2 文档地址 document.URL 与documentURI属性返回同 ...
- 数字时钟(DigitalClock)
数字时钟(DigitalClock) 这个其实就是我们平时看到的手机上面显示的时间 很简单 1.Activity //数字时钟 public class DigitalClockActivity ex ...
- android开启线程的误区
发现一些刚学android的人,和我当初一样,对android的线程会存在着一定误区. 在android中,开启新线程时,一些人会用以下方法: new Handler().post(r); 但是这样并 ...
- TOMCAT配置SSL认证为HTTPS协议服务
1 . 问题概述 很多安全性要求较高的系统,都会使用安全套接字层(SSL)进行信息交换, Sun为了解决在Internet上的实现安全信息传输的解决方案.它实现了SSL和TSL(传输层安全)协议 ...
- 《Pro Git》轻松学习版本控制
转自 https://kindlefere.com/post/333.html 什么是“版本控制”?我为什么要关心它呢?版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.在 ...
- C++发展概述、优缺点及应用领域
个人观点(C++虽功能强大,但是底层还是调用C,C++为了吸引更多的C程序员,功能过于丰富且复杂,一定程度上反而降低了可编程的实用性.但是不可否认C++ 也是一门艺术.) C++是一门以C为基础发展而 ...
- [控件] ColorfulProgressView
ColorfulProgressView 效果 说明 1. 支持颜色定制 2. 进度条动画可以自己控制 3. 简单易用 源码 https://github.com/YouXianMing/UI-Com ...
- 封装CoreGraphics的API简化绘图操作
封装CoreGraphics的API简化绘图操作 效果 说明 1. 将CoreGraphics的API接口抽象为对象,让绘图变得简单易懂 2. 简化常用的绘制操作 3. 源码长期更新 源码 https ...
- SpringBoot @AutoWired Null
在调用工具类时,若工具类中含有@Autowired注解,这此工具类对象必须同样使用@Autowired注解,否则工具类中的Spring注入的对象都为空值,这里的HadoopTest就是这样 比如MyC ...