用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());
}
}
}

}

转载于:https://www.cnblogs.com/xiaopangyu/p/9247280.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 购物车类 */ public class Cart { //创建一个map对象,用来保存商品,key为商品,value为商品的数量 private Map<Goods, Integer> map = new HashMap<Goods, Integer>(); /** * 添加商品到购物车方法 * @param id 商品编号 * @param quantity 商品数量 */ public void addGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ Integer oQuantity = map.get(goods);//获取商品在购物车中原本的数量 if(oQuantity!=null){ oQuantity += quantity; }else{ oQuantity = quantity; } map.put(goods, oQuantity);//添加商品到map中 System.out.println("添加商品"+goods.getName()+"成功!"); }else{ System.out.println("添加失败!商品编号不存在!"); } } /** * 按指定的编号删除商品 * @param id 商品编号 */ public void delGoods(int id){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.remove(goods);//从map删除商品 System.out.println("删除商品"+goods.getName()+"成功!"); }else{ System.out.println("删除失败!商品编号不存在!"); } } /** * 修改商品数量方法 * @param id 商品编号 * @param quantity 要修改的商品数量 */ public void updateGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.put(goods, quantity);//从map删除商品 }else{ System.out.println("修改失败!商品编号不存在!"); } } /** * 打印购物车商品列表 */ public void show(){ Set<Entry<Goods, Integer>> entrySet = map.entrySet(); System.out.println("编号\t单价\t数量\t名称\t总价"); for(Entry<Goods, Integer> entry:entrySet){ Goods goods = entry.getKey(); Integer quantity = entry.getValue(); System.out.println(goods.getId()+"\t"+goods.getPrice()+"\t"+quantity+"\t"+goods.getName()+"\t"+goods.getPrice()*quantity); } } }
Java中的数组是一种线性数据结构,它由相同类型的元素组成并按照顺序存储在内存中。Java中的数组可以使用new关键字动态地创建,并且长度是固定的,一旦创建后就不能再改变它的大小。 Java中的ArrayList是一个动态数组,它可以自动扩容以适应元素的添加和删除。ArrayList实际上是基于数组实现的,它内部维护了一个Object类型的数组来存储元素。当ArrayList的容量不足时,它会根据一定的策略自动扩容数组的大小,以容纳更多的元素。 ArrayList的实现原理主要包括以下几个方面: 1. 初始容量和扩容因子:当创建一个ArrayList时,可以指定一个初始容量和一个扩容因子。初始容量指定了ArrayList内部数组的初始大小,扩容因子指定了数组增长的比例。默认情况下,初始容量为10,扩容因子为1.5。 2. 自动扩容:当ArrayList内部数组的容量不足时,它会自动扩容。具体实现是创建一个新的数组,将原数组中的元素复制到新数组中,并将新元素添加到新数组中。 3. 随机访问:由于ArrayList是基于数组实现的,因此它支持随机访问。可以通过索引来快速访问元素,时间复杂度为O(1)。 4. 插入和删除:在ArrayList中插入和删除元素的时间复杂度取决于操作的位置。如果在末尾插入或删除元素,时间复杂度为O(1),否则需要将后面的元素都向后移动,时间复杂度为O(n)。 总之,ArrayList是Java中常用的动态数组,它的实现基于数组,并且支持随机访问、自动扩容等特性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值