java手机库存管理

import javax.swing.JOptionPane;
public class test {
	public static void main(String[] args) {
		String[] name = { "huawei", "iPhone", "vivo", "oppo" };// name保存4种手机品牌名
		//使用二维数组
		double[][] money = { { 10, 3500 }, { 20, 2100 },
						{ 17, 2800 }, { 8, 1355 } };// money保存手机数量和单价
		int fun = -1;//功能号
		while (true) {
			System.out.println("*********手机销售管理***********");
			System.out.println("----  1. 显示  -----" + "----- 2. 按单价排序   -----");
			System.out.println("----  3. 查询  -----" + "----- 0. 退出  -----");
			System.out.println("请输入功能代码(0——3)");
			//JOptionPane.showInputDialog()方法从窗口接收用户输入的功能代码,该方法返回的是字符串
			// 输入功能代码并解析成整数
			//语句integer.parseint()是将整型数据Integer转换为基本数据类型int
			//例子:
			//String str = "3";
			//int i = Integer.parseInt(str);转化成int类型
			//System.out.println(i+1);//输出4;
			//String str = "3";
			//System.out.println(str+1);//输出 31;
			fun = Integer.parseInt(JOptionPane.showInputDialog("请输入功能号(0---3)")); 
			if (fun < 0 || fun > 3) {
				System.out.println("请重新输入功能代码(0--3)");
				continue;
			}
			switch (fun) {
			case 1:// 执行显示功能
				System.out.println("序号\t手机品牌\t数量\t单价\t小计");
				for (int i = 0; i < name.length; i++) {
					System.out.print(i + "\t" + name[i]);// 输出序号和品牌名
					for (int j = 0; j < money[i].length; j++) {
						System.out.print("\t" + money[i][j]);// 输出数量和单价
					}
					System.out.println("\t" + money[i][0] * money[i][1]);// 输出小计
					// System.out.println();
				}
				break;
			case 2:// 执行按单价从高到低排序功能,采用冒泡排序方法
				for (int i = 0; i < money.length - 1; i++) {
					for (int j = 0; j < money.length - 1 - i; j++) {
						if (money[i][1] < money[i + 1][1]) {// 如果前面的比后面的元素小,交换
							// 交换手机品牌名
							String temp = name[i];
							name[i] = name[i + 1];
							name[i + 1] = temp;
							// 交换数量
							double number = money[i][0];
							money[i][0] = money[i + 1][0];
							money[i + 1][0] = number;
							// 交换单价
							double price = money[i][1];
							money[i][1] = money[i + 1][1];
							money[i + 1][1] = price;
						}
					}
				}
				break;
				//采用JOptionPane.showInputDialog()方法从窗口接收用户输入的手机品牌信息,
				//从数组name中找到匹配的手机并记录index,然后输出该品牌和money[index]的数量和单价。
			case 3:// 查询,输入手机品牌名,显示该手机信息
				String brand = JOptionPane.showInputDialog("请输入品牌名");
				int index = -1;// 手机索引号
				for (int k = 0; k < name.length - 1; k++) {
				//trim()移除字符串两侧的空白字符或其他预定义字符
					if (brand.trim().equalsIgnoreCase(name[k])) {// 取得手机索引号
						index = k;
						break;
					}
				}
				if (index < 0) {// 没有查询品牌的手机
					System.out.println("没有该手机");
					continue;
				}
				System.out.println("品牌名: " + brand + "  数量:" 
								+ money[index][0] + "  单价" + money[index][1]);
				break;
			case 0:
				System.out.println("退出程序");
				//是非正常退出,就是说无论程序正在执行与否,都退出,
				System.exit(1);
			}
		}
	}
}

在这里插入图片描述
功能2是排序,排完序在输出需要再按一次功能1.

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,库存管理系统是一个比较复杂的系统,需要考虑到很多方面,比如商品的分类、库存数量、出入库记录等等。下面是一个简单的Java库存管理系统的实现,仅供参考。 首先,我们需要定义商品类: ```java public class Product { private String id; // 商品ID private String name; // 商品名称 private String category; // 商品分类 private double price; // 商品价格 private int quantity; // 商品数量 public Product(String id, String name, String category, double price, int quantity) { this.id = id; this.name = name; this.category = category; this.price = price; this.quantity = quantity; } // Getter 和 Setter 略 } ``` 然后,我们需要定义一个仓库类,用于管理商品: ```java public class Warehouse { private List<Product> productList; // 商品列表 public Warehouse() { productList = new ArrayList<>(); } // 添加商品 public void addProduct(Product product) { productList.add(product); } // 删除商品 public void removeProduct(String id) { for (Product product : productList) { if (product.getId().equals(id)) { productList.remove(product); break; } } } // 修改商品信息 public void updateProduct(Product product) { for (Product p : productList) { if (p.getId().equals(product.getId())) { p.setName(product.getName()); p.setCategory(product.getCategory()); p.setPrice(product.getPrice()); p.setQuantity(product.getQuantity()); break; } } } // 查询商品 public Product getProduct(String id) { for (Product product : productList) { if (product.getId().equals(id)) { return product; } } return null; } // 获取所有商品 public List<Product> getAllProducts() { return productList; } // 入库 public void inStock(String id, int quantity) { for (Product product : productList) { if (product.getId().equals(id)) { product.setQuantity(product.getQuantity() + quantity); break; } } } // 出库 public void outStock(String id, int quantity) { for (Product product : productList) { if (product.getId().equals(id)) { if (product.getQuantity() >= quantity) { product.setQuantity(product.getQuantity() - quantity); } else { System.out.println("库存不足!"); } break; } } } } ``` 上面的代码实现了商品的增删改查、入库和出库等操作。最后,我们可以编写一个简单的测试类来测试这个库存管理系统: ```java public class Test { public static void main(String[] args) { Warehouse warehouse = new Warehouse(); // 添加商品 Product p1 = new Product("001", "手机", "电子产品", 2999.0, 100); warehouse.addProduct(p1); Product p2 = new Product("002", "电视", "电子产品", 3999.0, 50); warehouse.addProduct(p2); // 获取所有商品 List<Product> productList = warehouse.getAllProducts(); for (Product product : productList) { System.out.println(product); } // 查询商品 Product p = warehouse.getProduct("001"); System.out.println(p); // 修改商品信息 Product newProduct = new Product("001", "手机", "电子产品", 3999.0, 50); warehouse.updateProduct(newProduct); p = warehouse.getProduct("001"); System.out.println(p); // 入库 warehouse.inStock("001", 50); p = warehouse.getProduct("001"); System.out.println(p); // 出库 warehouse.outStock("001", 20); p = warehouse.getProduct("001"); System.out.println(p); } } ``` 上面的测试类演示了如何添加商品、获取所有商品、查询商品、修改商品信息、入库和出库等操作。当然,这只是一个简单的库存管理系统,实际应用中还需根据具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值