這篇文章主要介紹了基于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 pds;
// 訂單總金額
public float ordAllAmt;
// 訂單應付金額
public float payAmt;
// 計算總金額的方法
public void setAllAmt() {
float sum = 0;
for(int i=0;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 list = new ArrayList();
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);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持聚米學院。