农场游戏开发记录十八

经过一段时间的学习,初步了解了MongoDB的特性和使用方法。今天开始更新农场游戏,将它做成拥有数据库可存档的版本。如果顺利的话,紧跟着复习web和javascript。把界面也做出来。毕竟用了MongoDB后,控制台一大堆信息,不再适合用控制台玩了。

今日更新:种子商店的购买种子方法,实现了种子的插入和更新,实现了金钱的修改以及判断功能。明天试试把连接数据库、集合、迭代的对象写成静态的试试。

package shop;

import java.util.ArrayList;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

import farmgame.Farmer;
import plants.Seed;

public class SeedShop extends Shop {

	public SeedShop() {
		super.setShopName("种子公司");
		super.setItemList(new String[] { "番茄种子", "土豆种子", "玉米种子", "南瓜种子", null, null, null, null, null, null, null, null,
				null, null, null, null, null, null, null, null });
		super.setPrices(new float[] { 1.0F, 1.0F, 1.0F, 1.0F, 2.0F, 2.0F, 2.0F, 2.0F, 3.0F, 3.0F, 3.0F, 3.0F, 4.0F,
				4.0F, 4.0F, 4.0F, 5.0F, 5.0F, 5.0F, 5.0F}); 
	}

	public void welcomBuy() {
		System.out.println("欢迎光临" + this.getShopName() + "您想购买什么?");
		for(int i = 0;i<this.getItemList().length;i++) {
			if(this.getItemList()[i] != null)
			System.out.print((i+1)+ "." +this.getItemList()[i] + " 每个 " +  this.getPrices()[i] + "元。");
			}
		System.out.println("5、返回首菜单。");
		System.out.println();
	}

		public float sellSeeds(Seed sd) {
		switch(sd.getProductName()) {
		case "番茄种子":			return sd.getProductNum()*1F;			
		case "土豆种子":			return sd.getProductNum()*1F;			
		case "玉米种子":			return sd.getProductNum()*1F;			
		case "南瓜种子":			return sd.getProductNum()*1F;		
			default:				return 0.0F;
		}
	}
		
		public void buySeed(Seed sd) {
			try {
				//连接到mongodb服务
				MongoClient mongoClient = new MongoClient("localhost", 27017);
				//连接到数据库
				MongoDatabase mongoDatabase = mongoClient.getDatabase("farmgamedb");
				System.out.println("success");
				MongoCollection<Document> collection = mongoDatabase.getCollection("Seed");
				//集合Seed选择成功
				//插入文档  
				FindIterable<Document> findIterable = collection.find(new Document("seedName","番茄种子"));  
				MongoCursor<Document> mongoCursor = findIterable.iterator();  
				System.out.println(mongoCursor.hasNext()+"***");
				//如果没该种子则直接插入,如果已有则更新种子数量
				if(!mongoCursor.hasNext()) {
				Document document = new Document("seedName",sd.getProductName()).append("seedNum",sd.getProductNum());
				ArrayList<Document> documents = new ArrayList<>();
				documents.add(document);
				collection.insertMany(documents);
				System.out.println("文档插入成功");
				}else {
					collection.updateMany(Filters.eq("seedName", sd.getProductName()), new Document("$set",new Document("seedNum",(int)mongoCursor.next().get("seedNum")+sd.getProductNum())));  
					System.out.println("文档修改成功");
				}

				//扣减金钱
		         MongoCollection<Document> collectionFarmer = mongoDatabase.getCollection("Farmer");
		     	FindIterable<Document> fiFarmer = collectionFarmer.find(); 
		         MongoCursor<Document> mcFarmer = fiFarmer.iterator();  
		         double x = (double)mcFarmer.next().get("cash") - sd.getProductNum()*1.0;
		         collectionFarmer.updateOne(Filters.gt("cash", 0),new Document("$set",new Document("cash",x)));
				}
				catch(Exception e){
					 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
				}
		}
		public static void main(String[] args) {
			SeedShop ssp = new SeedShop();
			ssp.welcomBuy();
			Farmer  farmer = new Farmer();
			try {
				//连接到mongodb服务
			
			MongoClient mongoClient = new MongoClient("localhost", 27017);
			//连接到数据库
			MongoDatabase mongoDatabase = mongoClient.getDatabase("farmgamedb");
			System.out.println("success");
			MongoCollection<Document> collection = mongoDatabase.getCollection("Farmer");
			FindIterable<Document> findIterable = collection.find();  
			MongoCursor<Document> mongoCursor = findIterable.iterator();  
			farmer.setCash((double)mongoCursor.next().get("cash"));
			}
			catch(Exception e){
				 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
			}
			Seed sd = new Seed("番茄种子",4);
			if(ssp.sellSeeds(sd)<=farmer.getCash())
				ssp.buySeed(sd);
			else
				System.out.println("您的金额不够");
		}
}

隔了好久,终于更新了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值