예제 #1
0
 @Test
 public void testHumanSetArmor() {
   Human h = new Human("Steve", 30, -50);
   // test the setting of armor, even though the init does this for us.
   h.setArmorPoints(20);
   assertEquals(20, h.getArmorPoints());
 }
  @Test
  public void testUpdateOnImplicitJoinFails() {
    Session s = openSession();
    Transaction t = s.beginTransaction();

    Human human = new Human();
    human.setName(new Name("Steve", 'E', null));

    Human mother = new Human();
    mother.setName(new Name("Jane", 'E', null));
    human.setMother(mother);

    s.save(human);
    s.save(mother);
    s.flush();

    t.commit();

    t = s.beginTransaction();
    try {
      s.createQuery("update Human set mother.name.initial = :initial")
          .setString("initial", "F")
          .executeUpdate();
      fail("update allowed across implicit join");
    } catch (QueryException e) {
    }

    s.createQuery("delete Human where mother is not null").executeUpdate();
    s.createQuery("delete Human").executeUpdate();
    t.commit();
    s.close();
  }
예제 #3
0
 public static void main(String[] args) {
     Human man = new Man();
     Human woman = new Woman();
     man.sayHello();
     woman.sayHello();
     man = new Woman();
     man.sayHello();
 }
예제 #4
0
 public void destroy(Human man) {
   if (isEvil) {
     System.out.println(
         "Beep Boop Pew! Oh no! the robot " + name + " blasted " + man.getName() + "'s head off!");
     man.die();
   } else {
     System.out.println("No!! The robot " + name + " loves " + man.getName());
   }
 }
  public void testSimpleValidation() {
    ValidateContext context;
    Human human = new Human();
    context = ValidateHome.validate(human, null);
    // 3 properties are missing: firstname, birthdate and lastname
    assertTrue(context.getMissingProperties().size() == 3);
    System.out.println(context);

    //
    human.setFirstName("Eric");
    human.setLastName("Lef");
    context = ValidateHome.validate(human, null);
    assertTrue(context.getMissingProperties().size() == 1);
    System.out.println(context);
    //
    human.setFirstName("Eric");
    human.setLastName("Eric");
    context = ValidateHome.validate(human, null);
    assertTrue(context.getMissingProperties().size() == 1);
    System.out.println(context);
    //
    human.setFirstName("Eric");
    human.setLastName("Lefevre");
    human.setBirthDate(new Date(2007, 0, 1));
    context = ValidateHome.validate(human, null);
    assertNull(context.getMissingProperties());
    System.out.println(context);

    human.setEmailAddress("kjhkh@kljh");
    context = ValidateHome.validate(human, null);
    assertNull(context.getMissingProperties());
    System.out.println(context);
  }
예제 #6
0
 /** Randomly populate the field with foxes and rabbits. */
 private void populate() {
   Random rand = new Random();
   field.clear();
   for (int row = 0; row < field.getDepth(); row++) {
     for (int col = 0; col < field.getWidth(); col++) {
       if (rand.nextInt(120) <= HUMAN_CREATION_PROBABILITY) {
         if (nbHumans > 0) {
           Location location = new Location(row, col);
           Human h = new Human("Human" + row + col, HP_HUMANS, location, field);
           characterList.add(h);
           nbHumans--;
           switch (rand.nextInt(4)) {
             case 0:
               ShotGun weapon = new ShotGun(5, 2, field, location);
               h.setWeapon(weapon);
               break;
             case 1:
               LiquidNitrogen weapon1 = new LiquidNitrogen(2, field, location);
               h.setWeapon(weapon1);
               break;
             case 2:
               WoodenStake weapon2 = new WoodenStake(field, location);
               h.setWeapon(weapon2);
               break;
             default:
               break;
           }
         }
       } else if (rand.nextInt(120) <= ZOMBIE_CREATION_PROBABILITY) {
         if (nbZombies > 0) {
           Location location = new Location(row, col);
           Zombie z = new Zombie("Zombie" + row + col, HP_ZOMBIES, location, field);
           characterList.add(z);
           nbZombies--;
         }
       } else if (rand.nextInt(120) <= VAMPIRE_CREATION_PROBABILITY) {
         Location location = new Location(row, col);
         Vampire v = new Vampire("Vampire", HP_VAMPIRES, location, field);
         characterList.add(v);
       } else if (rand.nextInt(1000) <= MADZOMBIE_CREATION_PROBABILITY) {
         if (nbMadZombies > 0) {
           Location location = new Location(row, col);
           MadZombie mz = new MadZombie("Mad Zombie" + row + col, HP_ZOMBIES, location, field);
           characterList.add(mz);
           nbMadZombies--;
         }
       }
     }
   }
 }
