当前位置:   article > 正文

一个简单的Java超市管理系统_定义java对象类,超市

定义java对象类,超市

一、两个类一个是商品实体类,一个是业务管理类(功能类)

1、实体类

  1. public class Goods {
  2. private String Name;
  3. private String State; //促销状态
  4. private double listPrice; //原价
  5. private double promoPrice; //促销价
  6. private int Inventory; //库存
  7. private int SV; //销量
  8. //无参构造
  9. public Goods() {
  10. }
  11. //全参构造
  12. public Goods(String name, String state, double listPrice, double promoPrice, int inventory, int SV) {
  13. Name = name;
  14. State = state;
  15. this.listPrice = listPrice;
  16. this.promoPrice = promoPrice;
  17. Inventory = inventory;
  18. this.SV = SV;
  19. }
  20. public String getName() {
  21. return Name;
  22. }
  23. public void setName(String name) {
  24. Name = name;
  25. }
  26. public String getState() {
  27. return State;
  28. }
  29. public void setState(String state) {
  30. State = state;
  31. }
  32. public double getListPrice() {
  33. return listPrice;
  34. }
  35. public void setListPrice(double listPrice) {
  36. this.listPrice = listPrice;
  37. }
  38. public double getPromoPrice() {
  39. return promoPrice;
  40. }
  41. public void setPromoPrice(double promoPrice) {
  42. this.promoPrice = promoPrice;
  43. }
  44. public int getInventory() {
  45. return Inventory;
  46. }
  47. public void setInventory(int inventory) {
  48. Inventory = inventory;
  49. }
  50. public int getSV() {
  51. return SV;
  52. }
  53. public void setSV(int SV) {
  54. this.SV = SV;
  55. }
  56. @Override
  57. public String toString() {
  58. return "Goods{" +
  59. "Name='" + Name + '\'' +
  60. ", State='" + State + '\'' +
  61. ", listPrice='" + listPrice + '\'' +
  62. ", promoPrice='" + promoPrice + '\'' +
  63. ", Inventory=" + Inventory +
  64. ", SV=" + SV +
  65. '}';
  66. }
  67. }

