问一下java的 这一块怎么写

img




/**
 * 西瓜摊
 */
public class Booth {
    /**摊号*/
    private long id;
    /**摊主姓名*/
    private String name;
    /**在售西瓜数*/
    private int total;
    /**是否休摊整改*/
    private boolean isClosed;
    
    public void setId(long id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public void setClosed(boolean isClosed) {
        this.isClosed = isClosed;
    }
    public long getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public int getTotal() {
        return total;
    }
    public boolean isClosed() {
        return isClosed;
    }
    
    @Override
    public String toString() {
        return String.format("摊号:%d, 摊主姓名:%s, 在售西瓜数:%d, 是否休摊整改:%s", this.id, this.name, this.total, this.isClosed ? "是" : "否");
    }

    public Booth(long id, String name, int total, boolean isClosed) {
        this.id = id;
        this.name = name;
        this.total = total;
        this.isClosed = isClosed;
    }
    
    /**
     * 向目标摊位卖家购买指定数量的西瓜
     * @param booth 商家
     * @param total 购买数量
     */
    public static void purchase(Booth booth, int total) {
        if(total < 0) { System.out.println("购买数量必须是正整数"); return; }
        if(booth.isClosed()) { System.out.println("商家正在休摊整改,不能购买"); return; }
        if(booth.getTotal() < total) { System.out.println("购买数量超过了,商家在售西瓜数,不能交易"); return; }
        
        booth.setTotal(booth.getTotal() - total);
        System.out.println(String.format("购买成功,成功购买到 %d 个西瓜", total));
    }
    
    /**
     * 进货
     * @param total 进货西瓜数
     */
    public Booth restock(int total) {
        if(total > 200) { System.out.println("单次进货量不能超过 200"); return this; }
        this.total += total;
        
        System.out.println(String.format("成功进货 %d 个西瓜, 目前库存 %d", total, this.total));
        
        return this;
    }
    
    /**
     * 所有未被休业整改的摊位歇业
     * @param booths 摊位列表
     */
    public static void closeBooths(Booth[] booths) {
        System.out.println("全部摊位开始歇业整改");
        for(Booth booth : booths) {
            if(!booth.isClosed()) {
                booth.setClosed(true);
            }
            System.out.println(booth.toString());
        }
        System.out.println("全部摊位歇业完毕");
    } 
    
    public static void main(String[] args) {
        Booth booth1 =  new Booth(0, "test1", 0, false).restock(200);
        Booth booth2 =  new Booth(1, "test2", 0, true).restock(200);
        Booth booth3 =  new Booth(2, "test3", 0, false).restock(200);
        
        Booth.purchase(booth1, 300);
        Booth.purchase(booth2, 200);
        Booth.purchase(booth3, 30);
        
        Booth.closeBooths(new Booth[] { booth1, booth2, booth3 });
        
    }
}

img

有用望采纳

根据要求自己先写,写完有问题,再来问。
题目说明很清晰,就差组合了。


//西瓜摊类
public class Booth {
    //私有变量
    private long id;//摊号
    private String name;//摊位姓名
    private int total;//在售西瓜数
    private boolean isClosed;//是否休摊整改
    
    //1.1 get set
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return id;
    }
    public String setName(long name) {
        this.name = name;
    }
    public int getTotal() {
        return total;
    }
    public int setTotal(long total) {
        this.total = total;
    }
    public boolean getIsClosed() {
        return isClosed;
    }
    public boolean setIsClosed(long isClosed) {
        this.isClosed = isClosed;
    }
    //1.2 重写ToString();
    public String toString() {
            return "[name=" + name
                    + ",id=" + id.toString()
                    + ",total=" + total.toString()
                    + ",isClosed=" + isClosed.toString()+"]";
        }
    //2构造方法
    Booth(long id,String name,int total,boolean isClosed;){
        this.id=id;
        this.name=name;
        this.total=total;
        this.isClosed=isClosed;
    }
    //3静态方法
    public static String purchase(Booth booth,int num){
        //满足西瓜数必须为正数,商家不能处于休摊状态,购买西瓜数不能大于在售西瓜数
        if(num>0 && !booth.isClosed && num<=booth.total){
            return "购买成功";
        }else{
            return "购买失败";
        }
    }
    //4实例方法
    public String restock(int num){
        if(num>200){
            return "进货失败";
        }else{
            return "进货成功";
        }
    }
    //5静态方法
    public static closeBooths(Booth[] booths){
        for(int i=0;i<booths.length;i++){
           Booth booth=booths[i];
            if(!booth.isClosed){
                booth.isClosed=true;
            }else{
                System.out.println(booth.toString());
            }
        }
    }
}

public class Booth {

private long id;
private String name;
private int total;
private boolean isClosed;


public static void purchase(Booth booth,int num){
    if(booth.isClosed()){
        System.out.println("商家已修摊");
        return;
    }
    if(num < 1){
        System.out.println("购买西瓜数量必须为正数");
        return;
    }
    if(num > booth.getTotal()){
        System.out.println("购买西瓜数量已超出在售西瓜数");
        return;
    }
    booth.setTotal(booth.getTotal()-num);
}

public void restock(int num){
    if(num > 200){
        System.out.println("单次进货量不能超过200");
        return;
    }
    setTotal(getTotal()+num);
}

public static void closeBooths(Booth[] booths){
    for (Booth booth : booths) {
        if(booth.isClosed()){
            System.out.println(booth);
            continue;
        }
        booth.setClosed(true);
    }
}

public long getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getTotal() {
    return total;
}

public void setTotal(int total) {
    this.total = total;
}

public boolean isClosed() {
    return isClosed;
}

public void setClosed(boolean closed) {
    isClosed = closed;
}

@Override
public String toString() {
    return "Booth{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", total=" + total +
            ", isClosed=" + isClosed +
            '}';
}

}