예제 #7
0
  public static void main(String[] args) {
    Human original = new Human(25, "Dean");
    System.out.println(original);

    Human copy = (Human) original.copy();
    System.out.println(copy);

    // Copy with help interface "Copyable"
    HumanFactory factory = new HumanFactory(copy);
    Human copy2 = factory.makeCopy();
    System.out.println(copy2);

    // Copy with help factory
    factory.setPrototype(new Human(30, "Mary"));
    Human copy3 = factory.makeCopy();
    System.out.println(copy3);
  }
예제 #8
0
  public static void main(String[] args) {

    // 第一条生产线,男性生产线
    HumanFactory maleHumanFactory = new MaleHumanFactory();

    // 第二条生产线,女性生产线
    HumanFactory femaleHumanFactory = new FemaleHumanFactory();

    // 生产线建立完毕,开始生产人了:
    Human maleYellowHuman = maleHumanFactory.createYellowHuman();

    Human femaleYellowHuman = femaleHumanFactory.createYellowHuman();

    maleYellowHuman.cry();
    maleYellowHuman.laugh();
    femaleYellowHuman.sex();
  }
예제 #9
0
  @Test
  public void testUnderbid() {

    Human buyer = new Human();
    Human seller = new Human();

    seller.createConsumable();

    IConsumable product = seller.getItemsForSale().get(0);
    final int consumableId = product.getId();
    final BigDecimal price = product.getPrice();

    TransactionTerms transactionTerms =
        new TransactionTerms(price.multiply(new BigDecimal(.1)), consumableId);
    TransactionAgreement agreement = buyer.proposeTransaction(seller, transactionTerms);

    Transaction trans = new Transaction(buyer, seller, agreement);

    Assert.assertTrue(!trans.getProcessed());
  }
예제 #10
0
  @Test
  public void testInvalidTransaction() {

    Human buyer = new Human();
    Human seller = new Human();

    final int consumableId = -5;
    final BigDecimal price = new BigDecimal(-95954);

    TransactionTerms transactionTerms = new TransactionTerms(price, consumableId);
    try {
      TransactionAgreement agreement = buyer.proposeTransaction(seller, transactionTerms);

      Transaction trans = new Transaction(buyer, seller, agreement);

      Assert.assertTrue(!trans.getProcessed());
    } catch (IndexOutOfBoundsException e) {
      Assert.assertNotNull(e);
    }
  }
