Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Fixed RandomFiller multiple unwanted events being created
Browse files Browse the repository at this point in the history
  • Loading branch information
Soplicow committed Apr 15, 2024
1 parent 1452871 commit c00ac80
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Library/DataLayer/API/IDataFiller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DataLayer.API
{
public interface IDataFiller
{
void Fill(IDataContext context);
}
}
37 changes: 37 additions & 0 deletions Library/DataLayer/Implementations/DataFillers/PresetFiller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using DataLayer.API;
using DataLayer.Implementations.Events;

namespace DataLayer.Implementations
{
public class PresetFiller : IDataFiller
{
public void Fill(IDataContext context)
{
context.Users.Add(new User("John", "Doe", "johndoe@email", 100, 123456789, null));
context.Users.Add(new User("Jane", "Doe", "janedoe@email", 200, 111111111, null));
context.Users.Add(new User("Alice", "Smith", "alicesmith@email", 300, 121212121, null));
context.Users.Add(new User("Bob", "Smith", "bobsmith@email", 400, 987654321, null));
context.Users.Add(new User("Charlie", "Brown", "charliebrown@email", 500, 999999999, null));

context.Products.Add(new Book("Old Man and the Sea", 10, "Ernest Hemingway", "Charles Sons", 300, new DateTime(1952, 1, 1)));
context.Products.Add(new Book("The Great Gatsby", 20, "F. Scott Fitzgerald", "Charles Sons", 400, new DateTime(1925, 1, 1)));
context.Products.Add(new Book("To Kill a Mockingbird", 30, "Harper Lee", "Charles Sons", 500, new DateTime(1960, 1, 1)));
context.Products.Add(new Book("1984", 40, "George Orwell", "Charles Sons", 600, new DateTime(1949, 1, 1)));
context.Products.Add(new Book("Brave New World", 50, "Aldous Huxley", "Charles Sons", 700, new DateTime(1932, 1, 1)));

context.States.Add(new State(context.Products[0], 5));
context.States.Add(new State(context.Products[1], 5));
context.States.Add(new State(context.Products[2], 5));
context.States.Add(new State(context.Products[3], 5));
context.States.Add(new State(context.Products[4], 5));

context.Events.Add(new Borrow(context.Users[0], context.States[0]));
context.Events.Add(new Borrow(context.Users[1], context.States[1]));
context.Events.Add(new Borrow(context.Users[3], context.States[3]));
context.Events.Add(new Return(context.Users[0], context.States[0]));
context.Events.Add(new Return(context.Users[1], context.States[1]));
context.Events.Add(new Return(context.Users[3], context.States[3]));
context.Events.Add(new Delivery(context.Users[4], context.States[4], 10));
}
}
}
158 changes: 158 additions & 0 deletions Library/DataLayer/Implementations/DataFillers/RandomFiller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using DataLayer.API;
using DataLayer.Implementations.Events;

