Create an order class code instance based on Java

Time:2021-6-12

This article mainly introduces how to create an order class code example based on Java. The example code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. Friends in need can refer to it

Requirement description

  • Define a class to describe the order information
  • Order ID
  • Order user (user object)
  • Goods contained in the order (variable quantity of goods objects)
  • Total order amount
  • Order payable amount:
    • Total amount 500 ~ 1000, discount 85%
    • Total amount 1000 ~ 1500, discount 80%
    • Total amount 1500 ~ 2000, 70% discount
    • 65% discount if the total amount exceeds 2000

On this basis, it also depends on the user’s VIP level

  • If the VIP level of the user is: general member, the discount is 95%
  • If the user’s VIP level is: intermediate member, the discount is 90%
  • If the VIP level of the user is senior member, the discount is 80%

code implementation

User.java

package cn.test.logan.day04;
/**
 *User class
 *Contains information items: user ID, user name, user membership level
 * @author QIN
 *
 */
public class User {
  //User ID
  public String CustId;
    
  //User name
  public String CustName;
    
  //User membership level
  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;
/**
 *Commodity category
 *Include: Commodity ID, commodity name, commodity price, commodity quantity
 * @author QIN
 *
 */
public class Product {
  
  //Product ID
  public String pId;
  
  //Trade name
  public String pName;
  
  //Commodity price
  public float price;
  
  //Quantity of goods
  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;

/**
 *Order class
 *Include: order ID, order user, order commodity, order total amount, order payable amount
 *50% discount for 500-1000
 *1000-1500 -------- > 20% discount
 *1500-2000 -------- > 70% off
 *More than 2000 ------ > 6.50% off
 *If you are a member, you can continue to discount based on the above discount
 *Regular members: 15% off
 *Intermediate members: 10% off
 *Senior members: 20% off
 * @author QIN
 *
 */
public class Order {
  //Order ID 
  public String ordId;
  
  //Order user
  public User user;
  
  //Items included in the order (multiple items, using ArrayList)
  public ArrayList<Product> pds;
  
  //Total order amount
  public float ordAllAmt;
  
  //Order payable amount
  public float payAmt;
  
  //How to calculate the total amount
  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;
  }
  
  //Calculate the amount actually paid
  public void setPayAmt() {
    float tmp = this.ordAllAmt;
    
    //Discount according to the total amount
    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;
    }
    
    //Discount according to membership level
    If (user. Custlevel. Equals){
      tmp = tmp * 0.95f;
    }
    If (user. Custlevel. Equals){
      tmp = tmp * 0.9f;
    }
    If (user. Custlevel. Equals){
      tmp = tmp * 0.8f;
    }
    //The calculation result is assigned to the payamt variable on the object
    this.payAmt = tmp;
  }

}

OrderTest.java

package cn.test.logan.day04;

import java.util.ArrayList;

public class OrderTest {
  public static void main(String[] args) {
    //Create order object
    Order ord = new Order();
    ord.ordId="001";
    
    //Create the user object to which the order belongs
    User u_ XM = new user ("C001", "Xiaoming", "senior member");
    ord.user = u_xm;
    //Create commodity object
    ArrayList<Product> list = new ArrayList<Product>();
    
    Product P1 = new product ("P001", "Jack Jones", 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 ("total order amount: + ord. Ordallamt)";
    System. Out. Println ("order payable amount): + ord. Payamt)";
  }
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

[rust] basic data type

This article introduces the built-in data types provided by rust. Boolean type Boolean typeLogical values representing yes and No. It has two values:trueandfalseGenerally used in logical expressions, and, or, and not operations can be performed: fn main() { let x = true; let y: bool = ! x; // False, inverse operation let z = […]