再附赠一个知识点


    /**
     * 所有未被休业整改的摊位歇业
     * @param booths 摊位列表
     */
    public static void closeBooths(Booth[] booths) {
        System.out.println("全部摊位开始歇业整改");
        for(Booth booth : booths) {
            if(!booth.isClosed()) {
                booth.setClosed(true);
            }
            
//            System.out.println(booth.toString());
            System.out.println(booth); // 这里不写 toString 也可以,因为当对象作为 String 输出的时候会隐式调用 toString 方法
        }
        System.out.println("全部摊位歇业完毕");
    } 

希望你采纳,有问题继续询问!

public class Booth {
    /**
     * 摊位编号
     */
    private long id;
    /**
     * 摊位名称
     */
    private String  name;
    
    /**
     * 西瓜总数
     */
    private int total;
    
    /**
     * 是否整顿中
     */
    private  boolean isColsed;
    

    public long getId() {
        return id;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }

    public boolean isColsed() {
        return isColsed;
    }

    public void setColsed(boolean isColsed) {
        this.isColsed = isColsed;
    }

    //toString  方法
    @Override
    public String toString() {
        return "Booth [id=" + id + ", name=" + name + ", total=" + total + ", isColsed=" + isColsed + "]";
    } 
    
    
    /**
     * 购买西瓜的方法
     * @param booth  店家
     * @param count  购买数量
     */
    public static  void  purchase(final Booth booth, int count) {
        if(booth.isColsed()) {
            System.err.println("很抱歉,商家停业装修中,下次再来吧");
        }
        if(count>booth.getTotal()) {
              System.err.println("哎妈呀~老板,你买太多了!没那么多瓜了");
        }
        System.out.println("购买成功,您一共购买"+count+"个瓜,请拿好,欢迎下次光临");
        //减库存
        booth.setTotal(booth.getTotal()-count);
    }
    
    //进瓜方法
    public void restock(int count ) {
        if(count>200) {
            System.err.println("抱歉!一次性进200个西瓜,小三轮拉不动啊");
        }
        //增加库存
        this.total=this.total+count;
        System.out.println("进货成功!你本次进货"+count+"个瓜,目前库存量为:"+this.total);
    }
    
    //关店方法
    public void closeBooth(Booth [] booths ) {
        if (booths==null||booths.length==0) {
            System.err.println("至少选择一个你要关闭的店");
        }
        //循环遍历,一个个关掉
        for(int i=0;i<booths.length;i++) {
            Booth booth=booths[i];
            //关店
            booth.setColsed(true); 
            System.out.println("已成功关闭摊位:"+booth.getName());
            //输出toString方法的内容
            System.out.println(booth.toString());
            System.out.println("=======================");
        }
    }

}

着急了,上面的有bug,重新发一次,希望采纳^_~

public class Booth {
    /**
     * 摊位编号
     */
    private long id;
    /**
     * 摊位名称
     */
    private String  name;
    
    /**
     * 西瓜总数
     */
    private int total;
    
    /**
     * 是否整顿中
     */
    private  boolean isColsed;
    

    public long getId() {
        return id;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }

    public boolean isColsed() {
        return isColsed;
    }

    public void setColsed(boolean isColsed) {
        this.isColsed = isColsed;
    }

    //toString  方法
    @Override
    public String toString() {
        return "Booth [id=" + id + ", name=" + name + ", total=" + total + ", isColsed=" + isColsed + "]";
    } 
    
    
    /**
     * 购买西瓜的方法
     * @param booth  店家
     * @param count  购买数量
     */
    public static  void  purchase(final Booth booth, int count) {
        if(booth.isColsed()) {
            System.err.println("很抱歉,商家停业装修中,下次再来吧");
            return;
        }
        if(count>booth.getTotal()) {
              System.err.println("哎妈呀~老板,你买太多了!没那么多瓜了");
              return;
        }
        System.out.println("购买成功,您一共购买"+count+"个瓜,请拿好,欢迎下次光临");
        //减库存
        booth.setTotal(booth.getTotal()-count);
    }
    
    //进瓜方法
    public void restock(int count ) {
        if(count>200) {
            System.err.println("抱歉!一次性进200个西瓜,小三轮拉不动啊");
            return;
        }
        //增加库存
        this.total=this.total+count;
        System.out.println("进货成功!你本次进货"+count+"个瓜,目前库存量为:"+this.total);
    }
    
    //关店方法
    public void closeBooth(Booth [] booths ) {
        if (booths==null||booths.length==0) {
            System.err.println("至少选择一个你要关闭的店");
            return;
        }
        //循环遍历,一个个关掉
        for(int i=0;i<booths.length;i++) {
            Booth booth=booths[i];
            //关店
            booth.setColsed(true); 
            System.out.println("已成功关闭摊位:"+booth.getName());
            //输出toString方法的内容
            System.out.println(booth.toString());
            System.out.println("=======================");
        }
    }

}