I'm getting so confused with the C++ Analysis......................

Finally, the C section is over.
Now, I'm getting more difficult at learning C++.
I'm still not very good at programming. Comparing to JAVA, C++ is much harder.
I really need some helps about two analysis programs.

#1: ATM Problem
      We have only three accounts in our bank. The names on each account are: "Jones," "Brown," and "Smith." Respectively, they each have 500, 200, and 700 dollars, and their PINs are 5390, 4211, and 8607. Our ATM will accept only ATM cards from these customers. No one else will be allowed to use the machine.
      When idle, the machine is waiting for the ATM card. If the name on the card matches one of the account names, then the machine will get a PIN from the customer and verify it. If the PIN matches, then the machine will allow the customer to withdraw money, deposit money, or see the balance. The account balance will be updated accordingly.
      When you do this analysis, use these two class (besides other): PinVerifier and ElectronicDrawer. The PinVerifier will get the secret PIN number from the customer and verify it. The Electronic Drawer will accept cash and dispense it. All input and output amounts should go through this class.
      To simulate this machine on the computer, use the following dialog as an example:

Insert ATM card: Jackson
Jackson doesn't have an account in this bank

Insert ATM card: Brown
Enter your PIN code: 5390

Insert ATM card: Brown
Enter your PIN code: 4211
Enter 1 for Deposit, 2 for Withdrawal, 3 for Balance, 4 to Exit: 3
You have $200.00
Enter 1 for Deposit, 2 for Withdrawal, 3 for Balance, 4 to Exit: 2
How much? 300.00
Enter 1 for Deposit, 2 for Withdrawal, 3 for Balance, 4 to Exit: 2
How much? 50.00
Remove money from the cash drawer.
Enter 1 for Deposit, 2 for Withdrawal, 3 for Balance, 4 to Exit: 1
How much? 100
Place money in the deposit slot.
Enter 1 for Deposit, 2 for Withdrawal, 3 for Balance, 4 to Exit: 4

Insert ATM card:

#2: Inventory Problem
      When I worked with Northwest Orient Airlines at JFK airport and we needed a piece of equipment, we didn't have parts available from only our stock. Because many airlines use the same plane, for instance, the Boeing 747, we borrowed parts from other airlines as well. In this problem, create three instances of the class called DB(for database). DB should maintain a simple table of five parts each. Assume that there are only five possible parts in our databases. An airline terminal may handle only one part at a time so that we don't have to ask how many when getting the part information from the user.
      If a part is not available in your own terminal, then have your database interrogate other terminals' databases to see if you can borrow one. We won't worry about returning any borrowed parts, for now. Keep the databases as independent or autonomous as possible. Accessing the databases of other terminals should be done only by your own database.
      Create CRC cards for this problem.
      Here is a sample run:

1.Northwest  2.PanAm  3.TWA  4.Quit : 2
I am now in the PanAM terminal
   1.Need part,  2.Part came in,  3.See inventory,  4.Leave terminal : 1
   Give part number : A507
   We have it. Take one.
   1.Need part,  2.Part came in,  3.See inventory,  4.Leave terminal : 1
   Give part number : A507
   Northwest lent us one. Take it.
   1.Need part,  2.Part came in,  3.See inventory,  4.Leave terminal : 1
   Give part number : A507
   No one has it. Ground the plane.
   1.Need part,  2.Part came in,  3.See inventory,  4.Leave terminal : 2
   Give part number : X200
   Part placed in stock, thank you.
   1.Need part,  2.Part came in,  3.See inventory,  4.Leave terminal : 1
   Give part number : V009
   No such part
   1.Need part,  2.Part came in,  3.See inventory,  4.Leave terminal :
(and so on)

I will be very thankful to those who would like to spend time teaching me how to write these two programs.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is two different programs and although part of the things you learn while doing one problem can also be used on the other, it is best if you keep them apart and do one thing at a time.

Part of the road to solving a programming problem is to take big problems and split them into smaller problems and continue that process until all your problems are small and easy to solve.

So step 1 is to ignore problem 2 and only look at problem 1. Then when problem 1 is solved we can look at problem 2 and try to solve it. However, my posting will be big enough only dealing with problem 1 so I'll ignore problem 2 for now.

Secondly, the road to object oriented program design and analysis is to first take a good look at your problem text. Try to identify nouns and verbs that are important to the problem.

For example in your problem the word 'account' appear quite often, it is safe to say that you should have a class named 'account' in your problem.

An account has a balance (a monetary amount), a PIN code and a name. It does not appear to have much else so this imnplies that an account look like this:

