views:

158

answers:

3

You require an algorithm to read in one customer's account balance at the begining of the month, a total of all withdrawals for the month, and a total of all deposits made during the month. A federal tax charge of 1% is applied to all transactions made during the month. The program is to calculate the account balance at end of the month by (1) subtracting the total withdrawals from the account balance at the beginning of the month, (2) adding the total deposits to new balance, (3) calculating the federal tax (1% of total transactions - that is, total withdrawals+total deposits), and (4) subtracting this federal tax from the new balance. After these calculations, print the final end-of-month balance.

A: 

This happens to be the homework problem on www.palinfonet.com/hw_java2008.pdf due April 17th 2008, question #3.

NoahD
+1  A: 

This will be a straightforward translation into pseudocode. As this is a homework assignment¸ you'll have to do the legwork yourself. Just evaluate each step in turn:

input: accountBalanceStart, totalDeposits, totalWithdrawals
output: accountBalanceEnd

Let's take the first step:

accountBalanceEnd <-- accountBalanceStart - totalWithdrawals

Now the second:

accountBalanceEnd <-- accountBalanceEnd + totalDeposits

Next, compute the tax and subtract it:

accountBalanceEnd <-- accountBalanceEnd - (totalWithdrawals + totalDeposits) * 0.01

Finally, show the result:

print accountBalanceEnd
John Feminella
Would'nt it be a cleaner solution to store in a table with (amount, account_id, date) and not deposits and withdrawals separately,so one could just do a SUM(amount)?
Hamid
That's not how the input to the problem was defined. It's pretty clear that you get a single customer's account balance, their total withdrawals, and their total deposits. Then you're asked to (given this information) compute the final balance.
John Feminella
A: 

I agree with the other posters, that this is neither a question nor is this a place where people are going to do your homework. But just in case you really absolutely have no clue I give a more concrete example.

What you most likely will need to read:

PL/SQL

SQL Quickref

I would probably try something like this. I intendedly haven't tested or commented it and the syntax might also be wrong for mssql.

    CREATE OR REPLACE FUNCTION Calculate_Balance(account_id long, account_balance float, date datetime) RETURNS float AS
    DECLARE
 transactions_sum float;
 fed_tax float;
 new_account_balance float;    
BEGIN
 transactions_sum = SELECT SUM(amount) from transactions 
  WHERE transactions.account_id = account_id 
  AND month(transactions.date) = month(date) 
  AND year(transactions.date) = year(date);

 fed_tax = transactions_sum * 0,01;
 transactions_sum = transactions_sum - fed_tax;
 new_account_balance = transactions_sum + account_balance;

 RETURN new_account_balance;
END;
Hamid
I think there's a logic error on your fed_tax line. If there were 100 transactions in a month, you would be charging federal tax equal to the full sum! The *number* of transactions isn't relevant, only the total size.
John Feminella
I was wondering about that too.Seems I misunderstood "A federal tax charge of 1% is applied to all transactions made during the month". Thought this was another "strange tax" ;) Edited.
Hamid