温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Java怎么创建一个订单类

发布时间:2021-03-10 16:59:51 来源:亿速云 阅读:273 作者:Leah 栏目:编程语言

使用Java怎么创建一个订单类?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

需求描述

  • 定义一个类,描述订单信息

  • 订单id

  • 订单所属用户(用户对象)

  • 订单所包含的商品(不定数量个商品对象)

  • 订单总金额

  • 订单应付金额:

    • 总金额500~1000,打折85折

    • 总金额1000~1500,打折80折

    • 总金额1500~2000,打折70折

    • 总金额超过2000,打折65折

在此基础上,还要看用户的vip等级

  • 用户vip等级为:一般会员,则折上折:95

  • 用户vip等级为:中级会员,则折上折:90

  • 用户vip等级为:高级会员,则折上折:80

代码实现

User.java

package cn.test.logan.day04;
/**
 * 用户类
 * 包含信息项目:用户ID、用户名、用户会员等级
 * @author QIN
 *
 */
public class User {
  // 用户ID
  public String CustId;
    
  // 用户名
  public String CustName;
    
  // 用户会员等级
  public String CustLevel;
    
  public User() {
    
  }
  
  public User(String CustId,String CustName,String CustLevel) {
    this.CustId = CustId;
    this.CustName = CustName ;
    this.CustLevel = CustLevel ;
  }
}

Product.java

package cn.test.logan.day04;
/**
 * 商品类
 * 包含:商品ID、商品名称、商品价格、商品数量
 * @author QIN
 *
 */
public class Product {
  
  // 商品ID
  public String pId;
  
  // 商品名称
  public String pName;
  
  //商品价格
  public float price;
  
  // 商品数量
  public int number;
  
  public Product() {
    
  }
  
  public Product(String pId, String pName,float price,int number) {
    this.pId = pId;
    this.pName = pName;
    this.price = price;
    this.number = number;
  }
}

Order.java

package cn.test.logan.day04;

import java.util.ArrayList;

/**
 * 订单类
 * 包含:订单ID、订单所属用户、订单所包含的商品、订单总金额、订单应付金额
 * 500-1000 -------> 8.5折
 * 1000-1500 -------> 8折
 * 1500-2000 -------> 7折
 * 2000以上 -------> 6.5折
 *  如果是会员,那么可以基于以上折扣继续折扣
 *  一般会员:9.5折
 *  中级会员:9折
 *  高级会员:8折
 * @author QIN
 *
 */
public class Order {
  // 订单ID 
  public String ordId;
  
  // 订单所属用户
  public User user;
  
  // 订单所包含的商品(多个商品,使用ArrayList)
  public ArrayList<Product> pds;
  
  // 订单总金额
  public float ordAllAmt;
  
  // 订单应付金额
  public float payAmt;
  
  // 计算总金额的方法
  public void setAllAmt() {
    float sum = 0;
    for(int i=0;i<this.pds.size();i++) {
      sum +=this.pds.get(i).price * this.pds.get(i).number;
    }
    this.ordAllAmt = sum;
  }
  
  // 计算实付金额
  public void setPayAmt() {
    float tmp = this.ordAllAmt;
    
    // 根据总金额进行折扣
    if(this.ordAllAmt >= 500 && this.ordAllAmt < 1000) {
      tmp = this.ordAllAmt * 0.85f;
    }
    if(this.ordAllAmt >= 1000 && this.ordAllAmt < 1500) {
      tmp = this.ordAllAmt * 0.8f;
    }
    if(this.ordAllAmt >= 1500 && this.ordAllAmt < 2000) {
      tmp = this.ordAllAmt * 0.7f;
    }
    if(this.ordAllAmt >= 2000) {
      tmp = this.ordAllAmt * 0.65f;
    }
    
    // 根据会员等级折扣
    if(user.CustLevel.equals("一般会员")) {
      tmp = tmp * 0.95f;
    }
    if(user.CustLevel.equals("中级会员")) {
      tmp = tmp * 0.9f;
    }
    if(user.CustLevel.equals("高级会员")) {
      tmp = tmp * 0.8f;
    }
    //计算结果赋值给对象上的payAmt变量
    this.payAmt = tmp;
  }

}

OrderTest.java

package cn.test.logan.day04;

import java.util.ArrayList;

public class OrderTest {
  public static void main(String[] args) {
    // 创建订单对象
    Order ord = new Order();
    ord.ordId="001";
    
    // 创建订单所属用户对象
    User u_xm = new User("C001","小明","高级会员");
    ord.user = u_xm;
    // 创建商品对象
    ArrayList<Product> list = new ArrayList<Product>();
    
    Product p1 = new Product("P001","杰克琼斯",500.5f,2);
    Product p2 = new Product("P002","Nick",1000f,1);
    Product p3 = new Product("P003","Adidas",1200f,2);
    
    
    list.add(p1);
    list.add(p2);
    list.add(p3);
    
    ord.pds = list ;
    ord.setAllAmt();
    ord.setPayAmt();
    
    System.out.println("订单总金额:" + ord.ordAllAmt);
    System.out.println("订单应付金额:" + ord.payAmt);
  }
}

关于使用Java怎么创建一个订单类问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI