MOOC-Java-第二周-对象交互

开始:对之前做的一个自动贩卖机分解对象

范围:自动贩卖机,可以贩卖饮料。

功能:自动贩卖机可以提供1.选择贩卖机内的商品、2.塞入零钱、3.获取商品、4.提供贩卖机内的商品列表,包括单价、库存

分解:1.构建一个商品类,提供名称和单价、2.构建一个自动贩卖机类,提供商品列表,以及商品的库存。

一、以下是商品类

public class Food {

	private String foodName="";
	private int price=0;
	public Food(String name,int price)
	{
		this.foodName=name;
		this.price=price;
	}
	public void ShowInfo()
	{
		System.out.println(this.foodName+",卖"+this.price+"元");
	}
	public int GetPrice()
	{
		return this.price;
	}
	
	public String GetName()
	{
		return this.foodName;
	}
}

二、以下是自动贩卖机类,看到引用了上边的商品类

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class VendingMachine {
	
	private Food currentSelectFood;
	private int wantNum=0;
	private int balance=0;//余额
	private int totalMoney=0;//总共收了多少钱
	
	//食品及库存
	//使用匿名内部类的方式,在构造TreeMap的时候,提供了一个实现Comparator接口的比较器对象
	private Map<Food,Integer> foodList=new TreeMap<Food,Integer>(new Comparator<Food>() {
		//实现了Comparator接口的compare方法
		@Override
		public int compare(Food a,Food b)
		{
			if(a.GetName().compareTo(b.GetName())>0){
				   return 1;
				  }else if(a.GetName().compareTo(b.GetName())<0){
				   return -1;
				  }else{
				   return a.GetPrice()-b.GetPrice();
				  }
		}
	});
    
    // populating tree map

	//添加食品及库存
	public void addStock(Food a,Integer stock)
	{
		boolean isHave=false;
		for (Map.Entry<Food, Integer> entry : foodList.entrySet()) {
	    	  if(entry.getKey().GetName()==a.GetName())
	    	  {
	    		  isHave=true;
	    		  Integer c=entry.getValue();
	    		  c+=stock;
	    		  entry.setValue(c);
	    		  break;
	    	  }
	      }
		if(!isHave)
		{
			foodList.put(a, stock);
		}
	}
	//核减库存
	public void cutStock(Food a,int num)
	{
		for (Map.Entry<Food, Integer> entry : foodList.entrySet()) {
	    	  if(entry.getKey().GetName()==a.GetName())
	    	  {
	    		  int c=entry.getValue();
	    		  c-=num;
	    		  entry.setValue(c);
	    		  break;
	    	  }
	      }
	}
	
	//查找货品在TreeMap中的索引
	private int findIndex(String name)
	{
		if(foodList.isEmpty())
		{
			return -1;
		}else
		{
			  int i=0;
			  boolean isHave=false;
		      for (Map.Entry<Food, Integer> entry : foodList.entrySet()) {
		    	  if(entry.getKey().GetName()==name)
		    	  {
		    		  isHave=true;
		    	  }else
		    	  {
		    		  i++;
		    	  }
		      }
		      if(isHave)
		      {
		    	  return i;
		      }else
		      {
		    	  return -1;
		      }
		 
		}
	}
	
	//查找货品库存,-1代表没查到
	public int getStockOfFood(String name)
	{
			int r=-1;
			boolean isHave=false;
			for (Map.Entry<Food, Integer> entry : foodList.entrySet()) {
		    	  if(entry.getKey().GetName()==name)
		    	  {
		    		isHave=true;
		    		r=entry.getValue();
		    		break;
		    	  }
		      }
			if(isHave)
			{
				return r;
			}else
			{
				return -1;
			}
	}
	
	//获取当前余额
	public int getBalance()
	{
		return this.balance;
	}
	
	//选择食物,需要几个
	public void selectFood(String currentSelectFood,int wantNum)
	{
		int i=findIndex(currentSelectFood);
		if(i<0)
		{
			System.out.println("售货机中您要选择的食物");
			return;
		}else
		{
			boolean isHave=false;
			 for (Map.Entry<Food, Integer> entry : foodList.entrySet()) {
		    	  if(entry.getKey().GetName()==currentSelectFood)
		    	  {
		    		  int cStock=entry.getValue();
		    		  if(cStock==0)
		    		  {
		    			  System.out.println("您选择的"+currentSelectFood+"没有了");
		    			  return;
		    		  }else if(cStock<wantNum)
		    		  {
		    			  System.out.println("您选择的"+currentSelectFood+"就有"+cStock+"个了");
			    		  return;
		    		  }else
		    		  {
		    			  isHave=true;
		    			  this.currentSelectFood=entry.getKey();
		    			  this.wantNum=wantNum;
		    			  System.out.println("您选择了"+wantNum+"个"+currentSelectFood+",需要付款:"+entry.getKey().GetPrice()*wantNum+"元");
		    			  break;
		    		  }
		    	  }
		      }
			 if(!isHave)
			 {
				 System.out.println("请选择正确的食物");
			 }
		}
	}
	
	//塞入零钱
	public void insertMoney(int n)
	{
		if(this.currentSelectFood==null)
		{
			System.out.println("请先选择食物");
			return;
		}else
		{
			if(this.balance+n<this.currentSelectFood.GetPrice()*this.wantNum)
			{
				this.balance+=n;
				this.totalMoney+=n;
				System.out.println("请继续投币");
			}else
			{
				this.balance+=n;
				this.totalMoney+=n;
			}
		}
	}
	
	//获取食物
	public void getFood()
	{
		if(this.currentSelectFood==null)
		{
			System.out.println("请先选择食物");
			return;
		}
		if(this.balance<this.currentSelectFood.GetPrice()*this.wantNum)
		{
			System.out.println("请先投币");
			return;
		}
		int newBalance=this.balance-this.currentSelectFood.GetPrice()*this.wantNum;
		
		System.out.println("给你"+wantNum+"个"+this.currentSelectFood.GetName());
		System.out.println("单价:"+this.currentSelectFood.GetPrice()+"元,需付款:"+this.currentSelectFood.GetPrice()*this.wantNum+"元");
		System.out.println("找零:"+newBalance+"元,实付款:"+this.balance+"元");
		System.out.println("----------------------------------------------");
		//库存核减
		cutStock(this.currentSelectFood,this.wantNum);
		//成员变量复原
		this.balance=0;
		this.currentSelectFood=null;
		this.wantNum=0;
		
	}
	
	//获取总共收了多少钱
	public int getTotalMoney()
	{
		return this.totalMoney;
	}

	public Food getObject(int i)
	{
		int c=0;
		Food o=null;
		for(Map.Entry<Food, Integer> entry: foodList.entrySet())
		{
			if(c==i)
			{
				o=entry.getKey();
				break;
			}else
			{
				c++;
			}
		}
		return o;
		
	}
	//获取可以提供的食物
	public void WelCome()
	{
		System.out.println("欢迎光临,我们提供如下产品,请选择");
		for(Map.Entry<Food, Integer> entry:foodList.entrySet())
		{
			System.out.println(entry.getKey().GetName()+",单价:"+entry.getKey().GetPrice()+",库存:"+entry.getValue());
		}
		System.out.println("----------------------------------------------");
	}
}
import java.util.Scanner;

