赞
踩
package text61; import java.util.*; class Commodity { private String name; private String color; private double price; private int count; public Commodity() {} public Commodity(String name,String colour,double price,int count) { this(); this.name = name; this.color = colour; this.price = price; this.count = count; } 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 getCount() { return count; } public void setCount(int count) { this.count = count; } } public class Text61 { private static List commoditys = new ArrayList(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); Commodity commodity = new Commodity("狼毫笔", "黑色", 18.5, 10); commoditys.add(commodity); int choose; while (true) { printMenu(); choose = sc.nextInt(); if (choose == 1) { Commodity cdy = new Commodity(); System.out.print("商品名称:"); String cdyName = sc.next(); System.out.print("商品颜色:"); String cdyColor = sc.next(); System.out.print("商品价格:"); double cdyPrice = sc.nextDouble(); System.out.print("商品数量:"); int cdyCount = sc.nextInt(); cdy.setName(cdyName); cdy.setColor(cdyColor); cdy.setPrice(cdyPrice); cdy.setCount(cdyCount); addCommodity(cdy); } else if (choose == 2) { show(commoditys); System.out.println("\n"); } else if (choose == 3) { System.out.println("请输入要删除的商品编号:"); int i = sc.nextInt(); delete(i); } else if(choose == 4) { System.out.println("退出库存管理系统~"); System.exit(0); } else { System.out.println("输入有误,请重新输入!"); } } } public static void delete(int i) { i--; commoditys.remove(i); System.out.println("出库成功!\n"); } public static void addCommodity(Commodity cdy) { commoditys.add(cdy); System.out.println("商品已成功入库!\n"); } public static void show(Collection commoditys) { Iterator<Commodity> it = commoditys.iterator(); System.out.println("编号\t商品名称\t颜色\t价格\t库存"); int n = 1; //使用Iterator接口迭代集合中的元素 while (it.hasNext()) { Commodity cdy = it.next(); System.out.println(n + "\t" + cdy.getName() + "\t" + cdy.getColor() + "\t" + cdy.getPrice() + "\t" + cdy.getCount()); n++; } //使用foreach循环遍历集合中的元素 //for(Object obj : commoditys) { // System.out.print(obj); // n++; // } } public static void printMenu () { System.out.println("欢迎使用库存管理系统"); System.out.println("----------------"); System.out.println("1、商品入库"); System.out.println("2、商品显示"); System.out.println("3、删除商品"); System.out.println("4、退出系统"); System.out.println("-----------------"); System.out.print("请输入你选择操作:"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。