java的接口使用实例源码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenyefei/article/details/51468827

*************************封装代码***********************************

public interface BuySomeThing {
	public void DisplayAllGoods(ArrayList<String> allGoods);
	public ArrayList<String> buy();
	public int pay(int money);
	public void finish(ArrayList<String>goods, int ret);
}
public class PetShop {
	private final static String name = "PetShop";
	//HashMap<String, Integer> goodsPriceList = new  HashMap<String, Integer>();
	private ArrayList<String> allGoodsList = new ArrayList<String>();
	private ArrayList<Integer> allPriceList = new ArrayList<Integer>();
	private ArrayList<String> custGoodsList = new ArrayList<String>();
	public PetShop(){
		allGoodsList.add("猫饼干");
		allGoodsList.add("鱼罐头");
		allGoodsList.add("虾米");
		allGoodsList.add("狗零食");
		allGoodsList.add("狗粮");
		allPriceList.add(3);
		allPriceList.add(5);
		allPriceList.add(2);
		allPriceList.add(3);
		allPriceList.add(7);
		System.out.println("创建了一间宠物食品店");
	}
	private void say(String obj, String content){
		System.out.println(obj+" say: "+content);
	}
	private void printSay(String content){
		System.out.println(content);
	}
	public void Sell(BuySomeThing buySomeThing) {
		buySomeThing.DisplayAllGoods(allGoodsList);
		ArrayList<String> buyList = buySomeThing.buy();
		say(name, "你购买了:");
		int spend = 0;
		for(String a:buyList){
			for(int i = 0; i < allGoodsList.size(); i++){
				if(a.equals(allGoodsList.get(i))){
					printSay(a+"(价格:"+allPriceList.get(i)+")");
					spend += allPriceList.get(i);
					custGoodsList.add(allGoodsList.get(i));
				}
			}
		}
		say(name,"一共需要支付:"+spend+"元");
		int pay = buySomeThing.pay(spend);
		if(pay > 0){
			int change = pay - spend;
			if(change > 0){
				say(name,"你给的是"+pay+"元,找你零钱一共"+change+"元钱");
				buySomeThing.finish(custGoodsList, change);
			}else{
				say(name,"这是你买的东西,欢迎下次再来");
				buySomeThing.finish(custGoodsList, 0);
			}
		}
		else{
			say(name,"东西选好了,你居然不买了,浪费表情");
			buySomeThing.finish(null, 0);
		}
		custGoodsList.clear();
	}
}
******************************应用代码****************************

public class animals {

	void say(String obj, String content){
		System.out.println(obj+" say: "+content);
	}
}
public class Cat extends animals implements BuySomeThing{
	private final static String name = "Cat";
	private int myMoney = 10;//整张10元
	private ArrayList<String> allGoodsList = new ArrayList<String>();
	private ArrayList<String> buyList = new ArrayList<String>();
	private ArrayList<String> myGoods = new ArrayList<String>();
	public Cat(){
		System.out.println("创建了一只要买猫食的猫");
	}
	@Override
	public void DisplayAllGoods(ArrayList<String> allGoods) {
		allGoodsList = allGoods;
	}
	
	private void goodsBag(String goods) {
		for(String g:allGoodsList){
			if(g.equals(goods)){
				buyList.add(goods);
				say(name,"购买"+goods+"1盒");
				return;
			}
		}
		System.out.println("未找到购买商品");
	}
	@Override
	public ArrayList<String> buy() {
		say(name,"我是猫,我要买猫粮");
		goodsBag("猫饼干");
		goodsBag("鱼罐头");
		say(name,"就买这些准备结账");
		return buyList;
	}
	@Override
	public int pay( int money) {
		if(myMoney < money){
			say(name,"钱没带够,我不买了");
			return 0;
		}
		say(name,"好的,给你"+myMoney+"元");
		return myMoney;
	}
	@Override
	public void finish( ArrayList<String>goods, int ret) {
		myMoney += ret;
		myGoods = goods;
		say(name,"我购买了"+myGoods);
	}

}
public class Dog extends animals implements BuySomeThing{
	private final static String name = "Dog";
	private int myMoney = 10; //零钱:1元+2元+2元+5元
	private ArrayList<String> allGoodsList = new ArrayList<String>();
	private ArrayList<String> buyList = new ArrayList<String>();
	private ArrayList<String> myGoods = new ArrayList<String>();
	public Dog() {
		System.out.println("创建了一只要买狗食的狗");
	}
	@Override
	public void DisplayAllGoods(ArrayList<String> allGoods) {
		allGoodsList = allGoods;
	}
	private void goodsCart(String goods) {
		for(String g:allGoodsList){
			if(g.equals(goods)){
				buyList.add(goods);
				say(name,"购买"+goods+"1袋");
			}
		}
	}
	@Override
	public ArrayList<String> buy() {
		say(name, "我是狗,我要买狗食");
		goodsCart("狗粮");
		say(name,"就买这一个准备结账");
		return buyList;
	}
	@Override
	public int pay(int money) {
		if(myMoney < money){
			say(name,"你们简直是一家黑店");
			return 0;
		}
		if(money <= 5){
			say(name,"和之前买的一样很好");
			myMoney -= 5; //直接拿出5元钱付款
			say(name,"好的,给你5元");
			return 5;
		}
		else if((money > 5)&&(money < 8)){
			say(name,"不是都是5块的,怎么变成"+money+"块了");
			myMoney -= 7; //给一张5元的和一张2元的
			say(name,"给你7元");
			return 7;
		}
		else{
			say(name,"不是都是5块的,一下子变成了"+money+"块,你们简直比银行还贼!");
			say(name,"我不买了");
			return 0;
		}
	}
	@Override
	public void finish(ArrayList<String>goods, int ret) {
		myMoney += ret;
		myGoods = goods;
		say(name,"东西太贵,服务一般,回去就给个差评,呵呵");
		say(name,"我购买了"+myGoods);
	}
}
public class intoMain {
	
	public static void main(String[] args) {
		System.out.println("extends and implements test");
		Cat cat = new Cat();
		Dog dog = new Dog();
		PetShop petShop = new PetShop();
		System.out.println("The cat get into the PetShop");
		petShop.Sell(cat);
		System.out.println("The dog get into the PetShop");
		petShop.Sell(dog);
	}
}


运行结果:

extends and implements test
创建了一只要买猫食的猫
创建了一只要买狗食的狗
创建了一间宠物食品店
The cat get into the PetShop
Cat say: 我是猫,我要买猫粮
Cat say: 购买猫饼干1盒
Cat say: 购买鱼罐头1盒
Cat say: 就买这些准备结账
PetShop say: 你购买了:
猫饼干(价格:3)
鱼罐头(价格:5)
PetShop say: 一共需要支付:8元
Cat say: 好的,给你10元
PetShop say: 你给的是10元,找你零钱一共2元钱
Cat say: 我购买了[猫饼干, 鱼罐头]
The dog get into the PetShop
Dog say: 我是狗,我要买狗食
Dog say: 购买狗粮1袋
Dog say: 就买这一个准备结账
PetShop say: 你购买了:
狗粮(价格:7)
PetShop say: 一共需要支付:7元
Dog say: 不是都是5块的,怎么变成7块了
Dog say: 给你7元
PetShop say: 这是你买的东西,欢迎下次再来
Dog say: 东西太贵,服务一般,回去就给个差评,呵呵
Dog say: 我购买了[狗粮]

源码下载地址:http://download.csdn.net/detail/chenyefei/9526819

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页