예제 #11
0
  public static void main(String[] args) throws IOException {
    // init class
    Place place = new Place();
    place.setName("World");

    Human human = new Human();
    human.setMessage("Hi");
    human.setPlace(place);

    // convert to json
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Human> jsonAdapter = moshi.adapter(Human.class);

    String jsonString = jsonAdapter.toJson(human);
    System.out.println(
        "json " + jsonString); // print "json {"message":"Hi","place":{"name":"World"}}"

    // convert from json
    Human newHuman = jsonAdapter.fromJson(jsonString);
    newHuman.say(); // print "Hi , World!"
  }
  @Test
  @SuppressWarnings({"UnnecessaryUnboxing"})
  public void testUpdateOnComponent() {
    Session s = openSession();
    Transaction t = s.beginTransaction();

    Human human = new Human();
    human.setName(new Name("Stevee", 'X', "Ebersole"));

    s.save(human);
    s.flush();

    t.commit();

    String correctName = "Steve";

    t = s.beginTransaction();

    int count =
        s.createQuery("update Human set name.first = :correction where id = :id")
            .setString("correction", correctName)
            .setLong("id", human.getId().longValue())
            .executeUpdate();

    assertEquals("Incorrect update count", 1, count);

    t.commit();

    t = s.beginTransaction();

    s.refresh(human);

    assertEquals("Update did not execute properly", correctName, human.getName().getFirst());

    s.createQuery("delete Human").executeUpdate();
    t.commit();

    s.close();
  }
예제 #13
0
  /**
   * Run the simulation from its current state for a single step. Iterate over the whole field
   * updating the state of each fox and rabbit.
   */
  public void simulateOneStep() {
    step++;

    // Provide space for newborn humans.
    List<Human> newHumans = new ArrayList<Human>();
    // Provide space for newborn vampires.
    List<Vampire> newVampires = new ArrayList<Vampire>();
    // Provide space for newborn wolfs.
    List<Zombie> newZombies = new ArrayList<Zombie>();
    // Let all characters act.
    for (Iterator<Character> it = characterList.iterator(); it.hasNext(); ) {
      Character c = it.next();
      if (c.getCharacter() == TypeCharacter.HUMAN) {
        Human h = (Human) c;
        h.run(newHumans);
      }
      if (c.getCharacter() == TypeCharacter.VAMPIRE) {
        Vampire v = (Vampire) c;
        v.hunt(newVampires);
      }
      if (c.getCharacter() == TypeCharacter.ZOMBIE) {
        Zombie z = (Zombie) c;
        z.hunt(newZombies);
      }

      if (c.getCharacter() == TypeCharacter.MADZOMBIE) {
        MadZombie mz = (MadZombie) c;
        mz.hunt(newZombies);
      }
    }

    // Add the newly born humans, vampires and zombies to the main lists.
    characterList.addAll(newHumans);
    characterList.addAll(newVampires);
    characterList.addAll(newZombies);

    view.showStatus(step, field);
  }
예제 #14
0
  @Override
  protected void initializeStats() {
    super.initializeStats();
    hp = attributes[HPMAX] = 18;
    attributes[STRENGTH] = 0;
    attributes[MAGIC] = 3;
    attributes[SKILL] = 3;
    attributes[SPEED] = 3;
    attributes[LUCK] = 8;
    attributes[DEFENSE] = 1;
    attributes[RESISTANCE] = 8;
    attributes[CONSTITUTION] = 9;
    weight = 9;
    movement = attributes[MOVEMENTMAX] = 5;

    weaponClass[Weapon.SCEPTER] = 1;
  }
예제 #15
0
 /**
  * Operation
  *
  * @param h Human (ember) típusú ellenség.
  */
 public void damage(Human h) {
   Logging.log(">> Bullet.damage() hívás, paraméter: " + h.toString());
   h.sebez(humanDamage);
 }
