写一个Java的校园的商品管理系统

时间: 2024-04-22 14:25:52 浏览: 17
好的,校园商品管理系统可以包含以下功能: 1. 商品管理:包括商品的添加、修改、删除和查询功能; 2. 库存管理:包括库存的入库、出库、盘点和查询功能; 3. 销售管理:包括销售的录入、统计和查询功能; 4. 用户管理:包括用户的注册、登录、修改密码和退出登录功能; 5. 数据统计:包括商品销售量、库存量和销售额的统计功能。 以下是一个简单的Java代码实现: ``` import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class CampusGoodsManagementSystem { private static HashMap<Integer, Goods> goodsMap = new HashMap<>(); private static ArrayList<Inventory> inventoryList = new ArrayList<>(); private static ArrayList<Sale> saleList = new ArrayList<>(); private static ArrayList<User> userList = new ArrayList<>(); private static User currentUser = null; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean isExit = false; while (!isExit) { 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.退出系统"); System.out.print("请选择操作:"); int choice = scanner.nextInt(); switch (choice) { case 1: login(); break; case 2: register(); break; case 3: goodsManagement(); break; case 4: inventoryManagement(); break; case 5: saleManagement(); break; case 6: dataStatistics(); break; case 0: isExit = true; break; default: System.out.println("输入错误,请重新选择!"); break; } } System.out.println("谢谢使用校园商品管理系统!"); } private static void login() { Scanner scanner = new Scanner(System.in); System.out.print("请输入用户名:"); String username = scanner.nextLine(); System.out.print("请输入密码:"); String password = scanner.nextLine(); for (User user : userList) { if (user.getUsername().equals(username) && user.getPassword().equals(password)) { currentUser = user; System.out.println("登录成功!"); return; } } System.out.println("用户名或密码错误,登录失败!"); } private static void register() { Scanner scanner = new Scanner(System.in); System.out.print("请输入用户名:"); String username = scanner.nextLine(); System.out.print("请输入密码:"); String password = scanner.nextLine(); User user = new User(username, password); userList.add(user); System.out.println("注册成功!"); } private static void goodsManagement() { if (currentUser == null) { System.out.println("请先登录!"); return; } Scanner scanner = new Scanner(System.in); System.out.println("1.添加商品"); System.out.println("2.修改商品"); System.out.println("3.删除商品"); System.out.println("4.查询商品"); System.out.print("请选择操作:"); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("请输入商品编号:"); int id = scanner.nextInt(); System.out.print("请输入商品名称:"); String name = scanner.nextLine(); System.out.print("请输入商品价格:"); double price = scanner.nextDouble(); Goods goods = new Goods(id, name, price); goodsMap.put(id, goods); System.out.println("添加成功!"); break; case 2: System.out.print("请输入要修改的商品编号:"); int idToUpdate = scanner.nextInt(); if (!goodsMap.containsKey(idToUpdate)) { System.out.println("商品不存在!"); return; } Goods goodsToUpdate = goodsMap.get(idToUpdate); System.out.print("请输入新的商品名称(留空表示不修改):"); String newName = scanner.nextLine(); if (!newName.isEmpty()) { goodsToUpdate.setName(newName); } System.out.print("请输入新的商品价格(留空表示不修改):"); String newPriceStr = scanner.nextLine(); if (!newPriceStr.isEmpty()) { double newPrice = Double.parseDouble(newPriceStr); goodsToUpdate.setPrice(newPrice); } System.out.println("修改成功!"); break; case 3: System.out.print("请输入要删除的商品编号:"); int idToDelete = scanner.nextInt(); if (!goodsMap.containsKey(idToDelete)) { System.out.println("商品不存在!"); return; } goodsMap.remove(idToDelete); System.out.println("删除成功!"); break; case 4: System.out.print("请输入要查询的商品编号:"); int idToQuery = scanner.nextInt(); if (!goodsMap.containsKey(idToQuery)) { System.out.println("商品不存在!"); return; } Goods goodsToQuery = goodsMap.get(idToQuery); System.out.println("商品编号:" + goodsToQuery.getId()); System.out.println("商品名称:" + goodsToQuery.getName()); System.out.println("商品价格:" + goodsToQuery.getPrice()); break; default: System.out.println("输入错误,请重新选择!"); break; } } private static void inventoryManagement() { if (currentUser == null) { System.out.println("请先登录!"); return; } Scanner scanner = new Scanner(System.in); System.out.println("1.入库"); System.out.println("2.出库"); System.out.println("3.盘点"); System.out.println("4.查询库存"); System.out.print("请选择操作:"); int choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("请输入商品编号:"); int id = scanner.nextInt(); if (!goodsMap.containsKey(id)) { System.out.println("商品不存在!"); return; } System.out.print("请输入入库数量:"); int quantityIn = scanner.nextInt(); Inventory inventoryIn = new Inventory(id, quantityIn); inventoryList.add(inventoryIn); System.out.println("入库成功!"); break; case 2: System.out.print("请输入商品编号:"); int idOut = scanner.nextInt(); if (!goodsMap.containsKey(idOut)) { System.out.println("商品不存在!"); return; } System.out.print("请输入出库数量:"); int quantityOut = scanner.nextInt(); if (!isInventoryEnough(idOut, quantityOut)) { System.out.println("库存不足!"); return; } Inventory inventoryOut = new Inventory(idOut, -quantityOut); inventoryList.add(inventoryOut); System.out.println("出库成功!"); break; case 3: System.out.print("请输入商品编号:"); int idCheck = scanner.nextInt(); if (!goodsMap.containsKey(idCheck)) { System.out.println("商品不存在!"); return; } int quantityCheck = getInventoryQuantity(idCheck); System.out.println("商品编号:" + idCheck); System.out.println("库存数量:" + quantityCheck); break; case 4: System.out.println("商品编号\t库存数量"); for (Inventory inventory : inventoryList) { int idQuery = inventory.getGoodsId(); int quantityQuery = getInventoryQuantity(idQuery); System.out.println(idQuery + "\t" + quantityQuery); } break; default: System.out.println("输入错误,请重新选择!"); break; } } private static boolean isInventoryEnough(int goodsId, int quantity) { int currentQuantity = getInventoryQuantity(goodsId); return currentQuantity >= quantity; } private static int getInventoryQuantity(int goodsId) { int quantity = 0; for (Inventory inventory : inventoryList) { if (inventory.getGoodsId() == goodsId) { quantity += inventory.getQuantity(); } } return quantity; } private static void saleManagement() { if (currentUser == null) { System.out.println("请先登录!"); return; } Scanner scanner = new Scanner(System.in); System.out.print("请输入商品编号:"); int id = scanner.nextInt(); if (!goodsMap.containsKey(id)) { System.out.println("商品不存在!"); return; } System.out.print("请输入销售数量:"); int quantity = scanner.nextInt(); if (!isInventoryEnough(id, quantity)) { System.out.println("库存不足!"); return; } double price = goodsMap.get(id).getPrice(); double totalPrice = price * quantity; Sale sale = new Sale(id, quantity, totalPrice); saleList.add(sale); Inventory inventory = new Inventory(id, -quantity); inventoryList.add(inventory); System.out.println("销售成功!"); } private static void dataStatistics() { if (currentUser == null) { System.out.println("请先登录!"); return; } double totalSales = 0; for (Sale sale : saleList) { totalSales += sale.getTotalPrice(); } System.out.println("商品销售总额:" + totalSales); System.out.println("商品销售统计:"); System.out.println("商品编号\t销售数量\t销售额"); for (Sale sale : saleList) { int id = sale.getGoodsId(); int quantity = sale.getQuantity(); double totalPrice = sale.getTotalPrice(); System.out.println(id + "\t" + quantity + "\t" + totalPrice); } } private static class Goods { private int id; private String name; private double price; public Goods(int id, String name, double price) { 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; } } private static class Inventory { private int goodsId; private int quantity; public Inventory(int goodsId, int quantity) { this.goodsId = goodsId; this.quantity = quantity; } public int getGoodsId() { return goodsId; } public void setGoodsId(int goodsId) { this.goodsId = goodsId; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } } private static class Sale { private int goodsId; private int quantity; private double totalPrice; public Sale(int goodsId, int quantity, double totalPrice) { this.goodsId = goodsId; this.quantity = quantity; this.totalPrice = totalPrice; } public int getGoodsId() { return goodsId; } public void setGoodsId(int goodsId) { this.goodsId = goodsId; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public double getTotalPrice() { return totalPrice; } public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; } } private static class User { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } 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; } } } ``` 当然,这只是一个简单的实现,实际开发中需要考虑更多的细节和功能。

