Simple App Store

Simple App Store

Automatic “0” Policies

Goals:

User defined functions, pointers, arrays, Menu driven program

Specifications

A Company needs to create software for their new line of phone Applications. Customers will have the opportunity to purchase the Apps using the following cash amounts:

Cash Amounts that can be deposited:

  1. $1000.00,
  2. $500.00,
  3. $100.00
  4. $10.00

App List and Prices

C – Clown Punching                           $299.99
V – Virtual Snow Globe                 $349.99
R – Remote PC                                   $999.99
G– Grocery List Helper                    $2.99
M – Mobile Cam Viewer                    $89.99
Use Case 1 (Inadequate Balance)

  1. User opens the App and is greeted and the amount of money in the bank is displayed.
  2. List of available Apps is displayed.
  3. User selects an App to purchase.
  4. If user does not have enough money, she is prompted to add more money and money choices are displayed.
  5. User adds money
  6. User gets the App, and the price is deducted and
  7. The balance is displayed.
  8. User is prompted to see if she wants to buy another App.
  1. If user says yes, go to step 2
  2. If user says no, Display the amount available for next time and (Optional) display the items that were purchased.
  • Say Goodbye

Use Case 2 (Enough Balance)

  1. User opens the App and is greeted and the amount of money in the bank is displayed.
  2. List of available Apps is displayed.
  3. User selects an App to purchase.
  4. If user has enough money, user gets the App, and the price is deducted
  5. The balance is displayed.
  6. User is prompted to see if she wants to buy another App.
  1. If user says yes, go to step 2
  2. If user says no, Display the amount available for next time and (Optional) display the items that were purchased.
  • Say Goodbye

Sample Output

Welcome to THE APP STORE
**********************************
You have $0.00 in your bank
————————-
HERE ARE THE SELECTIONS:
C — Clown Punching             $299.99
V — Virtual Snow Globe         $349.99
R — Remote PC                  $999.99
G — Grocery List Helper        $2.99
M — Mobile Cam Viewer          $89.99

Please enter a selection: c
——————————
You do not have enough in your bank
The item costs $299.99
You have $0.00 available in your bank
Please credit your money by selection:
— 1   $1000.00
— 2   $500.00
— 3   $100.00
— 4   $10.00
Deposit Amount: 2

You have purchased: c
you have $200.01 left,
would you like to make another purchase?y
**********************************
You have $200.01 in your bank
————————-
HERE ARE THE SELECTIONS:
C — Clown Punching             $299.99
V — Virtual Snow Globe         $349.99
R — Remote PC                  $999.99
G — Grocery List Helper        $2.99
M — Mobile Cam Viewer          $89.99

Please enter a selection: g
You have purchased: g

you have $197.02 left,
would you like to make another purchase?y

**********************************
You have $197.02 in your ban
————————-
HERE ARE THE SELECTIONS:
C — Clown Punching             $299.99
V — Virtual Snow Globe         $349.99
R — Remote PC                  $999.99
G — Grocery List Helper        $2.99
M — Mobile Cam Viewer          $89.99

Please enter a selection: r
——————————
You do not have enough in your bank
The item costs $999.99
You have $197.02 available in your bank
Please credit your money by selection:
— 1   $1000.00
— 2   $500.00
— 3   $100.00
— 4   $10.00
Deposit Amount: 2

——————————
You do not have enough in your bank
The item costs $999.99
You have $697.02 available in your bank
Please credit your money by selection:
— 1   $1000.00
— 2   $500.00
— 3   $100.00
— 4   $10.00
Deposit Amount: 2
You have purchased: r
you have $197.03 left,
would you like to make another purchase?n
you  have: $197.03 credit available for next purchase
Thank you, enjoy your purchase(s)

You made 3 purchases as follows:
299.99
2.99
999.99
Press any key to continue . . .

Design
You must have at least 7 user defined functions as follows:

// Displays the list of apps available
//prompts for the user’s selection and sets (returns) the value of the selection
void displayApps(char *pSelection);

//sets (returns) the cost of the item based on value stored in purchase
void setCost(char selection, double *pCost);

//Displays the codes for the user to input money – gets user input amounts
//compares the int codes and updates the deposit amount
void paymentOptions(double *pDeposit, double cost);

//compares the amount the user has in deposits to the price of app selected.
//It returns 1 if the amount is enough to cover the cost, 0 if there is not enough.
int compare(double deposit, double choiceCost);

//uses paymentOptions function to display and collect dollar amounts from the user
//uses compare function to keep comparing the added deposited amount to the item cost.
void pay(double *pDeposit, double choiceCost);

//calculates the amount of leftover from your deposits
void getChange(double *pDdeposit, double choiceCost);

//Asks the user if they want another app
void doItAgain(char *pQuit);

Solution 

#include <stdio.h>

#include <ctype.h>

// Displays the list of apps available

// prompts for the user’s selection and sets (returns) the value of the selection

voiddisplayApps(char *pSelection);

// sets (returns) the cost of the item based on value stored in purchase

voidsetCost(char selection, double *pCost);

// Displays the codes for the user to input money – gets user input amounts

// compares the int codes and updates the deposit amount

voidpaymentOptions(double *pDeposit, double cost);

// compares the amount the user has in deposits to the price of app selected.

// It returns 1 if the amount is enough to cover the cost, 0 if there is not enough.

int compare(double deposit, double choiceCost);