예제 #16
0
파일: Good.java 프로젝트: jaegerpl/TeamSim
  public void step(final SimState state) {
    VirusInfectionDemo hb = (VirusInfectionDemo) state;

    desiredLocation = null;
    double distance2DesiredLocation = 1e30;

    Bag mysteriousObjects =
        hb.environment.getObjectsWithinDistance(
            agentLocation, 50.0 * VirusInfectionDemo.HEALING_DISTANCE);
    if (mysteriousObjects != null) {
      for (int i = 0; i < mysteriousObjects.numObjs; i++) {
        if (mysteriousObjects.objs[i] != null && mysteriousObjects.objs[i] != this) {
          // if agent is not human, wasted time....
          if (!(((Agent) mysteriousObjects.objs[i]) instanceof Human)) continue;
          Human ta = (Human) (mysteriousObjects.objs[i]);
          // if agent is already healthy, wasted time....
          if (!ta.isInfected()) continue;
          if (hb.withinHealingDistance(this, agentLocation, ta, ta.agentLocation))
            ta.setInfected(false);
          else {
            if (getIsGreedy()) {
              double tmpDist = distanceSquared(agentLocation, ta.agentLocation);
              if (tmpDist < distance2DesiredLocation) {
                desiredLocation = ta.agentLocation;
                distance2DesiredLocation = tmpDist;
              }
            }
          }
        }
      }
    }

    steps--;
    if (desiredLocation == null || !getIsGreedy()) {
      if (steps <= 0) {
        suggestedLocation =
            new Double2D(
                (state.random.nextDouble() - 0.5)
                        * ((VirusInfectionDemo.XMAX - VirusInfectionDemo.XMIN) / 5
                            - VirusInfectionDemo.DIAMETER)
                    +
                    // VirusInfectionDemo.XMIN
                    agentLocation.x
                // +VirusInfectionDemo.DIAMETER/2
                ,
                (state.random.nextDouble() - 0.5)
                        * ((VirusInfectionDemo.YMAX - VirusInfectionDemo.YMIN) / 5
                            - VirusInfectionDemo.DIAMETER)
                    + agentLocation.y
                // VirusInfectionDemo.YMIN
                // +VirusInfectionDemo.DIAMETER/2
                );
        steps = 100;
      }
      desiredLocation = suggestedLocation;
    }

    double dx = desiredLocation.x - agentLocation.x;
    double dy = desiredLocation.y - agentLocation.y;

    {
      double temp = 0.5 * /*Strict*/ Math.sqrt(dx * dx + dy * dy);
      if (temp < 1) {
        steps = 0;
      } else {
        dx /= temp;
        dy /= temp;
      }
    }

    if (!hb.acceptablePosition(this, new Double2D(agentLocation.x + dx, agentLocation.y + dy))) {
      steps = 0;
    } else {
      agentLocation = new Double2D(agentLocation.x + dx, agentLocation.y + dy);
      hb.environment.setObjectLocation(this, agentLocation);
    }
  }
  @SuppressWarnings({"unchecked"})
  @Test
  public void testUpdateWithWhereExistsSubquery() {
    // multi-table ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Human joe = new Human();
    joe.setName(new Name("Joe", 'Q', "Public"));
    s.save(joe);
    Human doll = new Human();
    doll.setName(new Name("Kyu", 'P', "Doll"));
    doll.setFriends(new ArrayList());
    doll.getFriends().add(joe);
    s.save(doll);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    String updateQryString =
        "update Human h "
            + "set h.description = 'updated' "
            + "where exists ("
            + "      select f.id "
            + "      from h.friends f "
            + "      where f.name.last = 'Public' "
            + ")";
    int count = s.createQuery(updateQryString).executeUpdate();
    assertEquals(1, count);
    s.delete(doll);
    s.delete(joe);
    t.commit();
    s.close();

    // single-table (one-to-many & many-to-many) ~~~~~~~~~~~~~~~~~~~~~~~~~~
    s = openSession();
    t = s.beginTransaction();
    SimpleEntityWithAssociation entity = new SimpleEntityWithAssociation();
    SimpleEntityWithAssociation other = new SimpleEntityWithAssociation();
    entity.setName("main");
    other.setName("many-to-many-association");
    entity.getManyToManyAssociatedEntities().add(other);
    entity.addAssociation("one-to-many-association");
    s.save(entity);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    // one-to-many test
    updateQryString =
        "update SimpleEntityWithAssociation e "
            + "set e.name = 'updated' "
            + "where exists ("
            + "      select a.id "
            + "      from e.associatedEntities a "
            + "      where a.name = 'one-to-many-association' "
            + ")";
    count = s.createQuery(updateQryString).executeUpdate();
    assertEquals(1, count);
    // many-to-many test
    if (getDialect().supportsSubqueryOnMutatingTable()) {
      updateQryString =
          "update SimpleEntityWithAssociation e "
              + "set e.name = 'updated' "
              + "where exists ("
              + "      select a.id "
              + "      from e.manyToManyAssociatedEntities a "
              + "      where a.name = 'many-to-many-association' "
              + ")";
      count = s.createQuery(updateQryString).executeUpdate();
      assertEquals(1, count);
    }
    s.delete(entity.getManyToManyAssociatedEntities().iterator().next());
    s.delete(entity);
    t.commit();
    s.close();
  }
