Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
The work was from the book written by Joyce Farrell. So she doesn't really have knowledge, not my teacher

[EDIT — the code dump moved from OP's comment — SA]

C#
using System;

class TaxPayer : IComparable<TaxPayer>
{
   private string ssn;
   private double grossIncome;
   private double taxOwed;

   public string SSN
   {
     get { return ssn; }
     set { ssn = value; }
   }

   public double GrossIncome
   {
     get { return grossIncome; }
     set
     {
        grossIncome = value;
        if (grossIncome < 30000)
           taxOwed = 0.15 * grossIncome;
        else
           taxOwed = 0.28 * grossIncome;
     }
  }

  public double TaxOwed
  {
     get{ return taxOwed; }
  }

  public TaxPayer(string ssn, double grossIncome)
  {
     this.ssn = ssn;
     GrossIncome = grossIncome; // need to set the property here, not the field directly
  }

  public override string ToString()
  {
     return String.Format("SSN = {0}, Income = {1,11:C}, Tax Owed = {2,10:C}", ssn, grossIncome, taxOwed);
  }

  public int CompareTo(TaxPayer other)
  {
     return this.taxOwed.CompareTo(other.taxOwed);
  }

}

class TaxPayerDemo
{
  static void Main()
  {
     TaxPayer[] taxPayers = new TaxPayer[10];
     string ssn;
     double grossIncome;
     Console.WriteLine("Please enter details for 10 taxpayers\n");

     for(int i = 0; i < 10; i++)
     {
        Console.WriteLine("Details for taxpayer #{0}", i + 1);
        Console.Write("   Enter Social Security Number : ");
        ssn = Console.ReadLine().Replace("-", ""); // get rid of any dashes
        Console.Write("   Enter Yearly Gross Income    : ");
        grossIncome = Convert.ToDouble(Console.ReadLine());
        taxPayers[i] = new TaxPayer(ssn, grossIncome);
        Console.WriteLine();
     }

     Console.WriteLine("The taxpayers and their details are\n");
     for(int i = 0; i < 10; i++) Console.WriteLine(taxPayers[i]);
     Array.Sort(taxPayers); // sorts the taxpayers in order of tax owed
     Console.WriteLine();
     Console.WriteLine("The taxpayers in order of tax owed are\n");
     for(int i = 0; i < 10; i++) Console.WriteLine(taxPayers[i]);

     Console.ReadKey();
  }
}
Posted
Updated 25-Jan-12 6:26am
v7
Comments
Sergey Alexandrovich Kryukov 23-Jan-12 22:06pm    
Help with what? Any particular difficulty? If you don't understand the basics, the answers cannot help you. Take an elementary language and .NET manual, read it from scratch, then try to solve the problem. If you're stuck with some problem, we will gladly help you. Right now, it will be just a waste of time.

I you feel it's hard now, seriously think about different career.
--SA
Alexander Alekseyevich Fedoseev 24-Jan-12 18:28pm    
Thank you for your help.... Think of different career? Really , just because
I don't know how to solve this problem?

[EDIT: code dump moved to the text of the question — SA]
SteveAdey 24-Jan-12 4:22am    
It sounds like you've been given a school assignment and you want someone to do it for you. Take a different course ;-)
Alexander Alekseyevich Fedoseev 24-Jan-12 18:28pm    
you gonna pay my financial aid back if I drop that course? I don't think so, if u not gonna help no need to post
Sergey Alexandrovich Kryukov 24-Jan-12 20:04pm    
I would say this is pretty rude to Steve. However, please see my answer. This is not an answer to your question yet, just a post in response to your comment, for now.
--SA

OK, I reviewed the code, as I promised. Now, the situation is very different from what I could see before you added the code to the post. Now, I would ask: "So, and where do you see a problem?". The code is not too bad.

I actually can see a few problems, and not very critical ones. Let me overview some of them:

  • Replace all keyword public with internal. You should not allow more access than it is really required. You will need public only when you need to reference your assembly by some other assembly and get access across the assembly boundary. Change access only when you need it.
  • Not a mistake at all, but just a note: you implemented IComparable<TaxPayer> which is really needed for sorting.

    You would also often need to implement System.Object.Equals and that will require you to override System.Object.GetHashCode. Do you know why? The syntax will require that, but the real purpose is: the object with overridden identity needs to be able to serve as a key in dictionaries and similar key-accessed collections. Just in case, definition of "==" and "!=" operators would be helpful.
  • Now, this is a really serious problem: look at the immediate constant 10. It appears 6 times! And this is a real crime you are committing (against youself). Suppose you will need 12 records next time. How will you support it? Remember one of the main principles: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

    You have two options. First, you need to make this 10 an explicit constant. When you change it, all program's behavior will be changed to 10 at once. Easy, isn't it?

    Another option is even better: request the user to enter the number of elements instead of 10. Don't tell me your teacher did not require that. I already explained that this teacher is not so qualified. Also, one of the main real responsibilities of the developer is to do something that your "boss" does not require if there is a good reason for that. Here is why, in this case: the development effort will be the same, but how can you debug it? Repeat 10 inputs every time? You should always make the most abstract solution possible when it does not take extra effort.
  • Do you need immediate constants at all? How about 30000, 0.15, 0.28 — they are not repeated? No, but it's still a problem. Do the same thing: put them in the explicit constants. Put all explicit constants together; they will show you what a parameters of the task. It if would be "real" development, sooner of later they all will be converted to variables. Well designed final-version real life application have almost no constants except things like, 0, 1 or null. All data ultimately comes from data files, configuration files, network, etc.
  • How about string immediate constants? Ideally, they all should go to resources and/or data files. If they are not repeated, what you do can be considered as acceptable, at least for the study assignment.
  • The stupid operation of removal of "-" (yes, it is stupid, but as I say, this is a fault of your teacher) should better be placed in the implementation of the property SSN.
  • Well, you follow naming convention quite well, except two problem: spell Ssn, not SSN (yes, this recommendation is officially recommended by Microsoft, and it has its reasons) and i — really bad name. Imagine you want to find all loop variables. Search feature won't help you. By that reason, all variables should be based on correctly spelled non-abbreviated English words. The word index would be just right.


That's it. I don't say that's all, but I think I outlined main problems. Your work (I really hope this is really your work) is not to bad for a beginner, not at all. You can be taught quite well. You only need to get more positive attitude, confidence, desire for hard and accurate work, for really good learning.

Good luck,
—SA
 
Share this answer
 
v8
Comments
Espen Harlinn 25-Jan-12 14:02pm    
Impressive - 5'ed!
Sergey Alexandrovich Kryukov 25-Jan-12 14:04pm    
Thank you, Espen.
--SA
DaSasha wrote:
you gonna pay my financial aid back if I drop that course? I don't think so, if u not gonna help no need to post [Spelling and grammar quotes as is — SA]


Dear Da Sasha,

First of all, I insist that we leave the the decisions on the need for his posts to SteveAdey along. I would modestly hope that you would grant similar privilege to me as well.

Just think about your financial situation more thoroughly:

You don't get financial aid for free. I'm pretty sure you will face one of the following: 1) a need to repay your aid money in full, 2) a need to re-pay part of this money while the rest of it will be covered by government or other funds; 3) more likely, a need to repay your aid money in full plus some interest.

