|
I have an Account class, which has a function
double WithDraw(double amountToWithdraw) {
...some code here....
}
If the balance of this account is <= 0, then what should I do?
==> throw an exception (which one?)
==> return 0.0 with some additional information? (in which way?)
I guess:
a) throwing an exception could be wrong, because this scenario is not "exceptional", but standard
b) returning some additional information requires to return a object with additional attributes or a reference to an object in the parameterlist. Seems to be crazy too.
|
|
|
|
|
You could return an instance of a structure instead of a double. This structure could contain any number of fields, which would provide you with unlimited information.
struct TransactionResult
{
double amount;
bool Success;
int ErrorCode;
}
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Frygreen wrote: b) returning some additional information requires to return a object with additional attributes or a reference to an object in the parameterlist. Seems to be crazy too. Why? If you want to communicate "what" went wrong you'll probably need a reference to a resource-text that is translated, and provide the variables.
Frygreen wrote: a) throwing an exception could be wrong, because this scenario is not "exceptional", but standard c) exceptions aren't there to model business cases, but for handling unexpected results in code. Yours isn't.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
The real question is "Is a withdrawal allowed if it results in a negative balance?" and that isn't necessarily a function of that method - for example, my current account has a "permitted overdraft" permanently attached so by balance can genuinely and acceptably go negative, up to a preset limit.
So having a Withdraw method that automatically refuses a transaction if it would result in a negative balance may be against the business logic. In addition, accounts often have "pending transactions" which may not be reflected in the balance but which could change the "approve / reject" decision.
Think about how you are doing this: you should probably divide the responsibilities up into three sections - Presentation (user interface), Business logic, and Data handling - and a withdrawal request would come from the Presentation to the Business for approval, and on then be committed to Data storage by the Business logic if approved, or rejected to the Presentation if not.
Certainly, I don't think an exception would be right - normal processing shouldn't use exceptions for flow control.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Depends on the requirements.
I personally try not to throw exceptions unless it makes sense to do so. You don't have to return additional info.
In this case, overdrafts might be allowed (if they are, you can also specify an overdraft limit, but I leave that as an exercise for you).
public decimal MakeWithdraw(ref decimal balance, decimal amount, bool allowOverdraft=false)
{
decimal widthdrawn = (amount <= balance) ? amount : ((allowOverdraft) ? amount : balance);
balance -= withdrawn;
return widthdrawn;
}
BTW, using decimal instead of double is a good idea because math performed on doubles is not as accurate as math performed on decimals.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
If this is the basis of an "accounting" system, then "transactions" never "fail".
And you don't "withdraw", you post a "debit" (increase in asset accounts / decrease in a liability account; etc.); or "credit".
And each "amount" "posted" to one account, is offset by an entry to another account (i.e. credit cash - debit accounts payable).
At the end of the day, the "reports" indicate who owes what, etc.
(A "mini" blockchain if you will; i.e. "ledger")
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Based on all of your feedback and some additional search I have the following solution for me
I will return a Transaction-Object
struct TransactionResult {
double amountReturned;
EnumTransactionResult eReturn;
}
Some more info:
- I have no complicated scenarios as "Pending Transactions"
- Of course: I can overdraw the account upto an allowed value, which can be less than zero
- This fully fulfills my use-case
Thank you for your input. My question is fully answered
|
|
|
|
|
I cannot debug (set breakpoints,...) my UnitTests with NUnit3.
What works already
My Test-Dll's contain all the tests. They are executed and the result is displayed correctly in the output-window when I run them with the console-runner. So, the test-setup itself seems to be correct.
What I want:
I want to DEBUG my tests when they are executed with the console-runner and stop at breakpoints.
What I did so far:
=> I create a Dll, which contains the testcases/-fixtures.
=> I have set this Dll-Project as startup-project
=> At Properties | Debug of the Dll-Project I select the console-runner as"Start external Program"
and I add the command-line arguments ( my dll )
=> I set breakpoints in my test-code
=> I start a debug-session: The tests are executed, but not breakpoints are hit
It seems, I misunderstand some concept completely
|
|
|
|
|
Have you copied your pdb files in as well?
This space for rent
|
|
|
|
|
yes, the complete dll-bunch is in one single directory. Together with all pdb's
|
|
|
|
|
|
It does not help. I was already aware of this
But thank you very much ! ! !
I searched a lot of links, but all I found is referencing quite old versions of Nunit.
I use NUnit.Console-3.7.0.
All of my tests are executed "perfectly". The only problem is: Breakpoints are not hit
|
|
|
|
|
Maybe try a newer version (3.9):
Downloads
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
hai
am cyrrently working on a file monitoring system in c# that records all events in log format, with a command to dump logs as .txt files.
my question is, is the a code i could use to dump these log files into a file, save them at a specific time in the day.
say while its monitoring, it automatically saves the logs in a file at 23:59 everyday.
while monitoring a particular folder.
given there is a path in which to save.
|
|
|
|
|
|
You can either use a Timer, or check the time whenever you write a log record. Alternatively use a separate application the gets scheduled to run at a fixed time every day.
|
|
|
|
|
Why aren't you just writing the events to the log? If you're keeping this stuff in memory and writing it to a log later, you run the risk of running out of memory before you write the log!
|
|
|
|
|
We need to bring a Word document into focus and then paste text into the document at the current cursor location. We know the name of the Word document. Unfortunately Word only has one instance and one main window, which means that I cannot simply locate the window, bring it into focusand paste the data. Is there a way to do this from C#?
|
|
|
|
|
I hate applications that steal focus, as many other users do.
Do you need to show the Word-application, or would it suffice if you can embed Word in your application and push that to the foreground?
Inserting text into a document would be rather simple, as a word-document is a zipped collection of files you can read with notepad.
Do you want to replace text at a certain pre-determined position, or at the cursor? If the latter, you may be better of writing a Word-adding that doesn't steal focus.
Many questions here
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks for the response. Our requirements are that the user must be able to configure our system such that data from a reader is pasted into a user chosen application. Because this is generic, the way we do it is to bring the app into focus and then paste the data. For images we use the clipboard.
This is not intended to run on someone's work PC, rather it is an automated system, and giving an app focus is desirable as the operator can see the read data.
We wish to post into the cursor position in the target document.
|
|
|
|
|
In that case you would want to get a list of processes and enumerate its windows. You'd probably need some WinAPI to identify the correct window.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I already do that. Unfortunately Word only has one main window. I've tried to enumerate the child windows but that does not work, it only locates the topmost. If you run up two documents, then look in Task Manager, you'll see what I mean: one process and one window.
|
|
|
|
|
That's the only way I know to get the Windows that are created by the process.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
[EDIT]
I agree with Eddy Vluggen that you don't need to keep focus to the document/application to be able to paste piece od text. You need to refer to the document you want to use:
document = InstanceOfWord.Documents["ShortDocumentName.docx"];
document.Selection.PasteSpecial();
document = InstanceOfWord.Documents("ShortDocumentName.docx")
document.Selection.PasteSpecial()
More at MSDN: How to: Programmatically Insert Text into Word Documents
But, if a requirement is to keep focus, there's good news: there's buit-in method called: Document.Activate Method (Word)
InstanceOfWord.Documents["ShortDocumentName.docx"].Activate;
InstanceOfWord.Documents("ShortDocumentName.docx").Activate
|
|
|
|
|
I used to unit test private methods, then some people advised me not to do so...
What do you think? should I unit test only public methods or private methods as well?
|
|
|
|