예제 #18
0
 @Test
 public void test_equals_notequal() {
   Human h1 = new Human();
   Human h2 = new Human("Adam", 20);
   assertFalse(h1.equals(h2));
 }
예제 #19
0
 /** Notify this Game that column col has been clicked by a user. */
 public void columnClicked(int col) {
   if (activePlayer instanceof Human) {
     ((Human) activePlayer).columnClicked(col);
   }
 }
예제 #20
0
 @Test
 public void test_toString_Adam() {
   Human h = new Human("Adam", 20);
   assertEquals("Adam is 20 years old.", h.toString());
 }
예제 #21
0
 @Test
 public void test_setAge_and_getAge() {
   Human h = new Human("Adam", 20);
   h.setAge(21); // so soon...
   assertEquals(21, h.getAge());
 }
예제 #22
0
 @Test
 public void test_setName_and_getName() {
   Human h = new Human("Adam", 20);
   h.setName("Bob");
   assertEquals("Bob", h.getName());
 }
예제 #23
0
  public static void main(String[] args) {
    System.out.println("------------造出的第一批人是这样的:白人-----------------");
    Human whiteHuman = HumanFactory.createHuman(WhiteHuman.class);
    whiteHuman.cry();
    whiteHuman.laugh();
    whiteHuman.talk();
    System.out.println("\n\n------------造出的第二批人是这样的:黑人-----------------");
    Human blackHuman = HumanFactory.createHuman(BlackHuman.class);
    blackHuman.cry();
    blackHuman.laugh();
    blackHuman.talk();
    // 第三批人了,这次火候掌握的正好,黄色人种(不写黄人,免得引起歧义),备注:RB人不属
    System.out.println("\n\n------------造出的第三批人是这样的:黄色人种-----------------");

    Human yellowHuman = HumanFactory.createHuman(YellowHuman.class);
    yellowHuman.cry();
    yellowHuman.laugh();
    yellowHuman.talk();

    // 女娲烦躁了,爱是啥人种就是啥人种,烧吧
    for (int i = 0; i < 10L; i++) {
      System.out.println("\n\n------------随机产生人种了-----------------" + i);
      Human human = HumanFactory.createHuman();
      human.cry();
      human.laugh();
      human.talk();
    }
  }
예제 #24
0
 @Test
 public void test_DefaultConstructors_and_getters() {
   Human h = new Human();
   assertEquals("", h.getName());
   assertEquals(0, h.getAge());
 }
예제 #25
0
  public static void main(String[] args) {
    // 女娲第一次造人,试验性质,少造点,火候不足,缺陷产品
    System.out.println("------------造出的第一批人是这样的:白人-----------------");
    Human whiteHuman = HumanFactory.createHuman(WhiteHuman.class);
    whiteHuman.cry();
    whiteHuman.laugh();
    whiteHuman.talk();
    // 女娲第二次造人,火候加足点,然后又出了个次品,黑人
    System.out.println("\n\n------------造出的第二批人是这样的:黑人-----------------");
    Human blackHuman = HumanFactory.createHuman(BlackHuman.class);
    blackHuman.cry();
    blackHuman.laugh();
    blackHuman.talk();
    // 第三批人了,这次火候掌握的正好,黄色人种(不写黄人,免得引起歧义),备注:RB人不属于此列
    System.out.println("\n\n------------造出的第三批人是这样的:黄色人种-----------------");
    Human yellowHuman = HumanFactory.createHuman(YellowHuman.class);
    yellowHuman.cry();
    yellowHuman.laugh();
    yellowHuman.talk();

    // 女娲烦躁了,爱是啥人种就是啥人种,烧吧
    for (int i = 0; i < 10; i++) {
      System.out.println("\n\n------------随机产生人种了-----------------" + i);
      Human human = HumanFactory.createHuman();
      human.cry();
      human.laugh();
      human.talk();
    }
  }
