溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中怎么創(chuàng)建一個訂單類

發(fā)布時間:2021-07-28 16:09:42 來源:億速云 閱讀:179 作者:Leah 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Java中怎么創(chuàng)建一個訂單類,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

需求描述

定義一個類,描述訂單信息  訂單id  訂單所屬用戶(用戶對象)  訂單所包含的商品(不定數(shù)量個商品對象)  訂單總金額  訂單應(yīng)付金額:      總金額500~1000,打折85折    總金額1000~1500,打折80折    總金額1500~2000,打折70折    總金額超過2000,打折65折

在此基礎(chǔ)上,還要看用戶的vip等級

用戶vip等級為:一般會員,則折上折:95  用戶vip等級為:中級會員,則折上折:90  用戶vip等級為:高級會員,則折上折:80

代碼實現(xiàn)

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、商品名稱、商品價格、商品數(shù)量 * @author QIN * */public class Product {    // 商品ID  public String pId;    // 商品名稱  public String pName;    //商品價格  public float price;    // 商品數(shù)量  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、訂單所屬用戶、訂單所包含的商品、訂單總金額、訂單應(yīng)付金額 * 500-1000 -------> 8.5折 * 1000-1500 -------> 8折 * 1500-2000 -------> 7折 * 2000以上 -------> 6.5折 *  如果是會員,那么可以基于以上折扣繼續(xù)折扣 *  一般會員:9.5折 *  中級會員:9折 *  高級會員:8折 * @author QIN * */public class Order {  // 訂單ID   public String ordId;    // 訂單所屬用戶  public User user;    // 訂單所包含的商品(多個商品,使用ArrayList)  public ArrayList<Product> pds;    // 訂單總金額  public float ordAllAmt;    // 訂單應(yīng)付金額  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;        // 根據(jù)總金額進(jìn)行折扣    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;    }        // 根據(jù)會員等級折扣    if(user.CustLevel.equals("一般會員")) {      tmp = tmp * 0.95f;    }    if(user.CustLevel.equals("中級會員")) {      tmp = tmp * 0.9f;    }    if(user.CustLevel.equals("高級會員")) {      tmp = tmp * 0.8f;    }    //計算結(jié)果賦值給對象上的payAmt變量    this.payAmt = tmp;  }}

OrderTest.java

package cn.test.logan.day04;import java.util.ArrayList;public class OrderTest {  public static void main(String[] args) {    // 創(chuàng)建訂單對象    Order ord = new Order();    ord.ordId="001";        // 創(chuàng)建訂單所屬用戶對象    User u_xm = new User("C001","小明","高級會員");    ord.user = u_xm;    // 創(chuàng)建商品對象    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("訂單應(yīng)付金額:" + ord.payAmt);  }}

上述就是小編為大家分享的Java中怎么創(chuàng)建一個訂單類了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI