如今即将上大二的我,回头做了一下大一上半学期的宠物商店的一个小项目,那时候刚接触java就被要求做个这东西出来,当时做了真的特别久。现在做起来觉得自己进步不少,挺简单的。废话不多说,直接上代码,嘿嘿。
整个程序主要用到ArrayList集合,对ArrayList进行增删查改。
1.先写一个属性类package com.animalShop; public class property { private String name; private String kind; private int age; private double price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public property(String name, String kind, int age, double price) { this.name = name; this.kind = kind; this.age = age; this.price = price; } @Override public String toString() { return name + "t" + kind + "t" + age + "t" + price; } }
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 2.再写一个商店类package com.animalShop; import java.util.ArrayList; import java.util.Scanner; public class shop { //商店品种集合 ArrayList<property> amlList = new ArrayList<>(); //购物车集合 ArrayList<property> stoCarList = new ArrayList<>(); //添加宠物到商店的方法 public void addAml() { System.out.println("请输入添加的宠物信息(名字->种类->年龄->价格):"); //创建对象animal属性,键入animal property pty = new property(getString(), getString(), getInt(), getDouble()); //判断是否添加到amlList集合里面 boolean bool = amlList.add(pty); if (bool == true) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } } //显示宠物商店宠物方法 public void showAml() { boolean bool = amlList.isEmpty(); if (bool != true) { System.out.println("序号" + "t" + "名字" + "t" + "种类" + "t" + "年龄" + "t" + "价格"); //定义一个序号 int n = 1; //遍历出宠物商店宠物 for (property property : amlList) { System.out.println(n + "t" + property); n++; } System.out.println(" "); } else { System.out.println("商店维护中,暂时没有商品哦!"); System.out.println(" "); } } //购买宠物到购物车的方法 public void buyAml() { boolean bool = amlList.isEmpty(); if (bool != true) { System.out.print("请输入您想要购买的宠物的序号:"); //键入购买宠物的序号 int n = getInt(); //防止下标越界 if (n <= amlList.size()) { stoCarList.add(amlList.get((n - 1))); amlList.remove((n - 1)); System.out.println("购买成功!"); System.out.println(" "); } else { System.out.println("购买失败!"); System.out.println("您想要的宠物不存在,请核实商店现有的宠物!"); System.out.println(" "); } } else { System.out.println("商店维护中,暂时没有商品可以购买哦!"); System.out.println(" "); } } //查看购物车方法 public void showShopCar() { boolean bool = stoCarList.isEmpty(); if (bool != true) { System.out.println("-------购物车-------"); System.out.println("序号" + "t" + "名字" + "t" + "种类" + "t" + "年龄" + "t" + "价格"); shopList(); System.out.println(" "); } else { System.out.println("您还没有购买过宠物呢!"); System.out.println(" "); } } //购物车结算方法 public void totalPrice() { boolean bool =stoCarList.isEmpty(); if (bool != true) { System.out.println("-------清单-------"); System.out.println("序号" + "名字" + "t" + "种类" + "t" + "年龄" + "t" + "价格" + "t"); shopList(); //清空购物车 stoCarList.clear(); //判断购物车是否为空 if (stoCarList.isEmpty()) { System.out.println("结算成功!"); System.out.println(" "); } else { System.out.println("结算失败!"); System.out.println(" "); } }else { System.out.println("购物车没有任何宠物哦,请先购买再结算!"); System.out.println(" "); } } //删除购物车某一个宠物方法 public void delStoreCar() { boolean bool = stoCarList.isEmpty(); if (bool != true) { System.out.print("请输入购物车中要删除的宠物序号:"); int n = getInt(); //防止下标越界 if ((n - 1) <= stoCarList.size()) { stoCarList.remove((n - 1)); System.out.println("删除成功!"); System.out.println(" "); } else { System.out.println("删除失败!"); System.out.println("请检查购物车宠物是否存在!"); System.out.println(" "); } } else { System.out.println("您还没有购买过宠物,无需删除!"); System.out.println(" "); } } //删除商店某一个宠物 public void delAmlList() { boolean bool = amlList.isEmpty(); if (bool != true) { System.out.print("请输入商店中要删除的宠物序号:"); int n = getInt(); //防止下标越界 if ((n - 1) <= amlList.size()) { amlList.remove((n - 1)); System.out.println("删除成功!"); System.out.println(" "); } else { System.out.println("删除失败!"); System.out.println("请检查商店宠物是否存在!"); System.out.println(" "); } } else { System.out.println("商城还未上架过任何宠物!"); System.out.println(" "); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); shop shop = new shop(); while (true) { System.out.println("--------欢" + "t" + "迎" + "t" + "来" + "t" + "到" + "t" + "上" + "t" + "天" + "t" + "的" + "t" + "小" + "t" + "宝" + "t" + "贝" + "t" + "儿" + "t" + "宠" + "t" + "物" + "t" + "商" + "t" + "店--------"); System.out.println("请输入序号(1:上架宠物 2:下架宠物 3:打开商城 4:购买 5:打开购物车 6:删除购物车宠物 7:结算 8:离开)"); switch (sc.nextInt()) { case 1: shop.addAml(); break; case 2: shop.delAmlList(); break; case 3: shop.showAml(); break; case 4: shop.buyAml(); break; case 5: shop.showShopCar(); break; case 6: shop.delStoreCar(); break; case 7: shop.totalPrice(); break; case 8: System.out.println("再见!"); return; } } } //封装购物清单方法,提高代码的复用性 public void shopList() { double total = 0; //序号计数器 int n = 1; //遍历购物车 for (int i = 0; i < stoCarList.size(); i++) { System.out.println(n + "t" + stoCarList.get(i)); property price = stoCarList.get(i); total += price.getPrice(); n++; } System.out.println("总价:" + "t" + "t" + "t" + total); } /* * 这里没有创建一个对象键入,是因为创建一个对象,在键入数据时,会因为对象数据类型冲突,导致InputMismatchException异常 * */ public static String getString() { Scanner a = new Scanner(System.in); return a.next(); } public static int getInt() { Scanner b = new Scanner(System.in); return b.nextInt(); } public static double getDouble() { Scanner c = new Scanner(System.in); return c.nextDouble(); } }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221相关知识
java宠物商店代码
java宠物作业 java实训项目宠物商店
Java实现简单的宠物商店管理系统
java宠物商店项目
基于java的宠物商城开题 java实训项目宠物商店
宠物商店中商店购买主人的方法怎样实现
mysql 项目案例宠物商店
基于java+springboot的宠物商店、宠物管理系统设计与实现
java 宠物销售网站 宠物销售系统 宠物商店管理系统 宠物店 jsp
java基于springboot+vue的宠物商店领养挂失管理系统 element 前后端分离
网址: Java做一个简单的宠物商店,实现添加,购买,结算等功能 https://m.mcbbbk.com/newsview707059.html
上一篇: 昆明哪里可以买宠物猫狗,这里可实 |
下一篇: 宠物店商城小程序设计与实现(30 |