笔试:天猫魔盒下单问题

s天猫双十一有个积分换墨盒的活动,总共有50万台天猫魔盒(box),每个用户(user)可以用99个天猫积分(point)兑换一台魔盒,且每人限换一台。请设计一套java接口并实现下单(order)逻辑。参考(但不局限于)下面的下单逻辑:

1、创建订单

2、扣减用户积分

3、扣减魔盒库存

4、下单成功

同时请回答:

1、数据库表结构如何设计,有哪些表,分别有什么作用?

2、下单过程中哪些地方可能成为瓶颈?如何解决或改善?

3、是否会用到数据库事务,哪些地方会用到?如果不用数据库事务,如何保证数据的一致性?

首先,我们考虑到实体有用户类和魔盒类,用户和魔盒之间涉及到交易,即每次交易生成一个订单。对应的包含三个数据表,包含的必要属性。
用户(用户ID,用户名,天猫积分)
魔盒(魔盒ID,魔盒名称,魔盒积分,库存数量,总数量)
订单(订单ID,用户ID,订单时间,订单状态)

/*用户类*/
public class User
{
    private int userID;
    private int userName;
    private int credit;//积分

    //getters and setters方法
    ......
}
/*魔盒类,单例模式*/
public class MagicBox
{
   private static MagicBox magicBox;
   private int restNumOfMagicBox=500000;
   private MagicBox(){}
   public static MagicBox getMagicBox()
   {
       if(magicBox==null)
       {
          synchronized(MagicBox.class)
          {
             if(magicBox==null)
               {
                 magicBox=new MagicBox();
               }
          }

      }
     return magicBox;   
   }

   public int getRestMagicBox()
   {
      return restNumOfMagicBox;
   }
   public void setRestMagicBox(int restNumOfMagicBox)
   {
       this.restNumOfMagicBox=restNumOfMagicBox;
   }
}

public interface OrderOperationImpl
{
   //创建订单
   public boolean createOrder(User user);
   //扣除积分
   public boolean minusCredit(User user,int credit);
   //消减库存
   public boolean minusMagicBox();
   //订单状态
   public boolean OrderStatus(User user);
}

public OrdeOperation implements OrderOperationImpl
{
   MagicBox magicBox=new MagicBox();
   public boolean createOrder(User user)
   {
     if(magicBox.getRestMagicBox>=1&&user.getCredit()>=99)
     {
        //创建订单细节
          ......
        return true;
     }
      return false;
   }
  public boolean minusCredit(User user,int credit)
  {
     if(user.getCredit>=credit)
     {
        user.setCredit(user.getCredit-credit);
        return true;
     }
     return false;
  }
  public boolean minusMagicBox()
  {
      if(magicBox.getRestMagicBox()>0)
      {
         magicBox.set(magicBox.getRestMagicBox()-1);
         return true;
      }
      return false;
   }
 public boolean orderStatus()
 {
 if(this.createOrder(user)&&this.minusCredit(user,99)&&this.minusMagicBox()){
          return true;
           }
           return false;
 }
}

下单过程中的瓶颈
数据库写操作问题,主要集中在对魔盒数量的读写问题。对魔盒库存操作过程中使用线程同步。
是否数据库事务,用到了。事务是一组原子性的SQL查询。在该操作中,涉及到到的事物,减积分,减魔盒库存,添加订单,其中任意操作失败,那么事务中的所有操作都不会执行。

A公司打算搭建一个Android App下载的Web站点,计划将目前常见的手机APP都放到这个网站上提供下载。因为业务开展初期下载量很小,技术部门就用了1台服务器,给服务器配置了一个公网IP对外进行服务。随着销售部门的推广到位,用户量和下载量呈指数级上载,要求技术部门马上进行改造。如果你是技术部门经理,你会怎么改造这个站点,以满足高负载的需求。
提示:短时间修改网站的代码不现实,其他方面的各种改造建议都可以,建议越多越好。
1.对APP资源进行多服务器备份,在每个下载页面新增多个下载通道。
2.新增在下载时,需校验图像验证码,确保用户确实要下载内容。
3.资源重定向:增加N台内部服务器,组成一个局域网,将APP复制N份放到内部服务器中,原来的服务器作为与因特网通信的接口。当原服务器收到客户端请求后,服务器选择一个负载最小的内部服务器,将客户端的请求重定向到该服务器 。
转载链接:
http://www.lxway.com/4269912.htm
http://udn.yyuap.com/forum.php?mod=viewthread&tid=18643&typeid=243

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值