class account {
   moneyType balance;
   string name;
   int pin;

The pin code could have been a string if you prefer. The balance must be some type that can implement the monetary amount. In C++ a double or an int counting cents would be good enough. so the moneyType can be double, this is the easiest I guess and should be sufficient for our purposes.

Now, the bank is then a collection of these accounts. There are only three acounts so you need a collection of three elements. You can use a map, a list, an array, a tree or simply have three global variables for each of the accounts. The latter is not useful in a real life situation though since it is hard to expand when the bank get a fourth customer. I would therefore suggest some other structure. Since you often need to look up the accounts based on account name you can probably use the name as a lookup key. Again, in real life situations a bank would never use name as lookup key since many people can have the same name. A real bank would associate each customer with a customer ID and use that as the lookup key. The natural thing is therefore to make a map. STL has a map type for you already, I suggest you use that.

map<string,account> accounts;

Also, you might want to say that "bank" is also an object - even if it is only one such object present - and thus make a class bank to hold this account collection:

class bank {
   std::map<string,account> accounts;

Also, in the classes above I have only written the members, the class probably need a lot of accessor functions etc but I leave all that to you. I believe you can figure it out.

However, since I introduced the map type I can show you how to find an account given a name.

account * bank::find(const char * name)
   std::map<string,account>::iterator p = accounts.find(name);
   if (p == accounts.end())
      return 0; // not found.
   return & *p;

bank the_bank;

I will also assume a 'using namespace std;' someplace in early in the code so that I don't have to write std:: everywhere.

The next little piece is the main program, it is a program that read info from screen and output a response. It is essentially something that goes like this:

[edited by moderator Mindphaser]

Several things about this code.

1. it is not perfect and I have purposely made it so, there are lots of room for improvement on it. But it does what you want it to do.

2. All the account methods used are not implemented anywhere I believe you know how to do that yourself.

3. This is essentially a 'request/response' engine which receives a request and then respond to it. It is also known as a 'state machine'. In my implementation the state is kept by being in a certain place in the program. A better solution would be to have a separate state variable and have the state determined by that variable. In this case determining the states would be an important part of the solution:

enum state_t {

[edited by moderator Mindphaser]

This code has several advantages over the first even though it does the same thing. It is a state machine so you can easily add new states to it as needed, so the code is more flexible. I believe it is also easier to read.

Each request/response is done by one loop through the while loop while the first solution had a nested loop solution where you had an inner loop to control the transactions from one customer and an outer loop for dealing with new customers.

In any case, your problem 2 is very similar and I am pretty sure you know how to solve problem 2 if you read through this solution for problem 1 given above.

EE policy is against making homework for students.
This is not the first time that you make homework for them.
You should restrain yourself to explain how to do things rather then giving them source codes.
This apply also for comments posted to answer non-students programmers, you should lead the way, enlight them, etc. but never write the whole code for them.
Doing so you prevent them from learning !
I got the impression that this is a person who is trying to teach himself/herself programming and not homework per se. In any case I did omit large portions of the code - and did so purposely exactly for the reason you state. They learn more if they work at it themselves.

However, I did try to explain how one thinks when translating a problem to a computer program and it is that process that is of importance in my posting. The code itself is only to illustrate it. The main reason why there's so much code in my answer was that I showed lots of code that did the same thing, i.e. several ways to do the same thing. None of the code is by itself enough to make the program. For example the account class is essentially missing - and I did that on purpose.

I did include the code for locating an account given a name if you use std::map, since I assumed this might not be something crystalfish is familiar with.

I also included large portions of the main program for the explicite reason that I showed several ways of doing it. In any case, this main program in itself doesn't really contain the hard logic, it is just a bunch of cout and cins. It is the code in the account class that contain the main logic.

I did possibly provide too much in that I listed up the data members of the account class. This - I admit - is a big spoiler, but it is also something that follows automatically when you list up the nouns used in describing the problem. As such the problem description is very important in that it describes all the object types encountered in the problem and the relationships between them.

No, I do not try to go against EE policy and provide homework solutions to people - at least not intentionally. There are times when I did write a bit too much and I have apologized for that. I am not sure that this is one of them though.

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.


I know that it is hard to define 'academic honesty', but this goes too far. We usually accept that our experts help students with their homework as long as they have a specific question they need help with. However we do not accept to get their homework done. Our Member Agreement at http://www.experts-exchange.com/jsp/infoMemberAgreement.jsp has "violating the guidelines for academic honesty or other unethical behavior" in its text and warns you that both asker and expert can get their account suspended if they violate these rules.

I removed the main parts of the program you posted and send a copy of the original to Admin. Please consider this as a warning, nothing else, I know that you are a very active member of this community. But the next time you are all exited about solving somebody's problem, take a good look at the question first and if it is homework take a deep breath and request a specific problem!

** Mindphaser - Community Support Moderator **

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crystalfish0318Author Commented:

Thank you for your support. I know that Mafalda and Mindphaser is probably thinking that I am a just lazy person who just wants other people to do the complete programs for for him.

Guess what!! If I'm that kind of asshole, I could just ask you guys to give me the straight programming codes. Alf is a really good person. For those guys who are already experts in programming, how can they understand my difficult time learning C++ and other programming languages.

If Mindphaser is gonna delete my account, FINE! I am just a beginner. I don't have any rights to anything here.

Salte, thanks again for your help. I think I will never come to this site again.
crystalfish0318Author Commented:
thank you very much. You are a really kind person.

I never said I am suspending your account. I pointed Salte to our Member Agreement and told him to be careful ... for his own sake.

I removed some of his code for these reasons and since you said you wouldn't need that complete code ther should be plenty of information to continue with your problems.

As I pointed out, there is no problem with helping but in these academic cases we agreed to provide help for specific questions, not for the whole problem.

Unfortunatelly we HAVE many students join EE and ask us to do their complete work ...

** Mindphaser - Community Support Moderator **
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.