Java实例---简单的超市管理系统
代码分析
Customer.java
1 package test; 2 3 public class Customer { 4 5 private String name; 6 private int customerType; 7 8 @Override 9 public String toString() { 10 return "顾客姓名:" + name + "\n 会员级别=" + customerType 11 + "\n"; 12 } 13 14 public Customer(String name, int customerType) { 15 super(); 16 this.name = name; 17 this.customerType = customerType; 18 } 19 public String getName() { 20 return name; 21 } 22 public void setName(String name) { 23 this.name = name; 24 } 25 public int getCustomerType() { 26 return customerType; 27 } 28 public void setCustomerType(int customerType) { 29 this.customerType = customerType; 30 } 31 32 33 34 }
Order.java
1 package test; 2 3 import java.util.Arrays; 4 5 public class Order { 6 private Customer cus; 7 private Product[] pros; 8 9 10 public Order(Customer cus, Product[] pros) { 11 super(); 12 this.cus = cus; 13 this.pros = pros; 14 } 15 16 17 18 public double getCustomerMoney() 19 { 20 double money = 0.0; 21 for(Product pro:pros) 22 { 23 if(!pro.isOnSale()) 24 { 25 if(cus.getCustomerType() == Type.Customer_GOLEAN) 26 { 27 money = pro.getSumMoney() * 0.96; 28 }else{ 29 money = pro.getSumMoney() ; 30 } 31 } 32 else if(cus.getCustomerType() == Type.Customer_Common) 33 { 34 money = pro.getSumMoney() * 0.99 ; 35 } 36 else if(cus.getCustomerType() == Type.Customer_PRIVARY) 37 { 38 money = pro.getSumMoney() * 0.98; 39 } 40 else if(cus.getCustomerType() == Type.Customer_ADVANCE) 41 { 42 money = pro.getSumMoney() * 0.97; 43 }else if(cus.getCustomerType() == Type.Customer_GOLEAN) 44 { 45 money = pro.getSumMoney() * 0.96; 46 } 47 } 48 return money; 49 } 50 51 52 public Customer getCus() { 53 return cus; 54 } 55 public void setCus(Customer cus) { 56 this.cus = cus; 57 } 58 public Product[] getPros() { 59 return pros; 60 } 61 public void setPros(Product[] pros) { 62 this.pros = pros; 63 } 64 @Override 65 public String toString() { 66 String str = null; 67 for(Product pro:pros) 68 { 69 str = "======================================\n" + 70 "顾客信息:" + cus.toString() + "\n" + 71 "-------------------------------\n" + 72 "商品信息:" + pro.toString() + "\n" + 73 "商品原价:" + pro.getSumMoney() +"\n" + 74 "会员价格:"+ this.getCustomerMoney() + "\n" + 75 "======================================\n"; 76 } 77 return str; 78 } 79 80 81 }
Product.java
1 package test; 2 3 public class Product { 4 5 private String name; 6 private double price; 7 private int num; 8 private boolean onSale; 9 10 public Product(String name, double price, int num, boolean onSale) { 11 super(); 12 this.name = name; 13 this.price = price; 14 this.num = num; 15 this.onSale = onSale; 16 } 17 18 public double getSumMoney() 19 { 20 return this.price * this.num; 21 } 22 23 @Override 24 public String toString() { 25 return "商品名称:" + name + "\n 商品价格:" + price + "\n 商品数量:" + num 26 + "\n 是否促销:" + onSale + "\n"; 27 } 28 29 30 public String getName() { 31 return name; 32 } 33 public void setName(String name) { 34 this.name = name; 35 } 36 public double getPrice() { 37 return price; 38 } 39 public void setPrice(double price) { 40 this.price = price; 41 } 42 public int getNum() { 43 return num; 44 } 45 public void setNum(int num) { 46 this.num = num; 47 } 48 public boolean isOnSale() { 49 return onSale; 50 } 51 public void setOnSale(boolean onSale) { 52 this.onSale = onSale; 53 } 54 55 }
Type.java
1 package test; 2 3 public class Type { 4 5 public static int Customer_Common = 1; 6 public static int Customer_PRIVARY = 2; 7 public static int Customer_ADVANCE = 3; 8 public static int Customer_GOLEAN = 4; 9 10 public static int PRODUCT_ON_SALE = 5; 11 public static int PRODUCT_NOT_SALE = 6; 12 }
Test.java
1 package test; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 Product pro = new Product("小米",12.2, 4,false); 8 Product pro1 = new Product("黄瓜",1.2, 6,false); 9 Product pro2 = new Product("香蕉",2.2, 14,true); 10 Product pro3 = new Product("果汁",7.8, 7,false); 11 Product pro4 = new Product("面包",22.4, 5,true); 12 13 Customer cus = new Customer("小明",1); 14 Customer cus1 = new Customer("小白",2); 15 Customer cus2 = new Customer("小王",3); 16 17 Customer cus3 = new Customer("小贵",4); 18 19 Order oder = new Order(cus, new Product[]{pro, pro4}); 20 Order oder1 = new Order(cus1, new Product[]{pro2, pro3}); 21 Order oder2 = new Order(cus2, new Product[]{pro1, pro3}); 22 Order oder3 = new Order(cus3, new Product[]{pro, pro4,pro2}); 23 24 System.out.println(oder.toString()); 25 System.out.println(oder1.toString()); 26 System.out.println(oder2.toString()); 27 System.out.println(oder3.toString()); 28 } 29 30 }
程序截图
源码下载
作者:小a玖拾柒
-------------------------------------------
个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~
本文版权归作者【小a玖拾柒】和【博客园】共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!