zoukankan      html  css  js  c++  java
  • 【Java】购物超市

    小明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。

    package chapter6;
    
    
    public class Job6 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ShoppingCart ps = new ShoppingCart(2);
            ps.add(new Phone("iphone5",1,1000));
            ps.add(new Computer("huashuo",2,2000));
    
            ps.del("huashuo");
            ps.printCart();
            System.out.println(Shop.paymen(ps));
        }
        
        public static Commodity[] delete(int i, Commodity[] c)
        {
            Commodity[] cnew = new Commodity[c.length-1];
            for(int j=0;j<c.length-1;j++)
            {
                if(j<i)
                {
                    cnew[j] = c[j];
                }
                else
                {
                    cnew[j] = c[j+1];
                }
                
            }
            return cnew;
        }
    
    }
    interface Commodity
    {
        public String getName();
        public int getAmount();
        public double getPrice();
    }
    
    class Phone implements Commodity
    {
        private String name;
        private int amount;
        private double price;
        
        public Phone(String name, int amount, double price) {
            super();
            this.name = name;
            this.amount = amount;
            this.price = price;
        }
    
        public String getName() {
            return name;
        }
    
        public int getAmount() {
            return amount;
        }
    
        public double getPrice() {
            return price;
        }
        
    }
    
    class Computer implements Commodity
    {
        private String name;
        private int amount;
        private double price;
        public Computer(String name, int amount, double price) {
            super();
            this.name = name;
            this.amount = amount;
            this.price = price;
        }
        public String getName() {
            return name;
        }
        public int getAmount() {
            return amount;
        }
        public double getPrice() {
            return price;
        }
        
    }
    
    
    class ShoppingCart
    {
        private Commodity[] goods;
        private int foot;
        
        public ShoppingCart(int len)
        {
            if(len>0)
                this.goods = new Commodity[len];
            else 
                this.goods = new Commodity[1];
        }
        public boolean add(Commodity commodity)
        {
            if(this.foot<this.goods.length)
            {
                this.goods[this.foot] = commodity;
                this.foot++;
                return true;
            }
            else
            {
                return false;
            }
        }
        public boolean del(String name)
        {
            int t = 0;
            Commodity[] goodsnew = null;
            for(int i = 0;i<this.goods.length;i++)
            {
                if(this.goods[i]!=null)
                {
                    if(this.goods[i].getName().indexOf(name)!=-1)
                    {
                        goodsnew = Job6.delete(i, this.goods);
                        t = 1;
                    }
                }
            }
            if(t==1)
            {
                this.goods = goodsnew;
                this.foot--;
                return true;
            }
            else
                return false;
        }
    
        public void printCart()
        {
            for(int i=0;i<this.goods.length;i++)
            {
                if(this.goods[i]!=null)
                    System.out.println(goods[i].getName()+','+goods[i].getAmount()+','+goods[i].getPrice());
            }
        }
        public Commodity[] getGoods() {
            return goods;
        }
        public int getFoot() {
            return foot;
        }
    
        
    }
    
    class Shop
    {
        public static  double paymen(ShoppingCart sc)
        {
            double sum  = 0;
            for(int i=0;i<sc.getGoods().length;i++)
            {
                sum += sc.getGoods()[i].getPrice() * sc.getGoods()[i].getAmount();
            }
            return sum;
        }
    
    }
    
    
    
    
    
    
  • 相关阅读:
    初步理解Java的三大特性——封装、继承和多态
    设计模式
    区分super和this
    类—矩形面积
    生成不重复的随机数(待完善)
    关于类的创建和调用
    生成验证码
    endsWith和startsWith同样效果其他形式的写法(2016.1.12)
    去除字符串中空格的方法(2016.1.12P141-2)
    java的抽象类
  • 原文地址:https://www.cnblogs.com/blknemo/p/10020201.html
Copyright © 2011-2022 走看看