java用接口做一个商品打折的程序

java用接口做一个商品打折的程序,第1张

首先要知道这个接口需要接受哪些参数,(商品原价,折扣)这两个都是不确定的参数至少要接受这两个参数,另外接口需要返回什么。既然是打折接口的话当然是要返回折后的价格嘛。那么接收商品原价和折扣值后要返回打折后的价格需要做哪些处理就在接口里处理就好了咯。

这个是按您的需求写的,您看看吧,没有用您说要求的Date,Round, 因为我觉得我用的方法会更好一点

import javatextDecimalFormat;
import javautilCalendar;
import javautilGregorianCalendar;
public class CalcFee {
    
    int num;//批发数量
    double per_price;//单价
    int start_num;//起批数量
    int discount;//优惠折扣
    double total_price;//总价
    boolean boo;//是否可以起批
    
    //根据当前时间段设置起批数量、单价、优惠折扣等信息
    public void set_info(){
        Calendar calendar = GregorianCalendargetInstance();
        int hour = calendarget(CalendarHOUR_OF_DAY);
        if (hour == 11 || hour == 16){
            start_num = 100;
            per_price = 3;
            discount = 8;
            
        } else {
            start_num = 200;
            per_price = 5;
            discount = 9;
        }
    }           
    //根据批发数量num计算总价
    public void calc(int num){
        thisnum = num;
        boo = thisnum > start_num;
        
        if (boo) {
            total_price = (per_price start_num) + (per_price (num-start_num) discount/10);
        } else {
            total_price = per_price num;
        }
        
    }
    //输出费用清单
    public void show(){
        DecimalFormat df = new DecimalFormat("###00");
        String display =
            "起批数量:"+start_num +
            "\n单价:"+per_price +
            "\n优惠折扣:"+ discount +"折"+
            "\n批发数量:"+ num +
            "\n============" +
            "\n总价:"+dfformat(total_price);
        
        Systemoutprintln(display);
        
    }
    
    public static void main(String[] args) {
        CalcFee c = new CalcFee();
        cset_info();
        ccalc(201);
        cshow();
    }
}

恩。。其实我还可以写的更复杂一点的。。。
public class Test {
private static final int HIGHEST_DISCOUNT = 1;
private static final String ERR_MESSAGE = "插入失败";
private static final String SUCC_MESSAGE = "允许插入";

private boolean checkDiscount(float dc) {
if (dc > HIGHEST_DISCOUNT || dc < 0) {
return false;
}
return true;
}
public void insert(float dc) {
if (!checkDiscount(dc)) {
Systemoutprintln(ERR_MESSAGE);
}else{
Systemoutprintln(SUCC_MESSAGE);
}
}
public static void main(String[] args) {
Test testor = new Test();
testorinsert(-1);
testorinsert(0);
testorinsert(1);
testorinsert(2);
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13103197.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-30
下一篇 2023-05-30

发表评论

登录后才能评论

评论列表(0条)

保存