Artificial intelligence robot in java a prototype
/**
* @author Consumerfed I T Section
*
*/
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
}
}
/**
*
*/
package com.consumerfed.airobo.model;
import com.consumerfed.airobo.utilities.RoboMessages;
/**
* @author Conumerfed I T Section
*
*/
public class AIRobot {
private boolean alert = false;
private RoboMessages message = null;
private float batteryStatus = 0;
private float weightCarried = 0;
private float distanceCovered = 0;
public float getBatteryStatus() {
return batteryStatus;
}
public void setBatteryStatus(float batteryStatus) {
this.batteryStatus = batteryStatus;
}
public RoboMessages getMessage() {
if (null == message) {
message = RoboMessages.NILL;
}
return message;
}
public void setMessage(RoboMessages message) {
this.message = message;
}
public float getWeightCarried() {
return weightCarried;
}
public void setWeightCarried(float weightCarried) {
this.weightCarried = weightCarried;
}
public boolean isAlert() {
return alert;
}
public void setAlert(boolean alert) {
this.alert = alert;
}
public float getDistanceCovered() {
return distanceCovered;
}
public void setDistanceCovered(float distanceCovered) {
this.distanceCovered = distanceCovered;
}
}
package consumerfed.xebia.airobo.model;
/**
* @author Conumerfed I T Section
*
*/
public class Barcode {
private String code = null;
private byte[] img = null;
private double price = 0;
public byte[] getImg() {
return img;
}
public void setImg(byte[] img) {
this.img = img;
}
public String getCode() {
return "ISBN 8281808025";
}
public double getPrice() {
return 120.75;
}
}
/**
* @author Consumerfed I T Section
*
*/
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
}
}
/**
*
*/
package com.consumerfed.airobo.model;
import com.consumerfed.airobo.utilities.RoboMessages;
/**
* @author Conumerfed I T Section
*
*/
public class AIRobot {
private boolean alert = false;
private RoboMessages message = null;
private float batteryStatus = 0;
private float weightCarried = 0;
private float distanceCovered = 0;
public float getBatteryStatus() {
return batteryStatus;
}
public void setBatteryStatus(float batteryStatus) {
this.batteryStatus = batteryStatus;
}
public RoboMessages getMessage() {
if (null == message) {
message = RoboMessages.NILL;
}
return message;
}
public void setMessage(RoboMessages message) {
this.message = message;
}
public float getWeightCarried() {
return weightCarried;
}
public void setWeightCarried(float weightCarried) {
this.weightCarried = weightCarried;
}
public boolean isAlert() {
return alert;
}
public void setAlert(boolean alert) {
this.alert = alert;
}
public float getDistanceCovered() {
return distanceCovered;
}
public void setDistanceCovered(float distanceCovered) {
this.distanceCovered = distanceCovered;
}
}
package consumerfed.xebia.airobo.model;
/**
* @author Conumerfed I T Section
*
*/
public class Barcode {
private String code = null;
private byte[] img = null;
private double price = 0;
public byte[] getImg() {
return img;
}
public void setImg(byte[] img) {
this.img = img;
}
public String getCode() {
return "ISBN 8281808025";
}
public double getPrice() {
return 120.75;
}
}
package com.Consumerfed.airobo.service;
import com.Consumerfed.airobo.model.AIRobot;
import com.Consumerfed.airobo.model.Barcode;
import com.Consumerfed.airobo.utilities.AIRobotUtilities;
import com.Consumerfed.airobo.utilities.RoboMessages;
/**
* @author Consumerfed
*
*/
public class AIRobotService implements AIRobotServiceInf, Scanning {
private AIRobot robot = null;
public AIRobotService(AIRobot robot) {
this.robot = robot;
}
@Override
public float walk(float kilometer) {
if (loadWeight(robot.getWeightCarried()) && checkBatteryStatus() && checkMaxDistance(kilometer)) {
if(isChargeExist(kilometer)) {
batteryConsumption(kilometer, robot.getWeightCarried());
float distanceCovered = robot.getDistanceCovered();
robot.setDistanceCovered(kilometer+ distanceCovered);
}
}
checkBatteryStatus();
display();
return robot.getBatteryStatus();
}
private boolean isChargeExist(float kilometer) {
boolean isChargeExistForMove = false;
float weight = robot.getWeightCarried();
float extraChargeConsumed = weight * AIRobotUtilities.EXTRA_REDUCE_PKM_WGT;
float chargeConsume = kilometer * (AIRobotUtilities.BATTERY_CONSUMPTION_PKM + extraChargeConsumed);
float charge = robot.getBatteryStatus();
if((charge)>= chargeConsume) {
isChargeExistForMove = true;
}
return isChargeExistForMove;
}
private void batteryConsumption(float kilometer, float weight) {
float charge = robot.getBatteryStatus();
float extraChargeConsumed = weight * AIRobotUtilities.EXTRA_REDUCE_PKM_WGT;
float chargeConsumed = kilometer * (AIRobotUtilities.BATTERY_CONSUMPTION_PKM + extraChargeConsumed);
float remainingCharge = charge - chargeConsumed;
robot.setBatteryStatus(remainingCharge < 0 ? 0 : remainingCharge);
}
@Override
public float walkWithWeight(float kilometer, float kilogram) {
if (loadWeight(kilogram))
walk(kilometer);
return robot.getBatteryStatus();
}
@Override
public boolean loadWeight(float kilogram) {
float weightCarried = robot.getWeightCarried();
float totalWeight = weightCarried + kilogram;
boolean isNormalWeight = true;
if (totalWeight > 10) {
robot.setMessage(RoboMessages.OVERWEIGHT);
isNormalWeight = false;
}
robot.setWeightCarried(totalWeight);
return isNormalWeight;
}
@Override
public void chargeBattery() {
if (robot.getBatteryStatus() == 100) {
robot.setMessage(RoboMessages.FULL_CHARGE);
robot.setAlert(false);
display();
} else {
System.out.println(" Battery charging..");
try {
Thread.sleep(2512);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" Fully charged ");
}
robot.setDistanceCovered(0);
robot.setBatteryStatus(100);
}
@Override
public void removeLoad() {
if (robot.getWeightCarried() == 0) {
robot.setMessage(RoboMessages.EMPTY_LOAD);
robot.setAlert(false);
display();
}
robot.setWeightCarried(0);
}
private boolean checkBatteryStatus() {
float batteryCharge = robot.getBatteryStatus();
if (batteryCharge == 0) {
robot.setMessage(RoboMessages.EMPTY_BATTERY);
robot.setAlert(true);
return false;
} else if (batteryCharge < 15) {
robot.setMessage(RoboMessages.RESEVE_BATTERY);
robot.setAlert(true);
return false;
}
return true;
}
private boolean checkMaxDistance(float kilometer) {
if (calcMaxMilege(kilometer) < kilometer) {
robot.setMessage(RoboMessages.MAX_DISTANCE_EXCEED);
return false;
}
return true;
}
private float calcMaxMilege(float kilometer) {
float weight = robot.getWeightCarried();
float maxMilege = 100
/ (AIRobotUtilities.BATTERY_CONSUMPTION_PKM + (AIRobotUtilities.EXTRA_REDUCE_PKM_WGT * weight));
return maxMilege;
}
private void display() {
System.out.println("Light on Robot head : \t" + (robot.isAlert() ? "Red" : " "));
System.out.println("Led display on Robot chest : \t" + robot.getMessage().getMessage());
System.out.println("Battery Remaining :\t" + robot.getBatteryStatus());
}
@Override
public double scan(Barcode barcode) {
double price = 0;
System.out.println("scanning");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (checkBarcodeImg(barcode)) {
price = barcode.getPrice();
System.out.println("Price display on Robot : \t" + price);
} else {
robot.setMessage(RoboMessages.SCAN_FAILED);
display();
}
return price;
}
/**
* @param barcode
* @return
*/
private boolean checkBarcodeImg(Barcode barcode) {
// if(barcode.getImg())
return true;
}
}
package com.consumerfed.airobo.service;
public interface AIRobotServiceInf {
public float walk(float kilometer);
public float walkWithWeight(float kilometer, float kilogram);
public boolean loadWeight(float kilogram);
public void chargeBattery();
public void removeLoad();
}
/**
*
*/
package com.consumerfed.airobo.service;
import com.consumerfed.airobo.model.Barcode;
/**
* @author consumerfed Information Technology
*
*/
public interface Scanning {
public double scan(Barcode barcode);
}
package com.cosumerfed.airobo.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.consumerfed.airobo.model.AIRobot;
import com.consumerfed.airobo.model.Barcode;
import com.consumerfed.airobo.service.AIRobotService;
import com.consumerfed.airobo.service.AIRobotServiceInf;
import com.consumerfed.airobo.service.Scanning;
public class AIRobotTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
// System.setOut("");
}
// Robot carries 12 KG
@Test
public void OverweightTest() {
AIRobot robot = new AIRobot();
AIRobotServiceInf tester = new AIRobotService(robot);
assertEquals(false, tester.loadWeight(12));
}
@Test
public void NormalTest() {
AIRobot robot = new AIRobot();
AIRobotServiceInf tester = new AIRobotService(robot);
assertTrue(tester.loadWeight(9.5f));
}
// Robot walks 3.5 kilometers
@Test
public void WalkBatteryValidTest() {
AIRobot robot = new AIRobot();
AIRobotServiceInf tester = new AIRobotService(robot);
tester.chargeBattery();
assertEquals(30.0, tester.walk(3.5f), 0.0001);
}
@Test
public void WalkWithoutCharingTest() {
AIRobot robot = new AIRobot();
AIRobotServiceInf tester = new AIRobotService(robot);
assertEquals(00.0, tester.walk(3.5f), 0.0001);
}
// Robot walks for 2Km carrying 3 kg
@Test
public void WalkWithWeighValidTest() {
AIRobot robot = new AIRobot();
AIRobotServiceInf tester = new AIRobotService(robot);
tester.chargeBattery();
assertEquals(36.0, tester.walkWithWeight(2, 3), 0.0001);
}
@Test
public void robotScanTest() {
Barcode barcode = new Barcode();
AIRobot robot = new AIRobot();
Scanning tester = new AIRobotService(robot);
assertEquals(120.75, tester.scan(barcode), 0.0001);
}
@Test
public void successiveWalkTest() {
AIRobot robot = new AIRobot();
AIRobotServiceInf tester = new AIRobotService(robot);
tester.chargeBattery();
tester.walk(3);
assertEquals(0.0, tester.walk(2), 0.0001);
}
@Test
public void walkAboveMaxDistanceTest() {
AIRobot robot = new AIRobot();
AIRobotServiceInf tester = new AIRobotService(robot);
tester.chargeBattery();
assertEquals(100.0, tester.walk(6), 0.0001);
}
}
package com.consumerfed.airobo.utilities;
public class AIRobotUtilities {
public static final float MAX_MILEAGE = 5;
// BATTERY RESERVICE IN PERCENAGE
public static final float BATTERY_RESERVE = 15;
// EXTRA PERCENTAGE REDUCTION PER KILO METER
public static final float EXTRA_REDUCE_PKM_WGT = 2;
// private battery consumption per kilo meter
public static final float BATTERY_CONSUMPTION_PKM = 20;
// WEIGHT IN KILO GRAM
public static final float MAX_WEIGHT = 10;
}
/**
*
*/
package com.consumerfed.airobo.utilities;
/**
* @author Consumerfed
*
*/
public enum RoboMessages {
OVERWEIGHT("OVERWEIGHT"),
FULL_CHARGE("BATTERY FULL CHARGED"),
EMPTY_LOAD("LOAD IS EMPTY"),
EMPTY_BATTERY("BATTERY IS EMPTY"),
MAX_DISTANCE_EXCEED("MAXIMUM DISTANCE EXCEEDED"),
RESEVE_BATTERY("BATTERY IN RESERVE"),
NILL(""), SCAN_FAILED("SCAN FAILURE");
private String message;
public String getMessage() {
return this.message;
}
private RoboMessages(String message) {
this.message = message;
}
}
No comments:
Post a Comment
Your feedback may help others !!!