Solved

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

Posted on 2003-03-17
Medium Priority
215 Views
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
Sorry,

Insert ATM card: Brown
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
Sorry.
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.
0
Question by:crystalfish0318
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +1

LVL 12

Expert Comment

ID: 8158172
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 {
private:
moneyType balance;
string name;
int pin;
public:
....
};

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 {
private:
std::map<string,account> accounts;
public:
...
};

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 & *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]

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 {
st_insert_card,
st_check_pin,
st_get_cmd,
st_deposit,
st_withdraw,
st_balance,
};

[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.

Alf
0

LVL 6

Expert Comment

ID: 8160397
Salte,
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 !
0

LVL 12

Expert Comment

ID: 8160820
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.

Alf
0

LVL 6

Expert Comment

ID: 8160823
Alf

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 **
0

LVL 12

Accepted Solution

Salte earned 100 total points
ID: 8160942
Understood
0

Author Comment

ID: 8162144
Alf

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.
0

Author Comment

ID: 8162168
thank you very much. You are a really kind person.
0

LVL 6

Expert Comment

ID: 8167511
crystalfish0318

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 **
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on thâ€¦
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month11 days, 10 hours left to enroll