namespace DataLayer.Implementations
{
public class RandomFiller : IDataFiller
{
private Random random = new Random();
public void Fill(IDataContext context)
{
int usersCount = random.Next(5, 11);
for (int i = 0; i < usersCount; i++)
{
context.Users.Add(new User(
GetRandomFirstName(),
GetRandomLastName(),
$"{GetRandomFirstName().ToLower()}{GetRandomLastName().ToLower()}@email.com",
random.Next(100, 1000),
GetRandomPhoneNumber(),
null
));
}

int productsCount = random.Next(5, 11);
for (int i = 0; i < productsCount; i++)
{
context.Products.Add(new Book(
GetRandomTitle(),
random.Next(10, 100),
GetRandomAuthor(),
GetRandomPublisher(),
random.Next(50, 1000),
GetRandomDate()
));
}

int statesCount = context.Products.Count;
for (int i = 0; i < statesCount; i++)
{
context.States.Add(new State(
context.Products[i],
random.Next(1, 10)
));
}

int eventsCount = random.Next(5, 11);
int borrowsCount = 0;
List<IUser> usersBorrowing = new List<IUser>();
List<IState> statesBorrowed = new List<IState>();
for (int i = 0; i < eventsCount; i++)
{
int userIndex = random.Next(context.Users.Count);
int stateIndex = random.Next(context.States.Count);
int randomEvent = random.Next(3);
if (randomEvent == 0)
{
context.Events.Add(new Borrow(context.Users[userIndex], context.States[stateIndex]));
borrowsCount++;
usersBorrowing.Add(context.Users[userIndex]);
statesBorrowed.Add(context.States[stateIndex]);
}
else if (randomEvent == 1 && borrowsCount > 0)
{
context.Events.Add(new Return(usersBorrowing[borrowsCount-1], statesBorrowed[borrowsCount-1]));
borrowsCount--;
usersBorrowing.RemoveAt(borrowsCount);
statesBorrowed.RemoveAt(borrowsCount);
}
else if (randomEvent == 1 && borrowsCount == 0)
{
i--;
}
else if (randomEvent == 2)
{
context.Events.Add(new Delivery(context.Users[userIndex], context.States[stateIndex], random.Next(1, 10)));
}
}
}
private string GetRandomFirstName()
{
string[] firstNames = {
"John", "Jane", "Alice", "Bob", "Charlie",
"Michael", "Emily", "David", "Sarah", "Daniel",
"Matthew", "Jessica", "Andrew", "Jennifer", "James",
"Linda", "William", "Karen", "Joseph", "Maria",
"Robert", "Susan", "Christopher", "Nancy", "Daniel",
"Karen", "Thomas", "Lisa", "Brian", "Margaret",
"Steven", "Betty", "Timothy", "Dorothy", "Kevin",
"Sandra", "Richard", "Ashley", "Mark", "Kimberly",
"Jeffrey", "Donna", "Scott", "Emily", "Charles",
"Carol", "Paul", "Michelle", "Anthony", "Laura"
};
return firstNames[random.Next(firstNames.Length)];
}
private string GetRandomLastName()
{
string[] lastNames = {
"Smith", "Johnson", "Williams", "Brown", "Jones",
"Garcia", "Miller", "Davis", "Rodriguez", "Martinez",
"Hernandez", "Lopez", "Gonzalez", "Wilson", "Anderson",
"Thomas", "Taylor", "Moore", "Jackson", "Martin",
"Lee", "Perez", "Thompson", "White", "Harris",
"Sanchez", "Clark", "Ramirez", "Lewis", "Robinson",
"Walker", "Young", "Allen", "King", "Wright",
"Scott", "Torres", "Nguyen", "Hill", "Flores",
"Green", "Adams", "Nelson", "Baker", "Hall",
"Rivera", "Campbell", "Mitchell", "Carter", "Roberts"
};
return lastNames[random.Next(lastNames.Length)];
}
private int GetRandomPhoneNumber()
{
return random.Next(100000000, 1000000000); // 9-digit random number
}
private string GetRandomTitle()
{
string[] titles = {
"The Catcher in the Rye", "To Kill a Mockingbird", "1984", "The Great Gatsby", "Pride and Prejudice",
"Harry Potter and the Philosopher's Stone", "The Hobbit", "The Lord of the Rings", "The Hunger Games", "The Da Vinci Code",
"The Chronicles of Narnia", "Twilight", "The Alchemist", "The Fault in Our Stars", "The Kite Runner",
"A Song of Ice and Fire", "The Hitchhiker's Guide to the Galaxy", "The Shining", "The Girl with the Dragon Tattoo", "Gone Girl",
"Brave New World", "Animal Farm", "The Adventures of Huckleberry Finn", "Moby-Dick", "Frankenstein",
"Dracula", "Alice's Adventures in Wonderland", "The War of the Worlds", "The Picture of Dorian Gray", "The Bell Jar"
};
return titles[random.Next(titles.Length)];
}
private string GetRandomAuthor()
{
string[] authors = {
"J.D. Salinger", "Harper Lee", "George Orwell", "F. Scott Fitzgerald", "Jane Austen",
"J.K. Rowling", "J.R.R. Tolkien", "George R.R. Martin", "Suzanne Collins", "Dan Brown",
"C.S. Lewis", "Stephenie Meyer", "Paulo Coelho", "John Green", "Khaled Hosseini",
"George R.R. Martin", "Douglas Adams", "Stephen King", "Stieg Larsson", "Gillian Flynn",
"Aldous Huxley", "George Orwell", "Mark Twain", "Herman Melville", "Mary Shelley",
"Bram Stoker", "Lewis Carroll", "H.G. Wells", "Oscar Wilde", "Sylvia Plath"
};
return authors[random.Next(authors.Length)];
}
private string GetRandomPublisher()
{
string[] publishers = {
"Random House", "Penguin Books", "HarperCollins", "Simon & Schuster", "Hachette Livre",
"Macmillan Publishers", "Scholastic Corporation", "Pearson Education", "Bloomsbury Publishing", "Oxford University Press",
"Cambridge University Press", "Wiley", "Springer Nature", "McGraw-Hill Education", "Cengage",
"Houghton Mifflin Harcourt", "Elsevier", "Taylor & Francis", "Harvard University Press", "MIT Press"
};
return publishers[random.Next(publishers.Length)];
}
private DateTime GetRandomDate()
{
// Random date between 1900 and now
DateTime start = new DateTime(1900, 1, 1);
int range = (DateTime.Today - start).Days;
return start.AddDays(random.Next(range));
}
}
}
24 changes: 24 additions & 0 deletions Library/Tests/DataLayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,28 @@ public void DataRepositoryTests()
Assert.IsFalse(dataRepository.GetAllProducts().Contains(product));
Assert.IsFalse(dataRepository.GetAllStates().Contains(state));
}

[TestMethod]
public void DataFillerTests()
{
IDataContext dataContext = new DataContext();
IDataFiller dataFiller = new PresetFiller();

dataFiller.Fill(dataContext);

Assert.AreEqual(5, dataContext.Users.Count);
Assert.AreEqual(5, dataContext.Products.Count);
Assert.AreEqual(5, dataContext.States.Count);
Assert.AreEqual(7, dataContext.Events.Count);

dataContext = new DataContext();
dataFiller = new RandomFiller();

dataFiller.Fill(dataContext);

Assert.IsTrue(dataContext.Users.Count >= 5 && dataContext.Users.Count <= 11);
Assert.IsTrue(dataContext.Products.Count >= 5 && dataContext.Products.Count <= 11);
Assert.AreEqual(dataContext.Products.Count, dataContext.States.Count);
Assert.IsTrue(dataContext.Events.Count >= 5 && dataContext.Events.Count <= 11);
}
}

0 comments on commit c00ac80

Please sign in to comment.