public void testBookingPeriod() { int btId = dbType.addNewRecord("newName", "newProducer", 10, 100); //capacity equals 10 hours int bId = dbBattery.addNewRecord("Charged", btId); int sID = dbStation.addNewRecord("newName", "newAddress", "newCountry", "newState"); int bsID = dbStorage.addNewRecord(btId, sID, 20); dbPeriod.addNewRecord(bsID, DateTime.Today, 10,5); // initial =10 custom = 5 period = dbPeriod.getRecord(bsID, DateTime.Today, true); storage = dbStorage.getRecord(bsID, true); DateTime time = new DateTime(); time = DateTime.Today.AddDays(10); DateTime secondTime = DateTime.Today.AddDays(5); try { period = pCalc.getBookingPeriod(storage, time); MPeriod previous = pCalc.getPreviousPeriod(storage, period); Assert.AreEqual(isInPeriod(period, time, storage), true); period = pCalc.getBookingPeriod(storage, secondTime); Assert.AreEqual(isInPeriod(period, secondTime, storage), true); } finally { storage = dbStorage.getRecord(storage.id,true); foreach (MPeriod p in storage.periods) { dbPeriod.deleteRecord(bsID,p.time); } dbStorage.deleteRecord(bsID); dbStation.deleteRecord(sID); dbBattery.deleteRecord(bId); dbType.deleteRecord(btId); } }
//This method returns period for given booking public MPeriod getBookingPeriod(MBatteryStorage storage, DateTime time) { List<MPeriod> periods = dbPeriod.getStoragePeriods(storage.id,true); MPeriod lastPeriod = periods[periods.Count - 1]; if (time.CompareTo(lastPeriod.time) > 0)//if time of booking is later then time of last period { while (time.CompareTo(lastPeriod.time) > 0) //while time of booking is earlier or in the same time then time of last period { lastPeriod = createPeriod(storage);//create new period } } else //if time of booking is before time of last period { for (int x = periods.Count - 1; x >= 1; x--) //for periods from last to first { MPeriod next = periods[x]; //last created period MPeriod curr = periods[x - 1]; //second last period if ((time.CompareTo(curr.time) >= 0) & (time.CompareTo(next.time) < 0)) //if time of booking is later then current and earlier then next period { return curr; } } } return lastPeriod; }
public MPeriod createPeriod(MBatteryStorage storage) { storage = dbStorage.getRecord(storage.id, true); MPeriod newPeriod = new MPeriod(); newPeriod.time = getTime(storage); newPeriod.initBatteryNumber = getInitNumber(storage); dbPeriod.addNewRecord(storage.id, newPeriod.time, newPeriod.initBatteryNumber, newPeriod.bookedBatteryNumber); return newPeriod; }
public bool isInPeriod(MPeriod period, DateTime time, MBatteryStorage storage) { DateTime first = period.time; double hours = (double) storage.type.capacity; DateTime second = period.time.AddHours(hours); if ((time.CompareTo(first) >= 0) & (time.CompareTo(second) < 0)) { return true; } else return false; }
public List<MPeriod> getAllPeriodsAfter(MBatteryStorage storage, MPeriod period) { List<MPeriod> periods = dbPeriod.getStoragePeriods(storage.id, true); bool found = false; List<MPeriod> periodsAfter = new List<MPeriod>(); foreach (MPeriod p in periods) { if (p.time == period.time) { found = true; } if (found) { periodsAfter.Add(p); } } return periodsAfter; }
public MPeriod getNextPeriod(MBatteryStorage storage, MPeriod current) { List<MPeriod> periods = dbPeriod.getStoragePeriods(storage.id, true); int x = 0; bool found = false; MPeriod next = new MPeriod(); while (!found || x < periods.Count) { MPeriod period = periods[x]; if (period.time == current.time) { found = true; try { next = periods[x + 1]; } catch (Exception) { next = createPeriod(storage); } } x++; } return next; }
//This method will return number of batteries of given type for one storage public int getInitNumber(MBatteryStorage storage) { int init = storage.storageNumber; return init; }
//This method adds hours according to the capacity of given battery type public DateTime getTime(MBatteryStorage storage) { int count = storage.periods.Count; DateTime firstPeriod = storage.periods[count-1].time; int capacity = (int) storage.type.capacity; DateTime secondPeriod = firstPeriod.AddHours(capacity); return secondPeriod; }
public MPeriod getPreviousPeriod(MBatteryStorage storage, MPeriod current) { List<MPeriod> periods = dbPeriod.getStoragePeriods(storage.id, true); int x = periods.Count; bool found = false; MPeriod previous = new MPeriod(); while(!found || x>0) { MPeriod period = periods[x-1]; if (period.time == current.time) { found = true; try { previous = periods[x - 2]; } catch (ArgumentOutOfRangeException) { } } x--; } return previous; }
public void TestGetInitNumber() { int btId = dbType.addNewRecord("newName", "newProducer", 10, 100); //capacity equals 10 hours int sID = dbStation.addNewRecord("newName", "newAddress", "newCountry", "newState"); int bsID = dbStorage.addNewRecord(btId, sID, 20); dbPeriod.addNewRecord(bsID, DateTime.Today, 10, 5); // initial =10 custom = 5 future = 1 period = dbPeriod.getRecord(bsID, DateTime.Today, true); storage = dbStorage.getRecord(bsID, true); try { int init = pCalc.getInitNumber(storage); Assert.AreEqual(init, 20); } finally { dbPeriod.deleteRecord(bsID, DateTime.Today); dbStorage.deleteRecord(bsID); dbStation.deleteRecord(sID); dbType.deleteRecord(btId); } }
public void TestGetPeriod() { int btId = dbType.addNewRecord("newName", "newProducer", 10, 100); //capacity equals 10 hours int sID = dbStation.addNewRecord("newName", "newAddress", "newCountry", "newState"); int bsID = dbStorage.addNewRecord(btId, sID, 20); dbPeriod.addNewRecord(bsID, DateTime.Today, 10, 5); // initial =10 custom = 5 future = 1 period = dbPeriod.getRecord(bsID, DateTime.Today, true); storage = dbStorage.getRecord(bsID, true); try { MPeriod firstPeriod = pCalc.createPeriod(storage); storage = dbStorage.getRecord(storage.id, true); MPeriod secondPeriod = pCalc.createPeriod(storage); Assert.AreEqual(DateTime.Today.AddHours(20), secondPeriod.time); Assert.AreEqual(20, secondPeriod.initBatteryNumber); Assert.AreEqual(0, firstPeriod.bookedBatteryNumber); } finally { dbPeriod.deleteRecord(bsID, DateTime.Today); dbPeriod.deleteRecord(bsID, DateTime.Today.AddHours(10)); dbPeriod.deleteRecord(bsID, DateTime.Today.AddHours(20)); dbStorage.deleteRecord(bsID); dbStation.deleteRecord(sID); dbType.deleteRecord(btId); } }