随机数0709

一、随机数

1、Math类数学类

包含大量数学相关的计算方法(类方法,可以类名直接引用)

Math.random();随机0-1之间的小数

Math.round(double d);将参数四舍五入为整数

随机x-y的整数的算法:

​ Math.round(Math.random()*(y-x)+x);

2、Random类

创建对象,使用nextInt(int n) 随机0-(n-1)的整数

		for (int i = 0; i < 10; i++) {
            double r = Math.random();
            //System.out.println("r = " + r);
            int a = (int) Math.round(r*10);
            //System.out.println("a = " + a);

            //随机x-y的整数
            int x=-5,y=15;
            int n = (int) Math.round(Math.random()*(y-x)+x);
            //System.out.println("n = " + n);


            //Random类
            Random random = new Random();
            n = random.nextInt(5);//随机0-4
            n = random.nextInt(5)+1;//随机1-5
            System.out.println("n = " + n);
        }

二、柜台商品管理

身份:柜台管理员

商品类

/**
 * 用来描述商品信息的实体类
 *
 */
public class Goods {
    private int id;// 商品编号
    private String goodsName;// 商品名称
    private double price;// 商品价格
    private String desc;// 商品描述

    public Goods() {
    }
    public Goods(int id, String goodsName, double price, String desc) {
        this.id = id;
        this.goodsName = goodsName;
        this.price = price;
        this.desc = desc;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", goodsName='" + goodsName + '\'' +
                ", price=" + price +
                ", desc='" + desc + '\'' +
                '}';
    }
}

柜台类

/**
 * 柜台类
 *  柜台类里面放商品(属性:商品数组)
 */
public class Counter {
    private Goods[]goodses = new Goods[10];//柜台
    private int num;// 柜台商品数量

    //无参构造器初始化2个商品
    public Counter(){
        this.goodses[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
        this.goodses[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
        num=2;//商品数量为2
    }

    //展示柜台所有的商品(不能输出null)
    private void show(){
        System.out.println("-------------  柜台的商品列表  --------------");
        if(this.num==0){
            System.out.println("警告:柜台没有商品..");
            return;
        }

        for (int i = 0; i < this.goodses.length; i++) {
            if(this.goodses[i]!=null){
                System.out.println(this.goodses[i]);
            }
        }
        System.out.println("------------------------------------------");
    }

    //菜单方法(柜台都有功能选择)
    public void main(){
        while(true){
            System.out.println("------------------------------------------");
            System.out.println("-------  1.展示商品    2.上架商品    ------");
            System.out.println("-------  3.下架商品    4.调整价格    ------");
            System.out.println("-------  0.退出                    ------");
            //选择功能,控制台输入
            System.out.println("-->请输入功能编号:");
            int key = InputUtils.getNum();
            switch (key){
                case 1:show();break;
                case 2:add();break;
                case 3:delete();break;
                case 4:update();break;
                case 0:System.exit(0);//退出JVM
            }
        }
    }

    /**
     * 调整价格
     */
    private void update() {
    }

    /**
     * 下架商品
     *
     */
    private void delete() {
        System.out.println("-->请输入要下架的商品编号:");
        int goodsId = InputUtils.getGoodsId();
        //第一件事?找到这个对应商品ID的商品对象
        for (int i = 0; i < this.goodses.length; i++) {
            //查找对应id的商品
            if(this.goodses[i]!=null && this.goodses[i].getId()==goodsId){
                //找到以后,删除?
                this.goodses[i] = null;
                //商品数量减一
                this.num--;
                System.out.println("-->提示:下架成功!!当前商品数量"+this.num);
                //还需不需要继续循环?
                return;//跳出方法(只有找到商品才执行)
            }
        }
        //如果没找到商品,就会执行到这里
        System.out.println("-->警告:未找到商品!!");
    }

    /**
     * 上架商品
     */
    private void add() {
        //判断是否有空余位置
        if(this.num==this.goodses.length){
            System.out.println("-->警告:柜台已满,不能再添加商品了");
            return;
        }

        System.out.println("-->请输入商品id:");
        int goodsId = InputUtils.getGoodsId();
        //丰富:id不能重复
        for (int i = 0; i < this.goodses.length; i++) {
            if(this.goodses[i]!=null && this.goodses[i].getId()==goodsId){
                System.out.println("-->警告:当前编号已存在!!请重新输入");
                add();
                return;//由于重复了,后续不执行
            }
        }

        //程序能走到这里,说明id没有重复
        System.out.println("-->提示:商品id可以使用!!");

        System.out.println("-->请输入商品名称:");
        String name = new Scanner(System.in).next();

        System.out.println("-->请输入商品价格:");
        double price = InputUtils.getPrice();

        System.out.println("-->请输入商品描述:");
        String desc = new Scanner(System.in).next();

        //把输入的信息装载到Goods对象
        Goods newGoods = new Goods(goodsId, name, price, desc);

        //把商品对象保存柜台数组中为null的位置
        for (int i = 0; i < this.goodses.length; i++) {
            //如果这个下标没有Goods商品对象,null,就把商品放进去
            if(this.goodses[i]==null){
                this.goodses[i] = newGoods;
                this.num++;//商品数量加一
                System.out.println("-->提示:商品上架成功!!柜台商品数量"+this.num);
                return;
            }
        }
    }

}

控制台输入工具类

/**
 * 提供一些输入的方法
 */
public class InputUtils {

    /**
     * 获取我们的功能序号
     * @return
     */
    public static int getNum(){
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!请重新输入");
            n = getNum();//递归
        }
        //必须是0-4
        if(n<0||n>4){
            System.out.println("--警告:非法命令!!请重新输入");
            n = getNum();//递归
        }
        return n;//返回输入的数字
    }


    /**
     * 获取输入的商品编号
     * @return
     */
    public static int getGoodsId(){
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!请重新输入");
            n = getGoodsId();//递归
        }
        return n;//返回输入的数字
    }

    /**
     * 获取输入的商品编号
     * @return
     */
    public static double getPrice(){
        double n = 0;
        try {
            n = new Scanner(System.in).nextDouble();
        } catch (Exception e) {
            //e.printStackTrace();
            //如果输入非数字,就会抛出异常,被catch语句捕获,在这里处理异常
            System.out.println("-->警告:输入非法数字!!请重新输入");
            n = getPrice();//递归
        }
        return n;//返回输入的数字
    }
}

测试类:测试使用柜台管理商品

/**
 * 测试柜台类的使用
 */
public class CounterTest {
    public static void main(String[] args) {
        //创建柜台对象
        Counter counter = new Counter();
        //开启菜单
        counter.main();

    }
}

建议:这里做参考,以自己熟悉和自己的思路来解决和实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值