Skip to content

Instantly share code, notes, and snippets.

@CasonBarnhill
Created December 4, 2015 20:33
Show Gist options
  • Save CasonBarnhill/6d4606b29ca5ca7e2956 to your computer and use it in GitHub Desktop.
Save CasonBarnhill/6d4606b29ca5ca7e2956 to your computer and use it in GitHub Desktop.
week-5-day-3 Assignment
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace week5day3
{
class LibraryManager
{
public LibraryManager()
{
checkedOutBooks = new List<Library>();
}
public List<Library> checkedOutBooks;
public Student SaveStudent(Student s)
{
s.Age = (int)(DateTime.Now.Subtract(s.dob).TotalDays / 365);
return s;
}
public void CreateBook()
{
}
public Library CheckOutBook(Book b, Student s, DateTime borrowdate)
{
var result = new Library();
if (checkedOutBooks.Any())
{
result.Id = checkedOutBooks.Max(x => x.Id) + 1;
}
else
{
result.Id = 1;
}
result.Book = b;
result.Student = s;
result.BorrowDate = borrowdate;
result.ExpectedReturnDate = borrowdate.AddMonths(1);
checkedOutBooks.Add(result);
return result;
}
internal object AddStudent(Student s)
{
throw new NotImplementedException();
}
internal Library CheckInBook(Book b, DateTime returndate)
{
var result = checkedOutBooks.FirstOrDefault(x => x.Book.Id == b.Id && x.ExpectedReturnDate != null);
result.ExpectedReturnDate = null;
result.ActualReturnDate = returndate;
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace week5day3
{
class Library
{
internal object ExpectedReturnDate;
public object BorrowDate { get; internal set; }
public DateTime? ActualReturnDate { get; internal set; }
public int Id { get; internal set; }
public Book Book { get; internal set; }
public Student Student { get; internal set; }
public static implicit operator Library(LibraryManager v)
{
throw new NotImplementedException();
}
internal Library CheckOutBook(Student s, Book b)
{
throw new NotImplementedException();
}
}
}
namespace week5day3
{
internal class Book
{
public int Id { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
}
}
using System;
namespace week5day3
{
public class Student
{
public DateTime dob;
public int Id { get; set; }
public int Age { get; internal set; }
public string Name { get; internal set; }
}
}
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace week5day3
{
[TestClass]
public class LibraryManagerTests
{
// When adding a student, their age is calculated correctly
[TestMethod]
public void CorrectStudentAge()
{
var s = new Student();
s.Name = "Jo";
var expectedAge = 27;
var daysInYear = 365;
s.dob = DateTime.Now.AddDays(-daysInYear * expectedAge);
LibraryManager mgr = new LibraryManager();
var savedStudent = mgr.SaveStudent(s);
Assert.AreEqual(expectedAge, savedStudent.Age);
}
//When checking out a book the check out date is set correctly and the expected return date is set to 1 month after the check out date.
[TestMethod]
public void CheckedOoutBookDatesSetCorrectly()
{
var s = new Student();
var b = new Book();
var checkOutDate = DateTime.Parse("5/5/2000");
LibraryManager mgr = new LibraryManager();
Library result = mgr.CheckOutBook(b, s, checkOutDate);
Assert.AreEqual(checkOutDate, result.BorrowDate);
Assert.AreEqual(checkOutDate.AddMonths(1), result.ExpectedReturnDate);
}
//When a book is checked in - the actual return date is the right day and the expected return date is set to null.
[TestMethod]
public void CheckedInBookDatesSetCorrectly()
{
var s = new Student();
var b = new Book();
LibraryManager mgr = new LibraryManager();
mgr.CheckOutBook(b, s, DateTime.Parse("5/5/2000"));
Library result2 = mgr.CheckInBook(b, DateTime.Parse("5/9/2000"));
Assert.AreEqual(DateTime.Parse("5/5/2000"), result2.BorrowDate);
Assert.IsNull(result2.ExpectedReturnDate);
Assert.AreEqual(DateTime.Parse("5/9/2000"), result2.ActualReturnDate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment