PROBLEM ANALYSIS
For an introduction to program transaction journal, see the background section of assignment .
Not only transaction journal, but also inventory sheet is needed to print at the end of the day. It is the list of items, inventory cost, inventory units, and inventory amount
When items of the same type were purchased at different unit cost, it is the question as to how to calculate the unit cost. There are several methods, here a periodic average cost (PA cost) method is used.
PA cost = Purchase amount /Units puchased (during the period, regardless of the time, at the beginning or at the end of a period )
For example, with the following transactions
Transaction journal
No Transactions Item Unit cost Units purchased Units sold -- ------------- ----- ----------- --------------- -------------
1 Purchase 001 3.0 30
2 Purchase 001 5.0 20
3 Sale 001 30
4 Purchase 002 1.0 20
5 Sale 002 10
Inventory sheet will be as follows
Inventory sheet
Item Description PA cost Inventory units Inventory amount ---- ------------- --------- ----------------- ------------------
001 pen 3.8 20 68
002 pencil 1.0 10 10
-----------------------------------------------------------------
TOTAL 78
PA cost of item 001 = (3.0 * 30 + 5.0 * 20)/(30 + 20) = 190/50 = 3.8, and PA cost of item 002 = 1.0
SPECIFICATIONS
Your transaction journal and inventory sheet program can
• calculate PA cost,
• display an inventory sheet,
• accept and display item descriptions
Your program displays a menu that offers the user five options:
MAIN MENU
--------------------------
1) Enter a Transaction
2) Edit a Transaction by No
3) View Transaction journal
4) View Inventory sheet NEW
5) Exit
Please select 1, 2, 3, 4, or 5: 1
If 1 is selected, then your program displays the No of transaction and allows the user to enter the transaction detail, including transaction type, item code, item name (only for the first time), unit price (only for puchase transaction), and quantity.
Sample run
Transaction No 1
Enter a transaction type (1 or 2) : 1
Enter a item code : 001
Enter the item name : Pen <= NEW
Enter unit price : -3
**Value must be greater than 0
**Enter again : 3.00
Enter quantity : 10
If the user enters the item code for the first time, your program
• creates the new item code and
• prompts the user to enter a brief description (maximum 30 characters)
Once the user has entered the transaction quantity, your program returns to the main menu
If 2 is selected, the program prompts the user for No of transaction, accepts the No, and displays this transaction details. If transaction No doesn’t exist, the user must be asked to enter again. Then gives the following Edit menu that allows the user to choose which option he/she wants. It allows the user to enter Type of transaction, Item code, Item name, Unit price, or Quantity according with 1, 2, 3, 4, 5 entered. If 6 is entered, it returns to the main menu
Sample run
Enter No of transaction: 10
**Enter again : 1
Type Item code Item name Unit price Quantity
Purchase 001 Pen 3.00 10
Edit Menu
---------
1) Type
2) Item code
3) Item name
4) Unit price
5) Quantity
6) Exit editing
Please select 1, 2, 3, 4, 5, or 6: 5
Enter Quantity : -30
**Value must be greater than 0
**Enter again : 30
Edit Menu
---------
1) Type
2) Item code
3) Item name
4) Unit price
5) Quantity
6) Exit editing
Please select 1, 2, 3, 4, 5, or 6: 6
MAIN MENU
--------------------------
1) Enter a Transaction
2) Edit a Transaction by No
3) View Transaction journal
4) View Inventory sheet
5) Exit
Please select 1, 2, 3, 4, or 5: 3
If 3 is seleted, then program displays all transactions that have been entered, in the order entered. The format of the transaction journal is as follows:
Transaction journal
No Transactions Item Unit cost Units purchased Units sold -- ------------- ----- ----------- --------------- -------------
1 Purchase 001 3.0 30
2 Purchase 001 5.0 20
3 Sale 001 3.8 30
4 Purchase 002 1.0 20
5 Sale 002 1.0 10
Here, the unit cost of the item sold is equal to PA cost.
If 4 is seleted, then program displays inventory sheet. The format of the inventory sheet is as follows:
Inventory sheet
Item Description PA cost Inventory units Inventory amount ---- ------------- --------- ----------------- ------------------
001 Pen 3.8 20 68
002 Pencil 1.0 10 10
-----------------------------------------------------------------
TOTAL 78
Expand|Select|Wrap|Line Numbers
- // gr.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_ITM 999 //maximum of the item
- #define MAX_TRS 20 //maximum of the transaction number
- #define MAX 10 //maximum of different items
- typedef struct trans{
- int tpe; //type
- int itm; //item
- double cst; //cost
- int qtt; //quantity
- } transaction; //transaction
- double input_ctrl(double inf, double sup, int opt);/* get an interger or a number in double type */
- int input_chck(double value, double inf, double sup, char ch);/* check if input is valid */
- void *tst_input(transaction *t, int *n);/* enter the transaction */
- int tst_edit(transaction *t, int n); /* allow users editing a transaction by number */
- int tst_view(transaction *t, int n, int opt);/* print the transactions to the screen */
- int tst_find(transaction *t, int n, int itm);/* find a number through cost item of a transaction */
- int main() {
- int choice, no = 0;
- transaction *tst;
- /* make a allocation for *tst*/
- tst = (transaction*) malloc( sizeof(transaction));
- do {
- printf("MAIN MENU\n");
- printf("=========\n");
- printf("1) Enter a Transaction.\n");
- printf("2) Edit a Transaction by No.\n");
- printf("3) View Transaction journal.\n");
- printf("4) Exit.\n");
- printf("Please select 1, 2, 3, or 4: ");
- choice = (int) input_ctrl(1, 4, 0);
- switch (choice) {
- case 1:
- tst = (transaction*) tst_input( tst, &no);
- break;
- case 2:
- tst_edit( tst, no);
- break;
- case 3:
- tst_view( tst, no, -1);
- }
- } while (choice != 4);
- printf("The application has stopped.");
- getch();
- return 0;
- } //end main()
- double input_ctrl(double inf, double sup, int opt) {
- if ( opt == 0) { //get an integer
- int value, temp;
- char ch;
- do {
- temp = scanf("%d%c", &value, &ch);
- fflush(stdin);
- if (temp == 0)
- ch = '\0'; //NUL
- } while (!input_chck(value, inf, sup, ch));
- return value;
- } else if (opt == 1){ //get a number type double
- double value, temp;
- char ch;
- do {
- temp = scanf("%lf%c", &value, &ch);
- fflush(stdin);
- if (temp == 0)
- ch = '\0'; //NUL
- } while (!input_chck(value, inf, sup, ch));
- return value;
- } else return -1;
- } //end input_ctrl
- int input_chck(double value, double inf, double sup, char ch) {
- if (inf < sup) {
- if (ch == '\n') {
- if (value < inf) {
- printf(" ** Input must be greater than %.0lf . Enter again : ", inf);
- return 0;
- } else if (value > sup) {
- printf(" ** Input must be less than %.0lf . Enter again : ", sup);
- return 0;
- } else
- return 1;
- } else { //ch != '\n'
- printf(" **Input is invalid. Enter again : ");
- return 0;
- }
- } else if (inf == sup) {
- if (ch == '\n') {
- if (value < inf) {
- printf(" ** Input must be greater than %.0lf . Enter again : ", inf);
- return 0;
- } else
- return 1;
- } else { //ch != '\n'
- printf(" ** Input is invalid. Enter again : ");
- return 0;
- }
- } else //inf > sup
- return -1;
- } //end input_chck
- void *tst_input(transaction *pt, int *n) {
- char ctrl;
- int itm, temp;
- do {
- if ( *n < MAX_TRS) {
- if (*n > 0)
- pt = (transaction*) realloc ( pt, sizeof(transaction)*(*n+1));
- printf("\nTransaction No %d\n", *n+1);
- printf("Enter a transaction type (1 for Purchase, 2 for Sale) : ");
- (pt+*n)->tpe = (int) input_ctrl(1, 2, 0);
- do {
- printf("Enter a item code : ");
- itm = (int) input_ctrl(1, MAX_ITM, 0);
- temp = tst_find( pt, *n, itm);
- if ( temp >= MAX)
- printf(" ** Error. Cannot enter more than %d of different items.\n",MAX);
- } while ( temp >= MAX);
- (pt+*n)->itm = itm;
- if ( (pt+*n)->tpe == 1) {
- printf("Enter unit cost : ");
- (pt+*n)->cst = input_ctrl(0, 0, 1);
- } else //(t+*n)->.tpe == 2
- (pt+*n)->cst = 0;
- printf("Enter quantity : ");
- (pt+*n)->qtt = (int) input_ctrl(1, 1, 0);
- printf("Press any key to continue (M to go back the menu) ");
- scanf("%c", &ctrl);
- ++*n;
- } else {
- printf(" ** The transaction number cannot be greater %d.\n", MAX_TRS);
- printf("Press any key to continue... ");
- ctrl = 'm';
- }
- } while (ctrl != 'M' && ctrl != 'm');
- printf("\n\n");
- return pt;
- } //end trans_input
- int tst_edit(transaction *pt, int n) {
- int ctrl, itm, tpe, i, temp;
- printf("\nEnter No of transaction: ");
- i = (int) input_ctrl(1, n, 0) - 1;
- tst_view(pt, 0, i);
- do {
- printf("Edit Menu\n");
- printf("---------\n");
- printf("1) Type.\n");
- printf("2) Item code.\n");
- printf("3) Unit cost.\n");
- printf("4) Quantity.\n");
- printf("5) Exit editing.\n");
- printf("Please select 1, 2, 3, 4, or 5: ");
- ctrl = (int) input_ctrl(1, 5, 0);
- switch( ctrl) {
- case 1:
- printf("Enter Type : ");
- tpe = (int) input_ctrl(1, 2, 0);
- if ( (pt+i)->tpe == 2 && tpe == 1) {
- printf("Enter Cost : ");
- (pt+i)->cst = input_ctrl(0, 0, 1);
- }
- (pt+i)->tpe = tpe;
- break;
- case 2:
- do {
- printf("Enter Item Code : ");
- itm = (int) input_ctrl(1, MAX_ITM, 0);
- temp = tst_find( pt, n, itm);
- if ( temp >= MAX)
- printf(" ** Error. Cannot enter more than %d of different items.\n",MAX);
- } while ( temp >= MAX);
- (pt+i)->itm = itm;
- break;
- case 3:
- if ( (pt+i)->tpe == 1) {
- printf("Enter Cost : ");
- (pt+i)->cst = input_ctrl(0, 0, 1);
- } else
- printf(" ** Error. Cannot edit this item.\n");
- break;
- case 4:
- printf("Enter Quantity : ");
- (pt+i)->qtt = (int) input_ctrl(1, 1, 0);
- default:
- ctrl=0;
- }
- printf("\n");
- } while (ctrl != 0);
- return 0;
- } //end trans_edit
- int tst_view(transaction *pt, int n, int opt) {
- int i;
- if (opt == -1) {
- printf("\n Transaction journal \n\n");
- printf("No Transactions Item Unit cost Units purchased Units sold\n");
- printf("-- ------------ ---- --------- --------------- ----------\n");
- for (i = 0; i < n; ++i) {
- if ( (pt+i)->tpe == 1)
- printf("%2d Purchase %03d %9.1lf %15d %10s\n",i+1, (pt+i)->itm, (pt+i)->cst, (pt+i)->qtt," ");
- else //(t+i)->tpe == 2
- printf("%2d Sale %03d %9s %15s %10d\n", i+1, (pt+i)->itm, " ", " ",(pt+i)->qtt);
- }
- printf("Press any key to continue... ");
- getch();
- printf("\n");
- } else { //(opt >= 0)
- if ( (pt+opt)->tpe == 1) {
- printf("Type Item code Unit cost Quantity\n");
- printf("Purchase %03d %9.1lf %8d\n",(pt+opt)->itm, (pt+opt)->cst, (pt+opt)->qtt);
- }
- else { //(t+opt)->tpe == 2
- printf("Type Item code Quantity\n");
- printf("Sale %03d %8d\n", (pt+opt)->itm, (pt+opt)->qtt);
- }
- printf("\n");
- }
- printf("\n");
- return 0;
- } //end trans_view
- int tst_find(transaction *t, int n, int itm) {
- int i, temp = 0;
- for (i = 0; i < n; ++i) {
- if ( itm != (t+i)->itm)
- ++temp;
- }
- return temp;
- } //end tst_find