zoukankan      html  css  js  c++  java
  • 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 + "
     会员级别=" + customerType
    11                 + "
    ";
    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 }
    View Code

    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 =  "======================================
    " + 
    70                     "顾客信息:" + cus.toString() + "
    " +
    71                     "-------------------------------
    " +
    72                     "商品信息:" + pro.toString() + "
    " +
    73                     "商品原价:" + pro.getSumMoney() +"
    " +
    74                         "会员价格:"+ this.getCustomerMoney() + "
    " +
    75                         "======================================
    "; 
    76         }
    77         return str;
    78     }
    79     
    80     
    81 }
    View Code

    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 + "
     商品价格:" + price + "
     商品数量:" + num
    26                 + "
     是否促销:" + onSale + "
    ";
    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 }
    View Code

    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 }
    View Code

    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 }
    View Code

    程序截图

    源码下载

    点击下载

  • 相关阅读:
    为什么说 Java 程序员必须掌握 Spring Boot ?(转)
    Vert.x 之 HelloWorld
    Vert.x Web 文档手册
    Vert.x Core 文档手册
    Android数据存储五种方式总结
    设置简单的定时
    自定义dialog
    Android DrawerLayout 高仿QQ5.2双向侧滑菜单
    Fragments碎片
    理解Fragment生命周期
  • 原文地址:https://www.cnblogs.com/ftl1012/p/supermarket.html
Copyright © 2011-2022 走看看