A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunnyday 中级黑马   /  2016-6-4 23:45  /  517 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

老师反反复复的说这个案例很经典,请大家看看,有没有什么要改进的地方



/*
        库存管理案例
        -------------库存管理------------
                1.查看库存清单
                2.修改商品库存数量
                3.退出
        请输入要执行的操作序号:

        分析:
                1.打印功能菜单;
                2.获取用户键盘录入的功能选项
                3.根据用户输入的选项,选择对应的功能,用switch语句完成
                        选择1:查看库存清单
                        选择2:修改商品库存数量
                        选择3:完成退出功能
*/
import java.util.Scanner;
public class StoreList{
        public static void main(String[] args){
               
                //0.定义3组15个变量用来记录商品信息
               
                //定义苹果电脑信息
                //苹果电脑品牌型号
                String macBrand = "MacBookAir";
                //苹果电脑的尺寸
                double macSize = 13.3;
                //苹果电脑的价格
                double macPrice = 6988.88;
                //苹果电脑的配置
                String macConfig = "i5处理器4GB内存128G固态硬盘";
                //苹果电脑的库存数
                int macCount = 5;
               
                //定义TinkPad电脑信息
                //定义ThinkPad电脑品牌型号
                String thinkBrand = "ThinkPadT450";
                //定义ThinkPad电脑尺寸
                double thinkSize = 14.0;
                //定义ThinkPad电脑价格
                double thinkPrice = 5999.99;
                //定义ThinkPad电脑配置
                String thinkConfig = "i5处理器4GB内存500G硬盘";
                //定义ThinkPad电脑库存数
                int thinkCount = 10;
               
                //定义华硕电脑的信息
                //定义华硕电脑品牌型号
                String asusBrand = "ASUS-FL5800";
                //定义华硕电脑尺寸
                double asusSize = 15.6;
                //定义华硕电脑价格
                double asusPrice = 4999.5;
                //定义华硕电脑配置
                String asusConfig = "i7处理器4GB内存128G固态硬盘";
                //定义华硕电脑库存数
                int asusCount = 18;
               
                while(true){
                        //1.打印功能菜单;
                        System.out.println("-------------库存管理------------");
                        System.out.println("1.查看库存清单");
                        System.out.println("2.修改商品库存数量");
                        System.out.println("3.退出");
                        System.out.println("请输入要执行的操作序号:");
                       
                        //2.获取用户键盘录入的功能选项
                        Scanner sc = new Scanner(System.in);
                        int choose = sc.nextInt();//获取用户键盘录入的功能选项
                       
                        //3.根据用户输入的选项,选择对应的功能,用switch语句完成
                        switch(choose){
                                case 1:
                                        //选择1:查看库存清单
                                        //计算总库存数和库存商品总金额
                                        int totalCount = macCount + thinkCount + asusCount;
                                        double totalPrice = (macCount * macPrice) + (thinkCount * thinkPrice) + (asusCount * asusPrice);
                                       
                                        /*
                                        清单打印
                                                * 清单顶部
                                                * 清单中部
                                                * 清单底部
                                        */
                                        //清单顶部
                                        System.out.println("----------------------------商城库存清单-------------------------------");
                                        System.out.println("品牌型号        尺寸        价格        配置                                库存数");
                                        //清单中部
                                        System.out.println(macBrand+"        "+macSize+"        "+macPrice+" "+macConfig+"        "+macCount);
                                        System.out.println(thinkBrand+"        "+thinkSize+"        "+thinkPrice+" "+thinkConfig+"                "+thinkCount);
                                        System.out.println(asusBrand+"        "+asusSize+"        "+asusPrice+" "+asusConfig+"        "+asusCount);
                                        //清单底部
                                        System.out.println("------------------------------------------------------------------------");
                                        System.out.println("总库存数: " + totalCount);
                                        System.out.println("库存商品总金额: " + totalPrice);
                                        break;
                                case 2:
                                        //选择2:修改商品库存数量
                                        System.out.println("请更新"+macBrand+"电脑的库存数量: ");
                                        macCount = sc.nextInt();//获取用户键盘输入的数量
                                       
                                        System.out.println("请更新"+thinkBrand+"电脑的库存数量: ");
                                        thinkCount = sc.nextInt();//获取用户键盘输入的数量
                                       
                                        System.out.println("请更新"+asusBrand+"电脑的库存数量: ");
                                        asusCount = sc.nextInt();//获取用户键盘输入的数量
                                        break;
                                case 3:
                                        //选择3:完成退出功能
                                        System.out.println("程序已结束!");
                                        System.exit(0);//完成退出java虚拟机
                                        break;
                                default:
                                        System.out.println("您输入的功能不存在,请重新输入!");
                                        break;
                        }
                }
        }
}

1 个回复

倒序浏览
不错不错!学习了~~~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马