案例6-1 库存管理系统
This commit is contained in:
		
							parent
							
								
									c0479e5bba
								
							
						
					
					
						commit
						c21520d8ee
					
				
							
								
								
									
										4
									
								
								src/chapter6/Example04.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/chapter6/Example04.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
package chapter6;
 | 
			
		||||
 | 
			
		||||
public class Example04 {
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										4
									
								
								src/chapter6/Example05.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/chapter6/Example05.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
package chapter6;
 | 
			
		||||
 | 
			
		||||
public class Example05 {
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										4
									
								
								src/chapter6/Example3.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/chapter6/Example3.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
package chapter6;
 | 
			
		||||
 | 
			
		||||
public class Example3 {
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										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