// uses paymentOptions function to display and collect dollar amounts from the user

// uses compare function to keep comparing the added deposited amount to the item cost.

void pay(double *pDeposit, double choiceCost);

// calculates the amount of leftover from your deposits

voidgetChange(double *pDdeposit, double choiceCost);

// Asks the user if they want another app

voiddoItAgain(char *pQuit);

// BONUS FUNCTION – WORTH 5 POINTS – OPTIONAL

// takes an array of all the purchase transaction costs

// and the number of items purchased

// and displays the information onto the screen

voiddisplayPurchases(double priceList[ ], intnum);

int main() {

char selection;

double deposit;

double cost;

double purchases[100]; // support at most 100 purchases

intnum;

printf(“Welcome to THE APP STORE\n”);

deposit = 0; // initial bank has $0

num = 0;     // number of purchases initialized to 0.

// use the above functions to implement the program

// loop until the user entered N to quit the program.

do {

printf(“**********************************\n”);

printf(“You have $%.2f in your bank\n”, deposit);

displayApps(&selection);   // select

setCost(selection, &cost); // calculate cost

pay(&deposit, cost);       // pay for it

// save the purchase into the array

purchases[num] = cost;

num ++;

printf(“You have purchased: %c\n”, selection);

printf(“you have $%.2f left,\n”, deposit);

doItAgain(&selection); // ask the user to buy again

printf(“\n”);

} while (selection == ‘Y’);

printf(“you have: $%.2f credit available for next purchase\n”, deposit);

printf(“Thank you, enjoy your purchase(s)\n\n”);

// display all purchases

displayPurchases(purchases, num);

// ask the user to press enter key to quit the program

printf(“\nPress any key to continue … \n”);

scanf(” %c”, &selection);

return 0;

}

// ——- function implementations ———

voiddisplayApps(char *pSelection) {

char choice, nl;

printf(“————————-\n”);

printf(“HERE ARE THE SELECTIONS:\n”);

printf(“C — Clown Punching             $299.99\n”);

printf(“V — Virtual Snow Globe         $349.99\n”);

printf(“R — Remote PC                  $999.99\n”);

printf(“G — Grocery List Helper        $2.99\n”);

printf(“M — Mobile Cam Viewer          $89.99\n”);

printf(“\n”);

// use a do-while loop to continue asking

// until the user entered a valid choice.

do {

printf(“Please enter a selection: “);

scanf(” %c”, &choice);

*pSelection = choice;

choice = toupper(choice);

} while  (choice != ‘C’ && choice != ‘V’ && choice != ‘R’ &&

choice != ‘G’ && choice != ‘M’);

}

voidsetCost(char selection, double *pCost) {

double cost = 0;

// compare uppercase of the selection only

selection = toupper(selection);

switch (selection) {

case ‘C’:

cost = 299.99;

break;

case ‘V’:

cost = 349.99;

break;

case ‘R’:

cost = 999.99;

break;

case ‘G’:

cost = 2.99;

break;

case ‘M’:

cost = 89.99;

break;

default: // should not happen

break;

}

*pCost = cost;

}

// Displays the codes for the user to input money – gets user input amounts

// compares the int codes and updates the deposit amount

voidpaymentOptions(double *pDeposit, double cost) {

char choice;

double deposit;

printf(“Please credit your money by selection:\n”);

printf(“— 1   $1000.00\n”);

printf(“— 2   $500.00\n”);

printf(“— 3   $100.00\n”);

printf(“— 4   $10.00\n”);

// use a do-while loop to make sure the selection is valid

do {

printf(“Deposit Amount: “);

scanf(” %c”, &choice);

} while (choice < ‘1’ || choice > ‘4’);

// calculate the amount to deposit according to user’s choice

switch (choice) {

case ‘1’:

deposit = 1000;

break;

case ‘2’:

deposit = 500;

break;

case ‘3’:

deposit = 100;

break;

case ‘4’:

deposit = 10;

break;

default: // this should not happen

deposit = 0;

break;

}

*pDeposit += deposit;

}

int compare(double deposit, double choiceCost) {

return deposit >= choiceCost;

}

void pay(double *pDeposit, double choiceCost) {

// loop until the user has enough to purchase

while (compare(*pDeposit, choiceCost) == 0) {

printf(“————————-\n”);

printf(“You do not have enough in your bank\n”);

printf(“The item costs $%.2f\n”, choiceCost);

printf(“You have $%.2f available in your bank\n”, *pDeposit);

// ask the user to deposit

paymentOptions(pDeposit, choiceCost);

printf(“\n”);

}

getChange(pDeposit, choiceCost);

}

// calculates the amount of leftover from your deposits

voidgetChange(double *pDeposit, double choiceCost) {

double change = *pDeposit – choiceCost;

*pDeposit = change;

}

// Asks the user if they want another app

voiddoItAgain(char *pQuit) {

char choice;

// use a do-while loop to make sure the user’s choice

// is either y or no.

do {

printf(“would you like to make another purchase? “);

scanf(” %c”, &choice);

choice = toupper(choice);

} while (choice != ‘Y’ && choice != ‘N’);

*pQuit = choice;

}

voiddisplayPurchases(double priceList[ ], intnum) {

int i;

printf(“You made %d purchase(s) as follows:\n”, num);

// display the price of purchases one by one

for (i = 0; i <num; i++) {

printf(“%.2f\n”, priceList[i]);

}

}