基于Java創(chuàng)建一個訂單類代碼實例
這篇文章主要介紹了基于Java創(chuàng)建一個訂單類代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
需求描述
- 定義一個類,描述訂單信息
- 訂單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); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于spring-security 401 403錯誤自定義處理方案
這篇文章主要介紹了基于spring-security 401 403錯誤自定義處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Spring框架JavaMailSender發(fā)送郵件工具類詳解
這篇文章主要為大家詳細(xì)介紹了Spring框架JavaMailSender發(fā)送郵件工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04Java中實體類為什么要實現(xiàn)Serializable序列化的作用
這篇文章主要介紹了Java中實體類為什么要實現(xiàn)Serializable序列化的作用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11springBoot 項目排除數(shù)據(jù)庫啟動方式
這篇文章主要介紹了springBoot 項目排除數(shù)據(jù)庫啟動方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Springboot jar運(yùn)行時如何將jar內(nèi)的文件拷貝到文件系統(tǒng)中
因為執(zhí)行需要,需要把jar內(nèi)templates文件夾下的的文件夾及文件加壓到宿主機(jī)器的某個路徑下,以便執(zhí)行對應(yīng)的腳本文件,這篇文章主要介紹了Springboot jar運(yùn)行時如何將jar內(nèi)的文件拷貝到文件系統(tǒng)中,需要的朋友可以參考下2024-06-06