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.2; else if (cost>1000.00) discount -=0.1; } public String getAddress() { return address; } public void setAddress(String address) { this.address=address; } public String getCardID() { return cardID; } public void setCardID(String cardID) { this.cardID=cardID; } public double getCost() { return cost; } public double getDiscount() { return discount; } public String getEmail() { return email; } public void setEmail(String email) { this.email=email; } public String getName() { return name; } public void setName(String name) { this.name=name; } } Customer customer[] = new Customer[3]; public CustomerDemo() { customer[0] = new Customer("001","wang ziyu","BeiJing","wangziyu@qq.com"); customer[1] = new Customer("002","Zhang zhiyi","ShangHai","Zhangzhiyi@qq.com"); customer[2] = new Customer("003","Li yanxi","BeiJing","Liyanxi@qq.com"); customer[0].buy(2600.00); customer[0].setDiscount(); customer[1].buy(1579.00); customer[1].setDiscount(); customer[2].buy(880.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("dico其中重要变量的定义和作用
时间: 2024-03-14 22:49:20 浏览: 106
SpringBoot 使用hibernate validator校验
这段代码定义了一个名为CustomerDemo的Java类,其中包含了一个名为Customer的内部类。Customer类有五个私有属性:卡号(cardID)、姓名(name)、花费(cost)、地址(address)和电子邮件(email),以及一个折扣(discount)属性,初始值为1。Customer类有一个构造方法,用于初始化卡号、姓名、地址和电子邮件属性。它还有一个buy方法,用于记录花费,并有一个setDiscount方法,根据花费来更新折扣。除此之外,它还有一些getter和setter方法,用于获取和设置属性值。
在CustomerDemo类中,定义了一个包含三个元素的Customer数组,分别用于存储三个客户的信息。随后,分别在每个客户对象上调用buy和setDiscount方法,并输出每个客户的卡号、姓名、花费和折扣。
阅读全文