前言
大家好,我是ChinaManor,直译过来就是中国码农的意思,我希望自己能成为国家复兴道路的铺路人,大数据领域的耕耘者,平凡但不甘于平庸的人。
下面是本码农在学习Java基础
做的练习题,发出来希望给各位萌新学java有所帮助
需要用到的Jar包
链接:https://pan.baidu.com/s/1Y4CT_ePPKc2lDdh_ylMCew
提取码:didj
手机商城
题干:
根据指定要求,完成手机商城以下功能:
登录、商品列表查询、商品详情、展示购物车商品、访问足迹、添加到购物车、立即购买
一、 登录
题干:
完成【登录】功能:
用户输入用户名和密码完成登录功能
注:用户名和密码在初始化数据中(init方法)已给出
要求:
用户输入用户名、密码正确,提示亲爱的xxx,您已登录成功,请开心购物!!!
用户输入用户名、密码错误,提示登录失败,请重新登录!!!
用户是否为登录状态,都可以直接访问以下功能:
a. 商品列表查询、商品详情、展示购物车商品、访问足迹、添加到购物车
注:用户只有在登录状态下,才可以访问 付款功能 !!!
二、 商品列表查询
题干:
完成【商品列表查询】功能:
要求:
效果图如下图所示:
三、 商品详情
题干:
完成【商品详情】功能:
要求:
效果图如下图所示:
四、 添加到购物车
题干:
完成【添加到购物车】功能:
要求:
输入错误的货物编号、商品数量,提示:添加失败
输入正确的货物编号、商品数量,提示:添加到购物车成功!
五、 立即购买
题干:
完成【立即购买】功能:
要求:
未登录状态下,点击付款功能,提示:您还没有登录,请登录...
输入正确的货物编号、购买数量、显示购买清单
付款成功后,在此查询对应的货物列表,其库存数量减少(现有库存数量=原有库存数量-购买数量),如下图3
付款成功后,把购买记录(购买人、购买货物名称、购买数量、购买商品总金额)写入orders.txt文件中,如下图4
a. 若orders.txt文件不存在,创建该文件
b. 若存储多个购买记录,换行存储(一行一条购买记录)
六、 展示购物车商品
题干:
完成【展示购物车商品】功能:
要求:
当购物车中没有商品时,提示:购物车中暂时还没有商品哟,请前往商品首页购物!!!
当购物车中不为空时,显示购物列表
七、 访问足迹
题干:
完成【访问足迹】功能:
要求:
在没有访问商品时,提示:您还没有访问任何商品哟!
在访问商品详情后,访问足迹显示访问商品列表,如图2 图3
参考代码:
Shop.java
package 每日作业17; import org.apache.commons.io.FileUtils; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by dingw on 2019/11/17. */ public class Shop { //品牌集合 static List<Brand> brandList = new ArrayList<>(); //用户 static ArrayList<User> users = new ArrayList<>(); //购物车 static ArrayList<Item> carts = new ArrayList<>(); //访问足迹 static List<Goods> goodsList = new ArrayList<>(); //判断登录的标识 static boolean flag = false; //登录人 static String loginname = ""; public static void main(String[] args) throws Exception { init(); System.out.println("=====================================大数据手机商城欢迎您========================================"); one: while (true) { 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.print("请选择[1-6]:"); System.out.println("请输入你的操作"); Scanner sc = new Scanner(System.in); String x = sc.nextLine(); switch (x) { case "1": login(); break; case "2": findAll(); break; case "3": findProductById(); two: while (true) { System.out.println("***************************************************************************"); System.out.println("【1】 添加到购物车"); System.out.println("【2】 立即购买"); System.out.println("【3】 退出"); System.out.print("请选择[1-3]:"); System.out.println("请输入你的操作"); Scanner sc1 = new Scanner(System.in); String xs = sc1.nextLine(); switch (xs) { case "1": addCart(); break; case "2": pay(); break; default: break two; } } break; case "4": showCart(); break; case "5": historyVisit(); break; default: break one; } } } /** * 展示购物车 */ public static void showCart() { System.out.println("===================================购物车商品如下============================================"); if (carts.size() == 0) { System.out.println("购物车中暂时还没有商品哟,请前往商品首页购物!!!"); } else { System.out.println("货物编号\t\t 货物名称(商品名+颜色)\t\t\t价格\t\t\t\t\t颜色\t\t购买数量"); for (Item item : carts) { System.out.println(item.getId() + "\t\t\t\t" + item.getName() + "\t\t\t\t" + item.getPrice() + "\t\t\t\t" + item.getColor() + "\t\t" + item.getNum()); } } } /** * 结账 */ private static void pay() throws Exception { if (flag) { System.out.println("请输入货物编号:"); Scanner sc = new Scanner(System.in); String num = sc.nextLine(); System.out.println("请输入购买数量:"); String kc = sc.nextLine(); boolean ss = false; for (Brand brand : brandList) { for (Goods goods : brand.getGoodsList()) { for (Item item : goods.getItemList()) { if (item.getId().equals(num)) { if (item.getNum() < Integer.parseInt(kc)) { flag = true; } else { System.out.println("货物编号\t\t 货物名称(商品名+颜色)\t\t\t价格\t\t\t\t\t颜色\t\t购买数量"); System.out.println(item.getId() + "\t\t\t\t" + item.getName() + "\t\t\t\t" + item.getPrice() + "\t\t\t\t" + item.getColor() + "\t\t" + kc); System.out.println("======================================================================="); double total = (Math.round(item.getPrice() * 100 * Integer.parseInt(kc))) / 100.0; System.out.println("总计付款:" + total); item.setNum(item.getNum() - Integer.parseInt(kc)); FileUtils.write(new File("orders.txt"), loginname + ":" + item.getName() + "," + kc + "," + total + "\r\n", true); return; } } } } } System.out.println("货物编号不存在,请重新输入!!!"); if (ss) { System.out.println("库存不足,请重新购买!"); } } else { System.out.println("您还没有登录,请登录....."); login(); } } /** * 添加到购物车 */ private static void addCart() { System.out.println("请输入货物编号:"); Scanner sc = new Scanner(System.in); String num = sc.nextLine(); System.out.println("请输入商品数量:"); String kc = sc.nextLine(); for (Brand brand : brandList) { for (Goods goods : brand.getGoodsList()) { for (Item item : goods.getItemList()) { if(carts.contains(item)){ for (Item cart : carts) { if (cart.getName().equals(item.getName())&&(cart.getNum()+Integer.parseInt(kc))<=item.getNum()){ cart.setNum(cart.getNum()+Integer.parseInt(kc)); System.out.println("添加到购物车成功!"); return; } } } if (item.getId().equals(num) && Integer.parseInt(kc) <= item.getNum()) { carts.add(new Item(item.getId(), item.getName(), item.getColor(), item.getPrice(), Integer.parseInt(kc))); System.out.println("添加到购物车成功!"); return; } } } } System.out.println("添加失败"); } /** * 访问足迹 */ private static void historyVisit() { if (goodsList.size() == 0) { System.out.println("您还没有访问任何商品哟!"); } else { System.out.println("商品编号\t\t 商品名称\t\t\t\t商品介绍"); for (Goods goods : goodsList) { System.out.printf(goods.getGid() + "\t\t\t " + goods.getGname() + "\t\t\t" + goods.getDesc()); System.out.println(); } } } /** * 商品详情 */ private static void findProductById() { findAll(); System.out.println("============================================商品详情========================================"); System.out.println("请输入您要查询的商品编号:"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); System.out.println("货物编号\t\t 货物名称(商品名+颜色)\t\t\t价格\t\t\t\t\t颜色\t\t库存数"); for (Brand brand : brandList) { for (Goods goods : brand.getGoodsList()) { if (goods.getGid() == num) { if (!goodsList.contains(goods)){ goodsList.add(goods); } for (Item item : goods.getItemList()) { System.out.println(item.getId() + "\t\t\t\t" + item.getName() + "\t\t\t\t" + item.getPrice() + "\t\t\t\t" + item.getColor() + "\t\t" + item.getNum()); } } } } } /** * 查询所有:商品列表查询 */ public static void findAll() { System.out.println("=========================================商品列表查询======================================"); System.out.println("商品编号\t\t 商品名称\t\t\t\t商品介绍"); for (Brand brand : brandList) { for (Goods goods : brand.getGoodsList()) { System.out.printf(goods.getGid() + "\t\t\t " + goods.getGname() + "\t\t\t" + goods.getDesc()); System.out.println(); } } } /** * 登录 */ public static void login() { System.out.println("=====================================欢迎使用登录功能======================================="); Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String username = sc.nextLine(); System.out.println("请输入密码: "); String password = sc.nextLine(); for (User user : users) { if (user.getUsername().equals(username) && user.getPassword().equals(password)) { System.out.println("亲爱的" + username + ",您已登录成功,请开心购物!!!"); flag = true; loginname = username; return; } } System.out.println("登录失败,请重新登录!!!"); } /** * 初始化数据 */ public static void init() { users.add(new User(1, "tom", "123")); users.add(new User(2, "jack", "456")); //货物集合 List<Item> hwitemList = new ArrayList<>(); List<Item> hwproitemList = new ArrayList<>(); List<Item> mzitemList = new ArrayList<>(); List<Item> mzPitemList = new ArrayList<>(); List<Item> xmitemList = new ArrayList<>(); hwitemList.add(new Item("1_1", "华为P20极光色", "极光色", 3788, 10)); hwitemList.add(new Item("1_2", "华为P20宝石蓝", "宝石蓝", 3788, 10)); hwproitemList.add(new Item("2_1", "华为P20 Pro极光色", "极光色", 4988, 10)); hwproitemList.add(new Item("2_2", "华为P20 Pro宝石蓝", "宝石蓝", 4988, 10)); mzitemList.add(new Item("3_1", "魅族16 th远山白", "远山白", 2698, 10)); mzPitemList.add(new Item("4_1", "魅族16 th plus远山白", "远山白", 3198, 1)); xmitemList.add(new Item("5_1", "小米8黑色", "黑色", 2699, 0)); //商品集合 List<Goods> hwgoodsList = new ArrayList<>(); List<Goods> mzgoodsList = new ArrayList<>(); List<Goods> xmgoodsList = new ArrayList<>(); hwgoodsList.add(new Goods(1, "华为P20", "华为P20,你值得拥有", hwitemList)); hwgoodsList.add(new Goods(2, "华为P20 Pro", "华为P20 Pro,你值得拥有", hwproitemList)); mzgoodsList.add(new Goods(3, "魅族16 th", "高通骁龙845,屏下指纹,AI双摄光学防抖", mzitemList)); mzgoodsList.add(new Goods(4, "魅族16 th plus", "高通骁龙845,屏下指纹,AI双摄光学防抖", mzPitemList)); xmgoodsList.add(new Goods(5, "小米8", "骁龙845处理器 / 红外人脸解锁 /AI变焦双摄", xmitemList)); brandList.add(new Brand(1, "华为", hwgoodsList)); brandList.add(new Brand(2, "魅族", mzgoodsList)); brandList.add(new Brand(3, "小米", xmgoodsList)); } }
Item.java
package 每日作业17; /** * Created by dingw on 2019/11/17. */ /** * 货物类:表示具体的手机 */ public class Item { //货物id private String id; //货物名称(商品名+手机颜色) private String name; //手机颜色 private String color; //售价 private double price; //库存数 private int num; public Item() { } public Item(String id, String name, String color, double price, int num) { this.id = id; this.name = name; this.color = color; this.price = price; this.num = num; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Item item = (Item) o; if (Double.compare(item.price, price) != 0) return false; if (num != item.num) return false; if (id != null ? !id.equals(item.id) : item.id != null) return false; if (name != null ? !name.equals(item.name) : item.name != null) return false; return color != null ? color.equals(item.color) : item.color == null; } @Override public int hashCode() { int result; long temp; result = id != null ? id.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); result = 31 * result + (color != null ? color.hashCode() : 0); temp = Double.doubleToLongBits(price); result = 31 * result + (int) (temp ^ (temp >>> 32)); result = 31 * result + num; return result; } }
Goods.java
package 每日作业17; import java.util.ArrayList; import java.util.List; /** * Created by dingw on 2019/11/17. */ public class Goods { private int gid; private String gname; private String desc; private List<Item> itemList = new ArrayList<>(); public Goods() { } public Goods(int gid, String gname, String desc, List<Item> itemList) { this.gid = gid; this.gname = gname; this.desc = desc; this.itemList = itemList; } public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public List<Item> getItemList() { return itemList; } public void setItemList(List<Item> itemList) { this.itemList = itemList; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Goods goods = (Goods) o; if (gid != goods.gid) return false; if (gname != null ? !gname.equals(goods.gname) : goods.gname != null) return false; if (desc != null ? !desc.equals(goods.desc) : goods.desc != null) return false; return itemList != null ? itemList.equals(goods.itemList) : goods.itemList == null; } @Override public int hashCode() { int result = gid; result = 31 * result + (gname != null ? gname.hashCode() : 0); result = 31 * result + (desc != null ? desc.hashCode() : 0); result = 31 * result + (itemList != null ? itemList.hashCode() : 0); return result; } }
Brand.java
package 每日作业17; import java.util.ArrayList; import java.util.List; /** * Created by dingw on 2019/11/17. */ public class Brand { private int bid; private String bname; private List<Goods> goodsList = new ArrayList<>(); public Brand() { } public Brand(int bid, String bname, List<Goods> goodsList) { this.bid = bid; this.bname = bname; this.goodsList = goodsList; } public int getBid() { return bid; } public void setBid(int bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public List<Goods> getGoodsList() { return goodsList; } public void setGoodsList(List<Goods> goodsList) { this.goodsList = goodsList; } }
User.java
package 每日作业17; /** * Created by dingw on 2019/11/17. */ /** * 用户 */ public class User { //用户id private int uid; //用户名 private String username; //密码 private String password; public User() { } public User(int uid, String username, String password) { this.uid = uid; this.username = username; this.password = password; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } 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; } }
总结
以上便是JavaSE综合练习之大数据手机商城 ,喜欢的小伙伴欢迎一键三连
!!!
我是manor,一枚相信技术改变世界的码农,我们下期再见~