From c21520d8eec76978dbb3bc6a39c3c3ebab2fcd81 Mon Sep 17 00:00:00 2001 From: seahi Date: Thu, 21 Nov 2024 09:35:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=88=E4=BE=8B6-1=20=E5=BA=93=E5=AD=98?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chapter6/Example04.java | 4 ++ src/chapter6/Example05.java | 4 ++ src/chapter6/Example3.java | 4 ++ src/chapter6/demo61/Goods.java | 44 +++++++++++++++++ src/chapter6/demo61/Manager.java | 81 ++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 src/chapter6/Example04.java create mode 100644 src/chapter6/Example05.java create mode 100644 src/chapter6/Example3.java create mode 100644 src/chapter6/demo61/Goods.java create mode 100644 src/chapter6/demo61/Manager.java diff --git a/src/chapter6/Example04.java b/src/chapter6/Example04.java new file mode 100644 index 0000000..c722543 --- /dev/null +++ b/src/chapter6/Example04.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example04 { +} diff --git a/src/chapter6/Example05.java b/src/chapter6/Example05.java new file mode 100644 index 0000000..94b9dae --- /dev/null +++ b/src/chapter6/Example05.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example05 { +} diff --git a/src/chapter6/Example3.java b/src/chapter6/Example3.java new file mode 100644 index 0000000..0072256 --- /dev/null +++ b/src/chapter6/Example3.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example3 { +} diff --git a/src/chapter6/demo61/Goods.java b/src/chapter6/demo61/Goods.java new file mode 100644 index 0000000..2aae665 --- /dev/null +++ b/src/chapter6/demo61/Goods.java @@ -0,0 +1,44 @@ +package chapter6.demo61; + +/** + * 商品类 + */ +public class Goods { + private String name; + private double price; + private int num; + + public Goods(String name, double price, int num) { + this.name = name; + this.price = price; + this.num = num; + } + + 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; + } + + public int getNum() { + return num; + } + + public void setNum(int num) { + this.num = num; + } + + public String toString() { + return name + "\t" + price + "\t" + num; + } +} diff --git a/src/chapter6/demo61/Manager.java b/src/chapter6/demo61/Manager.java new file mode 100644 index 0000000..25437ce --- /dev/null +++ b/src/chapter6/demo61/Manager.java @@ -0,0 +1,81 @@ +package chapter6.demo61; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Scanner; + +public class Manager { + static ArrayList goodsList = new ArrayList<>(); + // static ArrayList goodsList = new ArrayList<>(); + public static void main(String[] args) { + Goods meizu = new Goods("魅族22", 3999, 11); + goodsList.add(meizu); + + goodsList.add(new Goods("小米15", 6899, 10)); + goodsList.add(new Goods("iPhone15", 5999, 2)); + + while (true) { + System.out.println("欢迎使用库房管理系统"); + System.out.println("1. 商品入库"); + System.out.println("2. 商品显示"); + System.out.println("3. 商品出库"); + System.out.print("请选择要进行的操作:"); + + Scanner sc = new Scanner(System.in); + int select = sc.nextInt(); + switch (select) { + case 1: + addGoods(); + System.out.println("商品入库成功,入库后仓库商品如下:"); + showWareHouse(); + break; + case 2: + showWareHouse(); + break; + case 3: + System.out.print("请输入要出库的商品编号:"); + int id = sc.nextInt(); + removeGoods(id); + System.out.println("商品出库成功,出库后仓库商品如下:"); + showWareHouse(); + break; + } + } + } + + // 商品入库 + static void addGoods() { + Scanner sc = new Scanner(System.in); + + System.out.print("商品名称:"); + String name = sc.next(); + + System.out.print("商品价格:"); + double price = sc.nextDouble(); + + System.out.print("商品数量:"); + int num = sc.nextInt(); + + // Goods g = new Goods(name, price, num); + // goodsList.add(g); + + goodsList.add(new Goods(name, price, num)); + } + + // 商品出库(从goodList中删除指定位置的元素) + static void removeGoods(int index) { + goodsList.remove(index); + } + + // 使用迭代器遍历商品 + static void showWareHouse() { + System.out.println("=========================="); + Iterator iterator = goodsList.iterator(); + while (iterator.hasNext()) { + Goods goods = (Goods) iterator.next(); // 类型转换(Object->Goods) + System.out.println(goods); + } + System.out.println("=========================="); + } +}