The money is funded to the student in more or less shaky assumption that they are able to complete education, gain essential experience and in this way achieve some status and reputation which could help them to get reasonable earnings, sufficient for making for living and repay of the debt.

Let's look at your situation. You are at the very beginning, just getting very basic knowledge. Not only you are not confident enough in the most elementary tasks (this is not a problem at all, it will come sooner or later) you don't demonstrate the passion which is very typical for the beginners in the exciting field of computing, just the opposite, you demonstrate lack of independent behavior and something that I cannot call anything but "wining". Most interested students are over-estimate their capability, best students estimate them adequately, but who complains that the job is "really hard"? I almost never observe such behavior. Maybe this is just your sincere style of communication, but in fact it looks like an extreme weakness to me.

I have further concerns about your teacher. If the requirement "use a string for the type, but do not use dashes within the number" comes from a teacher, this is a strong indication that this teacher has very little idea about programming, its quality, maintenance, etc. The presentation of such a fixed-format structure as SSN using anything but some adequate integer type of a structure made of numeric members is plain stupid. Even if the goal would be to simplify your assignment — it does not simplify anything. You see, there is such thing which I call "education fraud". Everything is perfectly legal, only you pay your money for the fake knowledge while you might be not independent enough to feel the fake education services or learn by yourself. I don't know, maybe your teacher is just weak, but may be a fake. If you cannot resist and learn by yourself (which is normally the main way of learning), you can get fake knowledge and fake experience. Do you think you would be able to get enough money with that?

