'(COBOL) Input character combination, identifying them amd putting a certain condition based on it's combinations, then display the results
Problem:
The first four characters will determine if the card is credit or debit. If it's 'AABB', 'IT22', '1234', 'AAAA', 'ABAB', 'BSIT', 'STB0', '0000' then it's credit. Else it's a debit. Then, if the second four characters are '3133' the transaction will not proceed. If it's a credit card then the last four character combination will determine their credit limit AAAA is equal 700,000.00 BBBB is equal 600,000.00 CCCC is equal 500,000.00 DDDD is equal 400,000.00 EEEE is equal 300,000.00 If it did not match then default to 200,000.00
If debit card, ask for the current savings amount. Then ask for the amount they need to pay. If debit, deduct the amount to pay from their balance.
I tried out my code below, the debit seems to be working, but when it comes to displaying New Balance, it becomes "New balance: 000000"
And when I tried doing the character combination for credit, it didn't work because the code seems to read it as a debit. When others try it, it works for them but when I do it, it doesn't. Have I written something wrong? I use jdoodle when I run this.
IDENTIFICATION DIVISION.
PROGRAM-ID. POS-MACHINE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CARD_ID PIC X(19).
01 FIRST_ROW PIC X(4).
88 CREDIT_COMBINATION VALUE 'AABB', 'IT22', '1234', 'AAAA', 'ABAB', 'BSIT', 'STB0', '0000'.
01 SECOND_ROW PIC X(4).
01 THIRD_ROW PIC X(4).
01 FOURTH_ROW PIC X(4).
01 CREDIT_LIMIT PIC 9(6).
01 DEBIT_CARD PIC X(19).
01 CREDIT_CARD PIC X(19).
01 CURRENT_SAVINGS PIC 9(6).
01 BALANCE PIC 9(7).
01 NEW_BALANCE PIC 9(6) VALUE 0.
01 AMOUNT_TO_PAY PIC 9(6).
01 INSTALLMENT_PLAN PIC X(2).
88 AVAIL VALUE IS 'y'.
88 NOT_AVAIL VALUE IS 'n'.
01 MONTHLY_OPTION PIC 9(3).
01 12_MONTH_INTEREST PIC 9(6).
01 WITH_INTEREST PIC 9(6)V9(2).
01 CREDIT_REMAINING PIC 9(6)V9(2).
01 MONTHLY_DUE PIC 9(6)V9(2).
PROCEDURE DIVISION.
DISPLAY 'Card ID:'.
ACCEPT CARD_ID.
UNSTRING CARD_ID DELIMITED BY '-'
INTO FIRST_ROW, SECOND_ROW, THIRD_ROW, FOURTH_ROW
END-UNSTRING
IF SECOND_ROW NOT EQUAL TO '3133'
IF CREDIT_COMBINATION
MOVE CARD_ID TO CREDIT_CARD
DISPLAY 'CREDIT'
ELSE
MOVE CARD_ID TO DEBIT_CARD
DISPLAY 'DEBIT'
END-IF
ELSE
DISPLAY 'Transaction cannot proceed.'
END-IF
EVALUATE TRUE
WHEN FOURTH_ROW IS EQUAL TO "AAAA"
MOVE 900000 TO CREDIT_LIMIT
WHEN FOURTH_ROW IS EQUAL TO "BBBB"
MOVE 800000 TO CREDIT_LIMIT
WHEN FOURTH_ROW IS EQUAL TO "CCCC"
MOVE 700000 TO CREDIT_LIMIT
WHEN FOURTH_ROW IS EQUAL TO "DDDD"
MOVE 600000 TO CREDIT_LIMIT
WHEN FOURTH_ROW IS EQUAL TO "EEEE"
MOVE 500000 TO CREDIT_LIMIT
WHEN OTHER
MOVE 200000 TO CREDIT_LIMIT
END-EVALUATE
IF NOT CREDIT_COMBINATION
DISPLAY "Savings: "
ACCEPT CURRENT_SAVINGS
DISPLAY "Amount to pay:"
ACCEPT AMOUNT_TO_PAY
SUBTRACT AMOUNT_TO_PAY FROM CURRENT_SAVINGS GIVING NEW_BALANCE
DISPLAY 'New Balance:' NEW_BALANCE
ELSE
IF CREDIT_COMBINATION
DISPLAY "Amount to pay:"
ACCEPT AMOUNT_TO_PAY
END-IF
END-IF
END-IF
STOP RUN.
Solution 1:[1]
if you are using online compiler possibly you need to give your input string in STDIN. With the given input string it should work properly.
instead of Evaluate true series of statments following can be used, same way it is used in the following statement.
IF CREDIT_COMBINATION THEN
MOVE CARD_ID TO CREDIT_CARD
ELSE
MOVE CARD_ID TO DEBIT_CARD
END-IF
Solution 2:[2]
I ran your program and got:
Card ID:
Card ID after UNSTRING: 9876 1245 A012 AERG
FIRST ROW: 9876
DEBIT
so it seems good,,
If your input is good, it should work,
you can write less code by doing the following:
instead of:
INSPECT CARD_ID REPLACING ALL "-" BY SPACE.
UNSTRING CARD_ID DELIMITED BY SPACE INTO FIRST_ROW, SECOND_ROW, THIRD_ROW,
FOURTH_ROW
END-UNSTRING.
you can do:
UNSTRING CARD_ID DELIMITED BY '-'
INTO FIRST_ROW, SECOND_ROW, THIRD_ROW, FOURTH_ROW
END-UNSTRING
and like cobp mention, instead of Evaluate true series you can write:
IF SECOND_ROW NOT EQUAL TO '3133'
IF CREDIT_COMBINATION
MOVE CARD_ID TO CREDIT_CARD
DISPLAY 'CREDIT'
ELSE
MOVE CARD_ID TO DEBIT_CARD
DISPLAY 'DEBIT'
END-IF
ELSE
DISPLAY 'Transaction cannot proceed.'
END-IF
this is the result I got:
Card ID: 9876-1245-A012-AERG
DEBIT
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cobp |
| Solution 2 |