public class Main {



	public static void main(String[] args) {

		VendingMachine vm=new VendingMachine();
		//补货
		vm.addStock(new Food("农夫山泉",2), 7);
		vm.addStock(new Food("康师傅矿泉水",1), 10);
		vm.addStock(new Food("红牛",5), 5);
		vm.addStock(new Food("加多宝",4), 2);
		vm.addStock(new Food("统一冰红茶",3), 11);
		vm.addStock(new Food("格瓦斯",6), 5);
		vm.addStock(new Food("娃哈哈矿泉水",1), 20);
		
		vm.addStock(new Food("娃哈哈矿泉水",1), 1);
		
		Scanner in=new Scanner(System.in);
		vm.WelCome();
		Food o=vm.getObject(in.nextInt());
		while(o==null)
		{
			System.out.println("请重新选择:");
			o=vm.getObject(in.nextInt());
		}
		vm.selectFood(o.GetName(), in.nextInt());
		System.out.println("请给钱");
		vm.insertMoney(in.nextInt());
		vm.getFood();
		vm.WelCome();
		in.close();
	
	}
}
以上是测试代码,以下是测试结果。

欢迎光临,我们提供如下产品,请选择
农夫山泉,单价:2,库存:7
加多宝,单价:4,库存:2
娃哈哈矿泉水,单价:1,库存:21
康师傅矿泉水,单价:1,库存:10
格瓦斯,单价:6,库存:5
红牛,单价:5,库存:5
统一冰红茶,单价:3,库存:11
----------------------------------------------
0 2
您选择了2个农夫山泉,需要付款:4元
请给钱
6
给你2个农夫山泉
单价:2元,需付款:4元
找零:2元,实付款:6元
----------------------------------------------
欢迎光临,我们提供如下产品,请选择
农夫山泉,单价:2,库存:5
加多宝,单价:4,库存:2
娃哈哈矿泉水,单价:1,库存:21
康师傅矿泉水,单价:1,库存:10
格瓦斯,单价:6,库存:5
红牛,单价:5,库存:5
统一冰红茶,单价:3,库存:11
----------------------------------------------

此次练习中主要学习了如何针对现实的事物,进行对象的划分,对象的组合。

实验中TreeMap.put方法报错,为了解决此问题,借鉴学习了点击打开链接此链接中TreeMap如何使用引用类型的文章,感谢作者。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值