So, what can happen? If you change your career path, you can loose some money, but you can get a chance to earn some money in some other activity. If you don't, you can save your aid money, but only for some period of time as you will have to repay it anyway. In exchange, you risk wasting considerable time of your life to just frustration and loose you chance to earn money. You can potentially get into a great trouble of re-paying the debt, not just providing for living. I know, life can be really hard. It is hard, in fact.

However, I never know exactly. I don't know how much money, I don't know your real risks. You decide. Be responsible.

My best desire here is to be wrong. Yes, I wish to see that I was wrong and you can cope and change things and make a great career in computing. I really wish you that. I only try to help you to realize what's involved.

Please forgive me this off-topic post. I'll see if I can help you with the code you've added to your post.

Best wishes,
—SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Jan-12 12:59pm    
[Moved from "solution" post -- SA]

First of all, thank you for your advices, really helpful, you seem like have a lot of experience in programming, do you have masters?
Then he(Steve) can post as much he wants to I don't really care.

And how did we get in to "how I will repay my financial aid?"
If you wonder how I will repay the debt, that I don't have at all. Well then IF i will have debt I most likely will get my 4 year degree(but I am hoping to get in to four year college that has program that let you get masters degree i think its called accelerated program) and join military as officer.
Why am I not confident enough?Why I don't demonstrate passion? Wining? + (everything else you said) You were able to tell that about me by my one thread with work problem i copy and pasted from the book?
I mean honestly what was all that? You know I am taking 4 programming classes to see if I want it to be my future, and I am not really sure yet. I like everything that has to do with computers and how they work , I love working with hardware, but programming also seems very appealing to me.

Then its not my teacher its the author of the book who has no knowledge, feel free to write to her and tell her that, her name is Joyce Farrell.

And I don't think you were brought to US at age of 16 and had no English knowledge, had to spent extra year in high school why everyone else already were in college.
Well that's what happened to me, and I have been in US for only 5 years and I am trying my best to over come all the obstacles that foreign student has.
Alexander Alekseyevich Fedoseev 25-Jan-12 13:05pm    
oh and in the end of this reply I was wining a little... lol. Thx for moving it, I just realized the difference
Sergey Alexandrovich Kryukov 25-Jan-12 13:14pm    
I see, thank you. :-)
--SA
Sergey Alexandrovich Kryukov 25-Jan-12 13:14pm    
All right, thank you for this message. I only replies to your original post. People don't see what you really feel, people see only the behavior. If someone says "very hard" about something (about creation of the array of structure, what a big deal!), some would say you are wining, isn't that just natural? No more and no less than that. So, please don't it too close to the heart.

I told you that I would be glad to be wrong? I'm really glad if your life and career work out well.

If course I did not know anything about your teacher, but I needed a word to express the general idea. A "generic teacher". It could be a book, a professor, anything. You should understand such things. Whatever you say, there is a lot of fake books and fake education around. Naturally, everyone tries to make money...

Best regards to your teacher. Is she ever needs programming advice or help, please tell her she is welcome to ask us a question -- we will gladly help. She is also very welcome to help others.

Best,
--SA
Alexander Alekseyevich Fedoseev 25-Jan-12 19:15pm    
Kryukov!Так вы русский! У меня столько вопрос, не могли бы мы поговорить , где нибудь, к примеру через email или chat

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900