相关推荐

最新推荐

recommend-type

Java 实现简易教务管理系统的代码

主要介绍了Java 实现简易教务管理系统的代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

基于Spring Boot的校园食堂订餐系统的设计与实现.doc

本文在考虑到以上的问题的基础上,利用大学期间中所学到的的专业知识,独立开发一个基于Spring Boot和vue.js的校园食堂订餐系统。论文首先进行了系统功能的总体设计,使本系统具有以下主要功能:一是具有手机端让...
recommend-type

基于javaweb的论坛管理系统 JavaWeb设计

用户在本校园论坛系统没有登陆的时候(即游客)可以对本系统网站进行访问,并且具有以下几种基本操作功能: 查看帖子,访问者可以在没有登录的情况下访问版块的帖子,进入帖子查看帖子的具体发表信息。还可以查看...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性

![MATLAB结构体与对象编程:构建面向对象的应用程序,提升代码可维护性和可扩展性](https://picx.zhimg.com/80/v2-8132d9acfebe1c248865e24dc5445720_1440w.webp?source=1def8aca) # 1. MATLAB结构体基础** MATLAB结构体是一种数据结构,用于存储和组织相关数据。它由一系列域组成,每个域都有一个名称和一个值。结构体提供了对数据的灵活访问和管理,使其成为组织和处理复杂数据集的理想选择。 MATLAB中创建结构体非常简单,使用struct函数即可。例如: ```matlab myStruct
recommend-type

详细描述一下STM32F103C8T6怎么与DHT11连接

STM32F103C8T6可以通过单总线协议与DHT11连接。连接步骤如下: 1. 将DHT11的VCC引脚连接到STM32F103C8T6的5V电源引脚; 2. 将DHT11的GND引脚连接到STM32F103C8T6的GND引脚; 3. 将DHT11的DATA引脚连接到STM32F103C8T6的GPIO引脚,可以选择任一GPIO引脚,需要在程序中配置; 4. 在程序中初始化GPIO引脚,将其设为输出模式,并输出高电平,持续至少18ms,以激活DHT11; 5. 将GPIO引脚设为输入模式,等待DHT11响应,DHT11会先输出一个80us的低电平,然后输出一个80us的高电平,
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB结构体与数据库交互:无缝连接数据存储与处理,实现数据管理自动化

![MATLAB结构体与数据库交互:无缝连接数据存储与处理,实现数据管理自动化](https://ww2.mathworks.cn/products/database/_jcr_content/mainParsys/band_1749659463_copy/mainParsys/columns_copy/6d5289a2-72ce-42a8-a475-d130cbebee2e/image_copy_2009912310.adapt.full.medium.jpg/1709291769739.jpg) # 1. MATLAB结构体与数据库交互概述** MATLAB结构体与数据库交互是一种强大的