예제 #26
0
  @Test
  public void test() {

    Human buyer = new Human();
    Human seller = new Human();

    seller.createConsumable();

    final BigDecimal totalMoney = Money.MoneySupply.getTotalMoney();
    final BigDecimal buyerWorth = buyer.getMonetaryWorth();
    final BigDecimal sellerWorth = seller.getMonetaryWorth();
    final int numSellerConsumables = seller.getNumConsumables();
    final int numBuyerConsumables = buyer.getNumConsumables();

    IConsumable product = seller.getItemsForSale().get(0);
    final int consumableId = product.getId();
    final BigDecimal price = product.getPrice();

    TransactionTerms transactionTerms =
        new TransactionTerms(price.multiply(new BigDecimal(2)), consumableId);
    TransactionAgreement agreement = buyer.proposeTransaction(seller, transactionTerms);

    new Transaction(buyer, seller, agreement);

    final BigDecimal totalMoneyAfterTrans = Money.MoneySupply.getTotalMoney();
    final BigDecimal buyerPostWorth = buyer.getMonetaryWorth();
    final BigDecimal sellerPostWorth = seller.getMonetaryWorth();

    Assert.assertEquals(totalMoney, totalMoneyAfterTrans);
    Assert.assertEquals(buyerWorth.subtract(price.multiply(new BigDecimal(2))), buyerPostWorth);
    Assert.assertEquals(sellerWorth.add(price.multiply(new BigDecimal(2))), sellerPostWorth);
    Assert.assertEquals(numSellerConsumables - 1, seller.getNumConsumables());
    Assert.assertEquals(numBuyerConsumables + 1, buyer.getNumConsumables());
  }
예제 #27
0
  @Test
  public void testInsufficientFundsTransaction() {

    Human buyer = new Human();
    Human seller = new Human();

    seller.createConsumable();

    final BigDecimal buyerWorthPreTransaction = buyer.getMonetaryWorth();
    final BigDecimal transactionValue = buyerWorthPreTransaction.add(new BigDecimal(5.0));
    final int numSellerConsumables = seller.getNumConsumables();
    final int numBuyerConsumables = buyer.getNumConsumables();

    int consumableId = seller.getItemsForSale().get(0).getId();

    TransactionTerms transactionTerms = new TransactionTerms(transactionValue, consumableId);
    TransactionAgreement agreement = buyer.proposeTransaction(seller, transactionTerms);

    new Transaction(buyer, seller, agreement);

    Assert.assertEquals(buyerWorthPreTransaction, buyer.getMonetaryWorth());
    Assert.assertEquals(numSellerConsumables, seller.getNumConsumables());
    Assert.assertEquals(numBuyerConsumables, buyer.getNumConsumables());
  }
예제 #28
0
 @Test
 public void test_equals_equal1() {
   Human h1 = new Human("Adam", 20);
   Human h2 = new Human("Adam", 20);
   assertTrue(h1.equals(h2));
 }
예제 #29
0
 @Test
 public void test_TwoArgConstructor_and_getters() {
   Human h = new Human("Adam", 20);
   assertEquals("Adam", h.getName());
   assertEquals(20, h.getAge());
 }
예제 #30
0
 @Test
 public void test_equals_equal2() {
   Human h1 = new Human();
   Human h2 = new Human();
   assertTrue(h1.equals(h2));
 }