C
C#โ€ข11mo ago
tj

Error help

Hey there, been at this for a while now and for the life of me cannot figure out why it isn't passing these tests, any help would be appreciated. GitHub: https://github.com/Fallexs/CKK.Logic/tree/master/CKK.Logic Errors: https://pastebin.com/CQdTkbG4
GitHub
CKK.Logic/CKK.Logic at master ยท Fallexs/CKK.Logic
Contribute to Fallexs/CKK.Logic development by creating an account on GitHub.
Pastebin
Output CKK.Logic -> /home/codegrade/student/CKK.Logic/bin/Debug/ne...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
69 Replies
Angius
Angiusโ€ข11mo ago
The errors are quite clear Well, warnings, really
tj
tjOPโ€ข11mo ago
They are clear and tell me whats wrong but every time I try to edit it to fix it, it adds more or doesn't pass
Angius
Angiusโ€ข11mo ago
Uh, why is your UnitTest1.cs a git diff and not a normal C# file...?
Pobiega
Pobiegaโ€ข11mo ago
also, please add a .gitignore file with default .NET settings in it
Angius
Angiusโ€ข11mo ago
Also, this file /home/codegrade/student/CKK.Tests/StoreTests.cs just straight up isn't in your repo
Pobiega
Pobiegaโ€ข11mo ago
dotnet new gitignore
tj
tjOPโ€ข11mo ago
Those tests were for something old and aren't used anymore, just forgot to get rid of them. OH the code grade tests aren't in the repo They are separated in a VM in codegrade that are tested against my code
Pobiega
Pobiegaโ€ข11mo ago
public ShoppingCartItem? GetProductById(int id) {
if ( id < 0 ) {
throw new InvalidIdException();
} else {
var Existing = (
from product in Products
where id == product.Product.Id
select product);
if ( Existing.Any() ) {
foreach(var product in Products) {
return product;
}
} else {
return null;
}
return Existing.Single();
}
}
public ShoppingCartItem? GetProductById(int id) {
if ( id < 0 ) {
throw new InvalidIdException();
} else {
var Existing = (
from product in Products
where id == product.Product.Id
select product);
if ( Existing.Any() ) {
foreach(var product in Products) {
return product;
}
} else {
return null;
}
return Existing.Single();
}
}
:d
Angius
Angiusโ€ข11mo ago
The first error seems to be about this one, yes The test expects null but is given a value
Pobiega
Pobiegaโ€ข11mo ago
linq query syntax, bad formatting, returning inside a foreach... no early returns
Angius
Angiusโ€ข11mo ago
And PascalCase local vars, don't forget when
tj
tjOPโ€ข11mo ago
Sorry I've just been going off what I've been taught if there's a better way to query or return I'd appreciate any help
Pobiega
Pobiegaโ€ข11mo ago
public ShoppingCartItem? GetProductById(int id)
{
if (id < 0)
{
throw new InvalidIdException();
}

return Products.FirstOrDefault(product => id == product.Product.Id);
}
public ShoppingCartItem? GetProductById(int id)
{
if (id < 0)
{
throw new InvalidIdException();
}

return Products.FirstOrDefault(product => id == product.Product.Id);
}
Angius
Angiusโ€ข11mo ago
public ShoppingCartItem? GetProductById(int id)
{
if ( id < 0 ) {
throw new InvalidIdException();
}

return Products
.Select(p => p.Product)
.SingleOrDefault(p => p.Product.Id == id); // will return `null` when it can't find anything
}
public ShoppingCartItem? GetProductById(int id)
{
if ( id < 0 ) {
throw new InvalidIdException();
}

return Products
.Select(p => p.Product)
.SingleOrDefault(p => p.Product.Id == id); // will return `null` when it can't find anything
}
tj
tjOPโ€ข11mo ago
Let me try this and see if it fixes the error also
Pobiega
Pobiegaโ€ข11mo ago
not sure about that select? it seems to extract the product, not the shoppingcartitem
tj
tjOPโ€ข11mo ago
Is there an integrated console in Virtual Studio where I run this or just any terminal inside the folder path
Angius
Angiusโ€ข11mo ago
select is in the original query so ยฏ\_(ใƒ„)_/ยฏ
Pobiega
Pobiegaโ€ข11mo ago
the root folder of your project
Angius
Angiusโ€ข11mo ago
I never used query syntax thoug, so idk
Pobiega
Pobiegaโ€ข11mo ago
nah thats just confusing naming. there is no select.
tj
tjOPโ€ข11mo ago
As in where the .sln is located?
Pobiega
Pobiegaโ€ข11mo ago
sure
tj
tjOPโ€ข11mo ago
bet gimme 2 seconds to update it see if it fixes the error
Pobiega
Pobiegaโ€ข11mo ago
dunno if it will tbh, but it does pretty much the same as you have now you might want to use SingleOrDefault instead of FirstOrDefault, its technically more correct.
tj
tjOPโ€ข11mo ago
Pastebin
Output CKK.Logic -> /home/codegrade/student/CKK.Logic/bin/Debug/ne...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
tj
tjOPโ€ข11mo ago
It fixed one of them but I don't understand the others, are the methods not already doing that?
Pobiega
Pobiegaโ€ข11mo ago
well without the code for the tests we can only guess what its actually testing
tj
tjOPโ€ข11mo ago
Yea thats the big struggle i've been having with most of this Its trial and error to find what niche thing it wants me to do I can't tell if
CKK.Tests.ShoppingCartTests.FindStoreItemById_ShouldReturnEmptyStoreItem [FAIL]
CKK.Tests.ShoppingCartTests.FindStoreItemById_ShouldReturnEmptyStoreItem [FAIL]
is not returning null or if its testing an item to find that should be returning null
Pobiega
Pobiegaโ€ข11mo ago
Was expecting null but was given a value. tells me that you are returning a value when it was.. expecting a null but the name is weird ShouldReturnEmptyStoreItem is not the same as ShouldReturnNull if you ask me. but thats what the error seems to indicatre
Angius
Angiusโ€ข11mo ago
Failed CKK.Tests.ShoppingCartTests.RemoveProduct_ShouldRemoveIfQuantityIsNegative [< 1 ms] , meanwhile, should be about removing items when RemoveProduct() method is given a negative amount
Pobiega
Pobiegaโ€ข11mo ago
ah yes
tj
tjOPโ€ข11mo ago
I'm assuming its because the other error
CKK.Tests.ShoppingCartTests.RemoveProduct_ShouldRemoveIfQuantityIsNegative [FAIL]
CKK.Tests.ShoppingCartTests.RemoveProduct_ShouldRemoveIfQuantityIsNegative [FAIL]
Isn't removing the item correctly and returning something?
Pobiega
Pobiegaโ€ข11mo ago
so, I cleaned up your removeproduct but have not "optimized" it yet
public ShoppingCartItem RemoveProduct(int id, int quant)
{
var existing = Products.Where(product => id == product.Product.Id);

if (quant < 0)
{
throw new ArgumentOutOfRangeException(nameof(quant), "Invalid Quantity.");
}

if (!existing.Any())
{
throw new ProductDoesNotExistException();
}

foreach (ShoppingCartItem product in existing)
{
product.Quantity -= quant;
if (product.Quantity > 0)
{
return product;
}
else
{
Products.Remove(product);
return new ShoppingCartItem();
}
}

return existing.Single();
}
public ShoppingCartItem RemoveProduct(int id, int quant)
{
var existing = Products.Where(product => id == product.Product.Id);

if (quant < 0)
{
throw new ArgumentOutOfRangeException(nameof(quant), "Invalid Quantity.");
}

if (!existing.Any())
{
throw new ProductDoesNotExistException();
}

foreach (ShoppingCartItem product in existing)
{
product.Quantity -= quant;
if (product.Quantity > 0)
{
return product;
}
else
{
Products.Remove(product);
return new ShoppingCartItem();
}
}

return existing.Single();
}
we can easily see that you throw an exception on quant < 0 now
tj
tjOPโ€ข11mo ago
but it checks for
if (product.Quantity > 0) {
return product;
} else {
Products.Remove(product);
return new ShoppingCartItem();
}
if (product.Quantity > 0) {
return product;
} else {
Products.Remove(product);
return new ShoppingCartItem();
}
Should I not be returning a new item here?
Angius
Angiusโ€ข11mo ago
Seems like you're throwing on negative value, but the test expects it to be a valid value?
tj
tjOPโ€ข11mo ago
In the instructions I was told
Add Exceptions to the following Classes:
Entity
In the Id setter: If the value given is an invalid number (id less than 0), throw an InvalidIdException
InventoryItem
In the Quantity setter: If the value given is an invalid number (less than 0), throw InventoryItemStockTooLowException
Note: Changing InventoryItem will effect all child classes that derive from it
Product
In the Price setter: If the value given is an invalid price (less than 0), throw ArgumentOutOfRangeException
ShoppingCart
AddProduct(): If the quantity given is less than or equal to 0, throw InventoryItemStockTooLowException
RemoveProduct(): If the quantity is less than 0, throw ArgumentOutOfRangeException
RemoveProduct(): If the product does not exist, throw ProductDoesNotExistException
GetProductById(): If Id is invalid (less than 0), throw InvalidIdException
Store
AddStoreItem(): If the quantity given is less than or equal to 0, throw InventoryItemStockTooLowException
RemoveStoreItem(): If the quantity is less than 0, throw ArgumentOutOfRangeException
RemoveStoreItem(): If the product does not exist, throw ProductDoesNotExistException
FindStoreItemById(): If Id is invalid (less than 0), throw InvalidIdException
Add Exceptions to the following Classes:
Entity
In the Id setter: If the value given is an invalid number (id less than 0), throw an InvalidIdException
InventoryItem
In the Quantity setter: If the value given is an invalid number (less than 0), throw InventoryItemStockTooLowException
Note: Changing InventoryItem will effect all child classes that derive from it
Product
In the Price setter: If the value given is an invalid price (less than 0), throw ArgumentOutOfRangeException
ShoppingCart
AddProduct(): If the quantity given is less than or equal to 0, throw InventoryItemStockTooLowException
RemoveProduct(): If the quantity is less than 0, throw ArgumentOutOfRangeException
RemoveProduct(): If the product does not exist, throw ProductDoesNotExistException
GetProductById(): If Id is invalid (less than 0), throw InvalidIdException
Store
AddStoreItem(): If the quantity given is less than or equal to 0, throw InventoryItemStockTooLowException
RemoveStoreItem(): If the quantity is less than 0, throw ArgumentOutOfRangeException
RemoveStoreItem(): If the product does not exist, throw ProductDoesNotExistException
FindStoreItemById(): If Id is invalid (less than 0), throw InvalidIdException
It was super confusing because it wants me to throw exceptions in the setter of properties and also in the methods so most of the time it wouldn't even reach the method exception because it caught it in the property setter. if im understanding correctly Ill try this and report ๐Ÿซก
Pobiega
Pobiegaโ€ข11mo ago
no read what I said nothing has been changed functionality wise I just cleaned it up to make it readable
tj
tjOPโ€ข11mo ago
ah I saw that you removed the linq query and thought it was a possible fix
Angius
Angiusโ€ข11mo ago
It wasn't removed
Pobiega
Pobiegaโ€ข11mo ago
I just rewrote it with method syntax which is preferred by 99% of C# developers
tj
tjOPโ€ข11mo ago
Ohh sorry im unfamiliar with that way
Pobiega
Pobiegaโ€ข11mo ago
I'd suggest you learn it. its considered standard ๐Ÿ™‚ hm
tj
tjOPโ€ข11mo ago
Appreciate the help, it does look way better than my other query lol
Pobiega
Pobiegaโ€ข11mo ago
If the product does not exist, throw ProductDoesNotExistException
this can be interpreted in two ways one: no product exists with that id exists in the list of known products two: no cart item with a product with that id exists in this cart
tj
tjOPโ€ข11mo ago
In my Store.cs I have pretty much the exact same Method but it doesnt remove the item from the list when the quantity is below 0 so the product should always exist.
Pobiega
Pobiegaโ€ข11mo ago
well my point stands since we cant see the tests, we dont know what they are testing for either of those interpretations are valid if you ask me
tj
tjOPโ€ข11mo ago
Yea im stumped tbh idk if its like 1. List is wrong 2. Remove method is wrong 3. Product is wrong because im definitely checking for positive Quantity and removing if not
Pobiega
Pobiegaโ€ข11mo ago
are you sure returning an empty cart item is correct? seems odd that it can even exist if you ask me it has no product associated with it, for one
public ShoppingCartItem RemoveProduct(int id, int quant)
{
// prevents bad input
if (quant < 0)
{
throw new ArgumentOutOfRangeException(nameof(quant), "Invalid Quantity.");
}

var existing = Products
.SingleOrDefault(product => id == product.Product.Id);

// no cart item exists with that id
if (existing is null)
{
throw new ProductDoesNotExistException();
}

existing.Quantity -= quant;

if (existing.Quantity > 0)
{
return existing;
}

Products.Remove(existing);
return null; // ?
}
public ShoppingCartItem RemoveProduct(int id, int quant)
{
// prevents bad input
if (quant < 0)
{
throw new ArgumentOutOfRangeException(nameof(quant), "Invalid Quantity.");
}

var existing = Products
.SingleOrDefault(product => id == product.Product.Id);

// no cart item exists with that id
if (existing is null)
{
throw new ProductDoesNotExistException();
}

existing.Quantity -= quant;

if (existing.Quantity > 0)
{
return existing;
}

Products.Remove(existing);
return null; // ?
}
this is how I'd re-write your remove method but in general I think your cart has issues why is the list of cart items called Products when it contains cart items, not products?
tj
tjOPโ€ข11mo ago
๐Ÿคทโ€โ™‚๏ธ You're asking the same person who wrote the grading errors, half of this project I was taught something, told to implement it, then told to rewrite all of it with major inconsistencies once I was taught something new. Your guess is as good as mine
Pobiega
Pobiegaโ€ข11mo ago
๐Ÿ˜ฆ
AddProduct(): If the quantity given is less than or equal to 0, throw InventoryItemStockTooLowException
wait what
tj
tjOPโ€ข11mo ago
You should see the earlier versions of this lol my Shopping cart was a Method that had 3 spots and checked for null on first spot and if it wasn't then moved to the second one and so on, only holding 3 items.
Pobiega
Pobiegaโ€ข11mo ago
that exception indicates that your cart should be updating the inventory?!
tj
tjOPโ€ข11mo ago
The instructions and what im told to put are so confusing I'll probably delete all of this in the next assignment and do it another way anyway
Pobiega
Pobiegaโ€ข11mo ago
yeah don't envy your situation, having to code with poor specifications and against hidden tests is not fun
tj
tjOPโ€ข11mo ago
It's so weird because they teach me like the bottom barrel way to do something which is cool if i'm just starting out and learning it but then to have to delete and redo everything because oh wow heres a better way we held out on you because who knows It's like why not just teach me something and then keep building on it so I have a path im following and understand
Pobiega
Pobiegaโ€ข11mo ago
well as someone who has taught a lot of people how to code... its not possible to jump directly from beginner to "modern code" in most cases
tj
tjOPโ€ข11mo ago
Still returning same errors
Pobiega
Pobiegaโ€ข11mo ago
I mean yeah I'm not fixing any issues I just re-wrote your code I dont even know what the issues are, because again, I don't know what the test is testing
tj
tjOPโ€ข11mo ago
Yea but thats also what I mean like, teaching beginner stuff is fine but have a way to flow or lead into a better way to do it and build upon it instead of just ok now forget that way because we've upgraded you to a whole new different path and 100 new syntax
Pobiega
Pobiegaโ€ข11mo ago
I mean, yeah, but also.. thats hard ๐Ÿ™‚ if you've only learned if statements, you can do quite a bit, its just not the ideal way as you see here with your code vs mine but at the same time, its very important to understand if really really well building something that looks and behaves like a real project as early as possible is an admirable goal, even if it means you have to unlearn some stuff later on its a tradeoff for sure, and the specifications for this project seems to be very poor
tj
tjOPโ€ข11mo ago
I guess it wouldn't be as bad without these god awful instructions and ambiguous grading params because idk if im doing anything correctly and end up rethinking everything for oh maybe if i do this it'll work because I don't fully understand what its asking
Pobiega
Pobiegaโ€ข11mo ago
these god awful instructions and ambiguous grading params
I think you hit the nail on the head. I love automated test suites to verify that the code is correct, but here its really hard to guess what they mean
tj
tjOPโ€ข11mo ago
Yea any tests against my code user made or automated are super helpful, I just wish it would tell me if a certain line went wrong, had better error names, or showed the tests being run against it
Pobiega
Pobiegaโ€ข11mo ago
I don't suppose you can ask your teacher to clarify the instructions regarding where the confusion is?
tj
tjOPโ€ข11mo ago
I did yesterday with no response and again 3 hours ago with no response ๐Ÿ™‚
Pobiega
Pobiegaโ€ข11mo ago
:/
tj
tjOPโ€ข11mo ago
Appreciate the help anyway we got a couple errors to go away lol ๐Ÿ™
Pobiega
Pobiegaโ€ข11mo ago
yeah sure, np. try to look at how my methods work and how ๐Ÿ™‚ for example, any if statement that contains a return statement doesnt need an else you can just add the "else" code below the if your loops also dont make much sense, you only needed them becasue your queries were fetching multiple items instead of a single, but its clear from the method signature that a single item (or null) was expected
Want results from more Discord servers?
Add your server