2、业务管理类

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class GoodsManager {
  4. //全局静态变量
  5. static Goods[] goods; //商品数组
  6. static Scanner scanner = new Scanner(System.in);
  7. //静态代码块:初始化商品数据
  8. static {
  9. goods = new Goods[4];
  10. goods[0]=new Goods("苹果","促销",5,2,20,10);
  11. goods[1]=new Goods("方便面","未促销",4,3.5,30,20);
  12. goods[2]=new Goods("矿泉水","促销",2,1.5,40,30);
  13. goods[3]=new Goods("火腿肠","未促销",1.5,1,50,40);
  14. }
  15. //展示商品列表
  16. static void disPlay(){
  17. System.out.println("-------商品列表-------");
  18. for (int i = 0; i < goods.length; i++) {
  19. if(goods[i]!=null){
  20. System.out.println(goods[i].toString());
  21. }
  22. }
  23. }
  24. //添加商品
  25. static void addGoods(){
  26. System.out.println("********添加商品********");
  27. //添加前的扩容工作
  28. if(goods[goods.length-1]!=null){ //判断商品数组最后一个位置是否为空,不为空则满,需要扩容
  29. goods = Arrays.copyOf(goods,goods.length+5); //扩容
  30. }
  31. //创建一个新的商品对象
  32. Goods good = new Goods();
  33. //录入商品信息
  34. System.out.println("输入商品名称:");
  35. good.setName(scanner.next());
  36. System.out.println("输入商品状态:");
  37. good.setState(scanner.next());
  38. System.out.println("输入商品原价:");
  39. good.setListPrice(scanner.nextDouble());
  40. System.out.println("输入商品促销价:");
  41. good.setPromoPrice(scanner.nextDouble());
  42. System.out.println("输入商品库存:");
  43. good.setInventory(scanner.nextInt());
  44. System.out.println("输入商品销量:");
  45. good.setSV(scanner.nextInt());
  46. //遍历商品数组寻找空位置插入商品
  47. for (int i = 0; i < goods.length; i++) {
  48. if(goods[i]==null){
  49. goods[i]=good;
  50. break;
  51. }
  52. }
  53. System.out.println("商品添加成功!");
  54. }
  55. //删除商品
  56. static void removeGoods(){
  57. System.out.println("*********删除商品*********");
  58. disPlay(); //展示商品列表
  59. //接收待删除商品的名称
  60. System.out.println("请输入待删除的商品名称");
  61. String name = scanner.next();
  62. //定义存放待删除商品的下标
  63. int removeIndex = -1;
  64. //先在数组中寻找这个商品
  65. for (int i = 0; i < goods.length; i++) {
  66. if(goods[i].getName().equals(name)){ //找到这个商品
  67. removeIndex = i;
  68. break;
  69. }
  70. }
  71. if(removeIndex == -1 ){ //没找到这个商品
  72. System.out.println("该商品不存在!");
  73. return;
  74. }else { //商品存在,开始执行删除操作,并且将其之后的元素前移一位
  75. if(removeIndex == goods.length - 1 ){ //判断待删除元素是不是数组的最后一位
  76. goods[goods.length-1] = null ; //是的话直接置空即可
  77. }else{ //不在数组的最后一位,执行前移操作
  78. for (int i = removeIndex; i < goods.length-1; i++) {
  79. if(goods[i]==null){ //前移到为空为止
  80. break;
  81. }
  82. goods[i]=goods[i+1]; //前移操作
  83. }
  84. goods[goods.length-1]=null; //当数组为满情况删除非最后一位元素时,后移完需要将最后一个元素置空,否则会重复
  85. }
  86. }
  87. System.out.println("删除成功!");
  88. disPlay();
  89. }
  90. //购买商品
  91. static void buyGoods(){
  92. System.out.println("********购买商品*********");
  93. disPlay();
  94. double rental = 0.0; //应付款金额
  95. //接收购买的商品名
  96. System.out.println("输入购买的商品的名称:");
  97. String name = scanner.next();
  98. boolean exit = false; //判断是否找到的标志
  99. //接收购买的数量
  100. System.out.println("请输入你需要购买该商品的数量:");
  101. int buyStock = scanner.nextInt();
  102. //判断数量是否合法
  103. while(buyStock<0){
  104. System.out.println("购买数量不合法,请从新输入一个大于0的整数:");
  105. buyStock = scanner.nextInt();
  106. }
  107. //判断商品是否存在
  108. for (int i = 0; i < goods.length; i++) {
  109. if(goods[i]==null){
  110. break;
  111. }
  112. if(goods[i].getName().equals(name)){ //找到该商品
  113. exit = true;
  114. //判断库存是否足够
  115. while(goods[i].getInventory() < buyStock){
  116. System.out.println("购买数量超过库存!请重新购买");
  117. buyStock = scanner.nextInt();
  118. while(buyStock<0){
  119. System.out.println("购买数量不合法,请从新输入一个大于0的整数:");
  120. buyStock = scanner.nextInt();
  121. }
  122. }
  123. goods[i].setSV(goods[i].getSV()+buyStock);
  124. goods[i].setInventory(goods[i].getInventory() - buyStock);
  125. disPlay();
  126. //打印小票
  127. if(goods[i].getState().equals("促销")){
  128. rental = buyStock * goods[i].getPromoPrice(); //促销价
  129. System.out.println("您购买了"+buyStock+"个"+name+"促销单价为:"+goods[i].getPromoPrice()+"需要支付:"+rental);
  130. }
  131. if(goods[i].getState().equals("未促销")){
  132. rental = buyStock * goods[i].getListPrice(); //原价
  133. System.out.println("您购买了"+buyStock+"个"+name+"原价为:"+goods[i].getListPrice()+"需要支付:"+rental);
  134. }
  135. System.out.println("请输入您的支付金额");
  136. double pay = scanner.nextDouble();
  137. while (pay<rental){
  138. System.out.println("您支付的金额小于总租金,请重新输入");
  139. pay = scanner.nextDouble();
  140. }
  141. System.out.println("********购物小票********");
  142. System.out.println("购买商品:" + name);
  143. if(goods[i].getState().equals("促销")){
  144. System.out.println("购买商品单价为:" + goods[i].getPromoPrice());
  145. }else
  146. {
  147. System.out.println("购买商品单价为:" + goods[i].getListPrice());
  148. }
  149. System.out.println("购买数量为:"+buyStock);
  150. System.out.println("需支付:" + rental);
  151. System.out.println("实际支付"+ pay);
  152. System.out.println("找零:" + (pay-rental));
  153. System.out.println("**********欢迎下次光临*********");
  154. }
  155. }
  156. if(!exit){
  157. System.out.println("没找到该商品!");
  158. }
  159. }
  160. //修改商品促销状态
  161. static void updateState(){
  162. System.out.println("********修改商品的促销状态*********");
  163. disPlay();
  164. //接收待修改商品名
  165. System.out.println("输入需要修改商品的名称:");
  166. String name = scanner.next();
  167. boolean b2 = false; //判断是否找到的标志
  168. //接收新的状态
  169. System.out.println("请输入该商品的状态(促销 & 未促销):");
  170. String state = scanner.next();
  171. //判断状态是否合法
  172. boolean s1 = true;
  173. while(s1){
  174. if(state.equals("促销") || state.equals("未促销")){
  175. break;
  176. }
  177. System.out.println("状态不合法,请从新输入:");
  178. state = scanner.next();
  179. }
  180. //判断是否存在
  181. for (int i = 0; i < goods.length; i++) {
  182. if(goods[i]==null){
  183. break;
  184. }
  185. if(goods[i].getName().equals(name)){ //找到该元素并进行修改
  186. b2 = true;
  187. goods[i].setState(state);
  188. System.out.println("修改成功!");
  189. disPlay();
  190. }
  191. }
  192. if(!b2){
  193. System.out.println("没找到该商品!");
  194. }
  195. }
  196. //补充库存函数
  197. static void restock(){
  198. System.out.println("********补充商品的库存*********");
  199. disPlay();
  200. //接收待补充商品名
  201. System.out.println("输入需要补充库存的商品的名称:");
  202. String name = scanner.next();
  203. boolean b2 = false; //判断是否找到的标志
  204. //接收需要增加的量
  205. System.out.println("请输入你需要补充该商品的数量:");
  206. int addStock = scanner.nextInt();
  207. //判断数量是否合法
  208. boolean s1 = true;
  209. while(s1){
  210. if(addStock>=0){
  211. break;
  212. }
  213. System.out.println("数量不合法,请从新输入:");
  214. addStock = scanner.nextInt();
  215. }
  216. //判断是否存在
  217. for (int i = 0; i < goods.length; i++) {
  218. if(goods[i]==null){
  219. break;
  220. }
  221. if(goods[i].getName().equals(name)){ //找到该元素并进行修改
  222. b2 = true;
  223. goods[i].setInventory(goods[i].getInventory()+addStock);
  224. System.out.println("增加成功!");
  225. disPlay();
  226. }
  227. }
  228. if(!b2){
  229. System.out.println("没找到该商品!");
  230. }
  231. }
  232. //打印菜单函数
  233. static void printMenu(){
  234. boolean exit = false;
  235. do{
  236. System.out.println("************欢迎进入超市***********");
  237. System.out.println("1、查看商品列表");
  238. System.out.println("2、添加商品");
  239. System.out.println("3、删除商品");
  240. System.out.println("4、购买商品");
  241. System.out.println("5、修改商品促销状态");
  242. System.out.println("6、增加商品库存");
  243. System.out.println("7、退出超市");
  244. System.out.println("/请选择菜单编号进行选择/:");
  245. int c = scanner.nextInt();
  246. switch (c){
  247. case 1 :
  248. disPlay();
  249. break;
  250. case 2 :
  251. addGoods();
  252. break;
  253. case 3 :
  254. removeGoods();
  255. break;
  256. case 4 :
  257. buyGoods();
  258. break;
  259. case 5 :
  260. updateState();
  261. break;
  262. case 6 :
  263. restock();
  264. break;
  265. case 7 :
  266. exit = true ;
  267. break;
  268. default:
  269. System.out.println("输入的菜单编号错误!请重新输入");
  270. break;
  271. }
  272. }while (!exit);
  273. System.out.println("!Bye!");
  274. }
  275. public static void main(String[] args) {
  276. //removeGoods();
  277. //updateState();
  278. printMenu();
  279. }
  280. }

3、运行业务类结果

具体功能不在演示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/894242
推荐阅读
相关标签
  

闽ICP备14008679号