JAVA练习之消费用户折扣卡模拟

来源:互联网 发布:数学资源网源码 编辑:程序博客网 时间:2024/04/30 18:39

简单模拟一个商店客户折扣卡的功能,自定义Customer类用来保存了在某个商店中的客户的折扣卡信息。在主类CustomerDemo中,创建Customer类的一个数组对象,该数组中包含了三个Customer的对象,用来保存不同的三个消费者各自持有的折扣卡信息。通过这三个对象,可以根据用户消费的金额来改变用户在本店中所能享受到的折扣价格。

代码如下:

public class CustomerDemo {class Customer{private String cardID;private String name;private double cost = 0;private String address;private String email;private double discount = 1;public Customer(String id,String name,String add,String email){cardID = id;this.name = name;address = add;this.email=email;}public void buy(double cost){this.cost +=cost;}public void setDiscount(){if (cost>2000.00)discount -= 0.1;else if (cost>1000.00)discount -=0.05;}public void setAddress(String address){this.address=address;}public String getAddress(){return address;}public void setCardID(String cardID){this.cardID=cardID;}public String getCardID(){return cardID;}public double getCost(){return cost;}public double getDiscount(){return discount;}public void setEmail(String email){this.email=email;}public String getEmail(){return email;}public void setName(String name){this.name=name;}public String getName(){return name;}}Customer customer[] = new Customer[3];public CustomerDemo(){customer[0] = new Customer("c0001","wangxyw","BeiJing","wangxyue@cn.ibm.com");customer[1] = new Customer("c0002","Xu Quan","ShangHai","chunticha@yahoo.com");customer[2] = new Customer("c0003","Xu Guang Yang","BeiJing","xugy@hotmail.com");customer[0].buy(2800.00);customer[0].setDiscount();customer[1].buy(1688.00);customer[1].setDiscount();customer[2].buy(980.00);customer[2].setDiscount();for(int i=0; i < customer.length; i++){System.out.println("customer["+ i +"]");System.out.println("cardID:" + customer[i].getCardID());System.out.println("name:" + customer[i].getName());System.out.println("cost:" + customer[i].getCost());System.out.println("dicount:" + customer[i].getDiscount()*10);System.out.println("address:" + customer[i].getAddress());System.out.println("eamil:" + customer[i].getEmail()+"\n");}}public static void main(String[] args) {new CustomerDemo();}}
运行结果:

/*output: * customer[0]cardID:c0001name:wangxywcost:2800.0dicount:9.0address:BeiJingeamil:wangxyue@cn.ibm.comcustomer[1]cardID:c0002name:Xu Quancost:1688.0dicount:9.5address:ShangHaieamil:chunticha@yahoo.comcustomer[2]cardID:c0003name:Xu Guang Yangcost:980.0dicount:10.0address:BeiJingeamil:xugy@hotmail.com*///~

学习心得:

对JAVA创建对象更加熟悉,之前没有对这个点投入太多,结果自己做的时候常常出现问题,看来把java尽快学好势在必行。

0 0
原创粉丝点击