Teacher's Test Questions
Three object classes
Commodity category
User class
Order Category
Commodities contain the following attributes:
Commodity id
Name of commodity
item pricing
Order, the order contains the following attributes:
Order id
username, the user name to which the order belongs
items in the commodity array (where multiple commodity objects are stored)
Total order amount (can not be directly assigned to the caller, should be automatically calculated internally!!! Total order amount = the sum of the prices of each commodity in the commodity array)
payment of the actual amount payable for the order
The caller can't assign a value directly. It should be computed automatically internally!!!
Logic for calculating the actual amount payable:
If the total amount of the order is between [0-99], the amount payable = the total amount
If the total amount of the order is between [100-199], the amount payable = the total amount*0.9
If the total amount of the order is between [199-200], the amount payable = the total amount*0.85
If the total amount of the order is > 200, the amount payable = the total amount*0.8)
Customer, the customer contains the following attributes:
Customer name username
Customer Level (Level value can be "Supreme Diamond VIP", "Platinum VIP", "Ordinary Customer")
Then the username attribute of the order class in question 4 is changed to be represented by the customer object. The other attributes are consistent with that in question 4.
Order id
Users to whom the order belongs, such as Customer c;
items in the commodity array (where multiple commodity objects are stored)
Total order amount (total order amount = total price of each commodity in the commodity array)
The actual amount of payment for the order (change the logic before: on the basis of the previous total amount discount, according to the customer's grade, discount again, such as: Supreme Diamond VIP superimposed a discount of 8%, Platinum VIP superimposed a discount of 9, ordinary customers do not superimpose a discount)
Then write a program to simulate the creation of several goods, an order, and then print the order information (extended requirements: you can directly obtain the most expensive goods in the order by calling a custom method on the order object)
Implementation code
public class Test5{ public static void main(String[] args){ String username="Xiao_Ming"; String level="Supreme Diamond VIP"; Customer customer=new Customer(username,level);//User Object Item[] items=new Item[5];//Commodity array int id=0; String[] name={"Millet 1","Millet 2","Millet 3","Millet 4","Millet 5"}; float price=10f; for(String na:name){ Item it=new Item(id,na,price); items[id]=it;//The loop generates five objects to join the array id++; price+=10f; } int id1=0; System.out.println(new Order(id1,customer,items));//Rewrite toString() } } class Item{//Commodity category private int id; private String name; private float price; public Item(){ } public Item(int id,String name,float price){ this.id=id; this.name=name; this.price=price; } public void setId(int id){ this.id=id; } public int getId(){ return id; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setPrice(float price){ if(price<0){ this.price=0; System.out.println("The price is wrong"); }else this.price=price; } public float getPrice(){ return price; } public String toString(){ return "This commodity ID by:"+getId()+",The name is:"+getName()+",The unit price is:"+getPrice(); } } class Order{//Order Category private int id; private Customer customer; private Item[] items; private float amount; private float payment; public Order(){ } public Order(int id,Customer customer,Item[] items){ this.id=id; this.customer=customer; this.items=items; } public void setId(int id){ this.id=id; } public int getId(){ return id; } public void setCustomer(Customer customer){ this.customer=customer; } public Customer getCustomer(){ return customer; } public void setItems(Item[] items){ this.items=items; } public Item[] getItems(){ return items; } public float getAmount(){ for(int i=0;i<items.length;i++) amount+=items[i].getPrice(); return amount; } public float getPayment(){ if(amount<=99){//The above checked guaranteed price is not less than 0. payment=amount; }else if(amount<=199){ payment=amount*0.9f; }else{ payment=amount*0.8f; } if(customer.getLevel().equals("Supreme Diamond VIP"))//Customer Class Inspection payment=payment*0.8f; else if(customer.getLevel().equals("Enjoy Platinum VIP")) payment=payment*0.9f; else payment=payment; return payment; } public String toString(){ return "This order ID by:"+getId()+",The user name is:"+customer.getUsername()+",The user level is:"+customer.getLevel()+",The total amount of the order is ___________.:"+getAmount()+",Amount payable:"+getPayment()+",Most expensive goods:"+gui(); } public Item gui(){//The most expensive commodity float price=items[0].getPrice(); Item it=new Item(); for(int i=0;i<items.length;i++) if(price<items[i].getPrice()){ price=items[i].getPrice(); it=items[i]; } return it; } } class Customer{//Customer class private String username; private String level; public Customer(){ } public Customer(String username,String level){ this.username=username; if(level.equals("Supreme Diamond VIP")||level.equals("Enjoy Platinum VIP")||level.equals("Ordinary users")) this.level=level; else System.out.println("Error in grade information"); } public void setUsername(String username){ this.username=username; } public String getUsername(){ return username; } public void setLevel(String level){ if(level.equals("Supreme Diamond VIP")||level.equals("Enjoy Platinum VIP")||level.equals("Ordinary users")) this.level=level; else System.out.println("Error in grade information"); } public String getLevel(){ return level; } public String toString(){ return "User name:"+getUsername()+",The user level is:"+getLevel(); } }
Today I learned about basic java encapsulation