用java代码写一个商城管理
import java.util.*;
public class ShoppingMallManager {
public static void main(String[] args) {
Map<String, Integer> productMap = new HashMap<>(); // 商品名称与库存量的映射
Map<String, Integer> priceMap = new HashMap<>(); // 商品名称与价格的映射
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入要进行的操作:1、查看所有商品 2、新增商品 3、修改库存 4、退出");
int op = scanner.nextInt();
switch (op) {
case 1: // 查看所有商品
System.out.println("当前所有的产品如下:");
Set<String> keyset = productMap.keySet(); // 获取所有键的集合
for (String key : keyset) { // 遍历键集合
int count = productMap.get(key); // 根据键获取对应的库存量
int price = priceMap.get(key); // 根据键获取对应的价格
System.out.println("产品名称:" + key + ",数量:" + count + ",单价:" + price); } break; case 2: // 新增一个产品 System.out.println("请输入要新加入到产城中的产在信息[name count price]"); String line= scanner .nextLine(); String[] arr= line .split("\\s+"); String name= arr[0]; int count= Integer .parseInt(arr[1]); int price= Integer .parseInt(arr[2]); productMap .put(name ,count ); priceMap .put(name ,price ); break; case 3: // 修改一个产在数量 System .out .println ("请辜九条要修改数量的交昵 [name num]") ; line=scanner .nextLine() ; arr=line ;split ("\\s+") ; name=arr[0] ; count+=Integer .parseInt (arr[1]) ; if (count <0 ){ Systenm println ("Error! The number of the product cannot be negative!" ) ; continue ; } produtcMab
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!