Compare commits
4 Commits
c0479e5bba
...
9e780a4f1b
Author | SHA1 | Date | |
---|---|---|---|
9e780a4f1b | |||
f3cb1fe50d | |||
0d57276315 | |||
c21520d8ee |
24
src/chapter6/Example04.java
Normal file
24
src/chapter6/Example04.java
Normal file
@ -0,0 +1,24 @@
|
||||
package chapter6;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Example04 {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add("张三");
|
||||
list.add("李四");
|
||||
list.add("王五");
|
||||
list.add("张三");
|
||||
|
||||
Iterator<String> iterator = list.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String name = iterator.next();
|
||||
if (name.equals("张三")) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(list);
|
||||
}
|
||||
}
|
17
src/chapter6/Example05.java
Normal file
17
src/chapter6/Example05.java
Normal file
@ -0,0 +1,17 @@
|
||||
package chapter6;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Example05 {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
list.add("A");
|
||||
list.add("B");
|
||||
list.add("C");
|
||||
list.add("D");
|
||||
|
||||
for(String s : list){
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
22
src/chapter6/Example3.java
Normal file
22
src/chapter6/Example3.java
Normal file
@ -0,0 +1,22 @@
|
||||
package chapter6;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Example3 {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<String> list = new ArrayList();
|
||||
list.add("A");
|
||||
list.add("B");
|
||||
list.add("C");
|
||||
list.add("D");
|
||||
|
||||
Iterator<String> iterator = list.iterator();
|
||||
// Iterator> iterator = list.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String item = iterator.next();
|
||||
// String item = (String) iterator.next();
|
||||
System.out.println(item);
|
||||
}
|
||||
}
|
||||
}
|
44
src/chapter6/demo61/Goods.java
Normal file
44
src/chapter6/demo61/Goods.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
81
src/chapter6/demo61/Manager.java
Normal file
81
src/chapter6/demo61/Manager.java
Normal file
@ -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<Goods> 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("==========================");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user