IGCSE cs 0478 2023 Jun 22 12

IGCSE cs 0478 2023 Jun 22 12

A two-dimensional (2D) array Account[] contains account holders’ names and passwords for a banking program.

A 2D array AccDetails[] has three columns containing the following details:

  • column one stores the balance – the amount of money in the account, for example 250.00

  • column two stores the overdraft limit – the maximum total amount an account holder can borrow from the bank after the account balance reaches 0.00, for example 100.00

  • column three stores the withdrawal limit – the amount of money that can be withdrawn at one time, for example 200.00

The amount of money in a bank account can be negative (overdrawn) but not by more than the overdraft limit.

For example, an account with an overdraft limit of 100.00 must have a balance that is greater than or equal to –100.00

Suitable error messages must be displayed if a withdrawal cannot take place, for example if the overdraft limit or the size of withdrawal is exceeded.

The bank account ID gives the index of each account holder’s data held in the two arrays. For example, account ID 20’s details would be held in:

Account[20,1] and Account[20,2]

AccDetails[20,1] AccDetails[20,2] and AccDetails[20,3]

The variable Size contains the number of accounts.

The arrays and variable Size have already been set up and the data stored.

Write a program that meets the following requirements:

  • checks the account ID exists and the name and password entered by the account holder match the name and password stored in Account[] before any action can take place

  • displays a menu showing the four actions available for the account holder to choose from:

    • display balance

    • withdraw money

    • deposit money

    • exit

  • allows an action to be chosen and completed. Each action is completed by a procedure with a parameter of the account ID.

You must use pseudocode or program code and add comments to explain how your code works. All inputs and outputs must contain suitable messages.

You only need to declare any local arrays and local variables that you use.

You do not need to declare and initialise the data in the global arrays Account[] and AccDetails[] and the variable Size

Python版初始化代码,练习的时候使用如下的代码


import string
import random

#################################################
# Declare
#################################################

size = 100
account = []
acc_details = []

#################################################
# Initialize Data (not include)
#################################################

for account_index in range(size):
    holder_name = ''.join(random.choice(string.ascii_letters) for name_letter_index in range(6))
    holder_password = ''.join(random.choice(string.digits) for password_index in range(6))
    account.append([holder_name, holder_password])
    account_balance = round(random.random() * 1000, 2)
    account_overdraft_limit = round(random.randint(1, 9) * 100.00, 2)
    account_withdrawal_limit = round(random.randint(1, 9) * 100.00, 2)
    acc_details.append([account_balance, account_overdraft_limit, account_withdrawal_limit])


#################################################
# For you to write
#################################################

def check_details(acc_id: int):
    pass


def balance(acc_id: int):
    pass


def with_drawal(acc_id: int):
    pass


def deposit(acc_id: int):
    pass
    

图解

Python版答案


import string
import random

#################################################
# Declare
#################################################

size = 100
account = []
acc_details = []

#################################################
# Initialize Data (not include)
#################################################

for account_index in range(size):
    holder_name = ''.join(random.choice(string.ascii_letters) for name_letter_index in range(6))
    holder_password = ''.join(random.choice(string.digits) for password_index in range(6))
    account.append([holder_name, holder_password])
    account_balance = round(random.random() * 1000, 2)
    account_overdraft_limit = round(random.randint(1, 9) * 100.00, 2)
    account_withdrawal_limit = round(random.randint(1, 9) * 100.00, 2)
    acc_details.append([account_balance, account_overdraft_limit, account_withdrawal_limit])

print(account)

#################################################
# For you to write
#################################################

def check_details(acc_id: int):
    name = ""
    password = ""
    valid = False
    if acc_id < 0 or acc_id > size:
        print("Invalid account number")
        return valid
    else:
        name = input("Please enter name: ")
        password = input("Please enter password: ")
        # Check the account name and password
        if name != account[acc_id][0] or password != account[acc_id][1]:
            print("Invalid name or password")
        else:
            valid = True
            return valid


def balance(acc_id: int):
    print("Your balance is ", acc_details[acc_id][0])


def with_drawal(acc_id: int):
    amount = float(input("Please enter amount to withdraw: "))
    # check the input withdrawal amount valid
    while amount > acc_details[acc_id][2] or amount > acc_details[acc_id][1] + acc_details[acc_id][2] or amount < 0:
        if amount > acc_details[acc_id][2]:
            print("Amount greater than withdrawal limit")
        if amount > acc_details[acc_id][1] + acc_details[acc_id][2]:
            print("Amount greater than cash available")
        if amount < 0:
            print("Amount must be positive")
        amount = float(input("Please reenter amount to withdraw: "))
    acc_details[acc_id][0] = acc_details[acc_id][0] - amount


def deposit(acc_id: int):
    amount = float(input("Please enter a positive amount to deposit: "))
    # check the input deposit amount valid
    while amount <= 0:
        amount = float(input("Please enter a positive amount to deposit "))
    acc_details[acc_id][0] = acc_details[acc_id][0] + amount


account_number = int(input("Please enter your account number: "))
valid = check_details(account_number)
choice = 0

if valid:
    while choice != 4:
        # display menu
        print("Menu")
        print("1. display balance")
        print("2. withdraw money")
        print("3. deposit money")
        print("4. exit")
        choice = int(input("please choose 1, 2, 3 or 4: "))
        if choice == 1:
            balance(account_number)
        elif choice == 2:
            with_drawal(account_number)
        elif choice == 3:
            deposit(account_number)
        elif choice == 4:
            print("finish")
        else:
            print("Invalid choice")
else:
    print("Invalid account number ")

Last updated

Was this helpful?