Register Question

Joined
Oct 21, 2024
Messages
1
Reaction score
0
Python:
#!/bin/python3

import math
import os
import random
import re
import sys

# PART 1 - Implement Customer Register ADT

# Constructor
def makeCustomerRegister():
    return ("CR", [])

# Selector to get contents of the register
def contents(cr):
    if isCustomerRegister(cr):
        return cr[1]
    else:
        raise ValueError("Invalid Customer Register provided")

# Mutator to add a customer to the register
def addCustomer(custRec, cr):
    if isCustomerRegister(cr):
        cr[1].append(custRec)
    else:
        raise ValueError("Invalid Customer Register provided")

# Mutator to remove a customer from the register by their customer ID
def removeCustomer(cid, cr):
    if isCustomerRegister(cr):
        cr[1] = [record for record in cr[1] if record[0] != cid]
    else:
        raise ValueError("Invalid Customer Register provided")

# Predicate to check if an object is a valid Customer Register
def isCustomerRegister(cr):
    return isinstance(cr, tuple) and len(cr) == 2 and cr[0] == "CR" and isinstance(cr[1], list)

# Predicate to check if the Customer Register is empty
def isEmpty(cr):
    if isCustomerRegister(cr):
        return len(cr[1]) == 0
    else:
        raise ValueError("Invalid Customer Register provided")

# Part 2: Define Functions to Sign Up A New Customer

def generateCId(cName, cr, platinum=False):
    first_initial = cName[0][0].upper()
    last_initial = cName[1][0].upper()
    ascii_sum = ord(first_initial) + ord(last_initial)
    prefix = "PC" if platinum else "NC"
    cid = f"{prefix}{ascii_sum}"
    existing_cids = [cust[0] for cust in contents(cr)]
    while cid in existing_cids:
        ascii_sum += 1
        cid = f"{prefix}{ascii_sum}"
    return cid

def makeCustomerRecord(cid, hh, mm):
    return [
        cid,               # Customer ID
        (hh, mm),          # Arrival Time (HH, MM)
        -1,                # Service time (not assessed yet, so set to -1)
        []                 # Vehicle information (empty list, will be updated later)
    ]

# Update Functions
def updateVehicle(vhcl, custRec):
    custRec[3] = vhcl

def updateServiceTime(serviceTime, custRec):
    custRec[2] = serviceTime

def updateServiceCost(serviceCost, custRec):
    if custRec[3]:
        custRec[3][-1] = serviceCost

# Selector Functions for accessing customer record details
def getCID(custRec):
    return custRec[0]

def getArivalTime(custRec):
    hh, mm = custRec[1]
    return f"{hh:02}{mm:02}"

def getServiceTime(custRec):
    return custRec[2]

def getCustType(custRec):
    return "PC" if custRec[0].startswith("PC") else "NC"

def getVehicle(custRec):
    return custRec[3]

# Part 3: Adding Vehicle Information
def addVehicle(custReg, cid, pl, mk, md, y, ml, lsd):
    """
    Adds vehicle information to a customer record in the Customer Register.
    :param custReg: The existing Customer Register.
    :param cid: Customer ID to locate the record.
    :param pl: Pay Limit.
    :param mk: Make of the vehicle.
    :param md: Model of the vehicle.
    :param y: Year of the vehicle.
    :param ml: Mileage on the vehicle.
    :param lsd: Last service date in months.
    :return: Updates Customer Register with vehicle info added to the corresponding customer.
    """
    for custRec in contents(custReg):
        if getCID(custRec) == cid:
            vehicle_info = [pl, (mk, md, y), ml, lsd, 0]
            updateVehicle(vehicle_info, custRec)

# Main Program

if __name__ == '__main__':   
    no_of_customers = int(input())
    
    custReg = makeCustomerRegister()
    
    for i in range(no_of_customers):
        customer_info = input().strip().split(' ')
        
        if isCustomerRegister(custReg):
            # Check if the customer is Platinum based on the original ID
            platinum = customer_info[0][0] == "P"
            
            # Create the customer record directly using parsed information
            addCustomer(
                [
                    customer_info[0],
                    (int(customer_info[3]), int(customer_info[4])),
                    -1,
                    [
                        customer_info[5],
                        (customer_info[6], customer_info[7], customer_info[8]),
                        int(customer_info[9]),
                        customer_info[10],
                        0
                    ]
                ],
                custReg
            )
            
            # Use addVehicle to update the vehicle information, including adjusting mileage
            addVehicle(
                custReg,
                customer_info[0],
                customer_info[5],
                customer_info[6],
                customer_info[7],
                customer_info[8],
                int(customer_info[9]) + 5,  # Update mileage by adding 5
                customer_info[10]
            )
            
    # Print the remaining customers in the register
    for cust in contents(custReg):
        print(
            getCID(cust),
            getArivalTime(cust),
            getServiceTime(cust),
            getCustType(cust),
            getVehicle(cust)
        )


Here is the sample :
Sample Input 0
6
PC140 Paul Clover 09 29 50000 Honda Civic 2019 25642 4
PC157 John Smith 10 30 100000 BMW X5 2017 71000 6
NC141 Karl Brown 07 15 1000000 Ferrari 458 2020 10000 3
NC132 Andrea Chambers 10 50 50000 Toyota Yaris 2012 342986 7
PC180 Zechariah Zephaniah 10 15 25000 Toyota Corolla 1998 709642 24
NC146 Alphanso Quenzentine 11 13 23900 Suzuki Swift 1999 567086 12

Sample Output 0
0924
1025
0710
1215
1240
1343
PC140 0929 0000 PC ['50000', ('Honda', 'Civic', '2019'), 25647, '4', 0.0]
PC157 1030 0000 PC ['100000', ('BMW', 'X5', '2017'), 71005, '6', 0.0]
NC141 0715 0000 NC ['1000000', ('Ferrari', '458', '2020'), 10005, '3', 0.0]
NC132 1050 0130 NC ['50000', ('Toyota', 'Yaris', '2012'), 342991, '7', 30000.0]
PC180 1015 0230 PC ['25000', ('Toyota', 'Corolla', '1998'), 709647, '24', 0.0]
NC146 1113 0235 NC ['23900', ('Suzuki', 'Swift', '1999'), 567091, '12', 25000.0]

Sample Input 1
10
PC140 Paul Clover 09 29 1150000 Honda Civic 2014 25642 4
PC157 John Smith 10 30 11100000 BMW X5 2010 71000 6
NC141 Karl Brown 07 15 111000000 Ferrari 458 2010 10000 3
NC132 Andrea Chambers 10 50 1110000 Toyota Yaris 2012 342986 7
PC180 Zechariah Zephaniah 10 15 1115000 Toyota Corolla 1998 709642 24
NC146 Alphanso Quenzentine 11 13 1120000 Suzuki Swift 1999 567086 12
NC145 Lucca Esquivel 05 46 117000 Benz Mercedes 2020 100000 4
NC162 Kristie Werner 08 30 111000 Honda Fit 2015 70000 10
NC147 Abubakr Rivas 09 15 1171000 BMW X6 2017 120946 22
NC159 Moesha Read 09 30 1167000 Honda Fit 2015 340138 17

Sample Output 1
1154
1355
1400
1215
1240
1343
0611
0825
1040
1125
PC140 0929 0230 PC ['1150000', ('Honda', 'Civic', '2014'), 25647, '4', 45000.0]
PC157 1030 0330 PC ['11100000', ('BMW', 'X5', '2010'), 71005, '6', 90000.0]
NC141 0715 0650 NC ['111000000', ('Ferrari', '458', '2010'), 10005, '3', 245000.5]
NC132 1050 0130 NC ['1110000', ('Toyota', 'Yaris', '2012'), 342991, '7', 30000.0]
PC180 1015 0230 PC ['1115000', ('Toyota', 'Corolla', '1998'), 709647, '24', 40000.0]
NC146 1113 0235 NC ['1120000', ('Suzuki', 'Swift', '1999'), 567091, '12', 33000.0]
NC145 0546 0030 NC ['117000', ('Benz', 'Mercedes', '2020'), 100005, '4', 7800.0]
NC162 0830 0000 NC ['111000', ('Honda', 'Fit', '2015'), 70005, '10', 0.0]
NC147 0915 0130 NC ['1171000', ('BMW', 'X6', '2017'), 120951, '22', 46000.0]
NC159 0930 0200 NC ['1167000', ('Honda', 'Fit', '2015'), 340143, '17', 80000.0]



I just can't get the code to this part 4 to work:
def convertHHMMtoMin(time):
return int(time[:2]) * 60 + int(time[2:])
def assessVehicle(customerReg, CID, SCL):

for customer in contents(customerReg):
if CID == getCID(customer):
customerRec = customer
break

service_costs = 0
service_time = 0
customer_costs = []
customer_vchl = getVehicle(customerRec)

if date.today().year - getYear(customer_vchl) > 5 and getMake(customer_vchl) != "Benz":
customer_costs.append(getFullHouseInfo(SCL, getMake(customer_vchl)))
service_costs += getServCost(SCL, getMake(customer_vchl), "FH" )
service_time += convertHHMMtoMin(getActivTime(SCL, getMake(customer_vchl), "FH"))
elif getMile(customer_vchl) > 100000:
customer_costs.append(getShocksInfo(SCL, getMake(customer_vchl)))
service_costs += getServCost(SCL, getMake(customer_vchl), "SH")
service_time += convertHHMMtoMin(getActivTime(SCL, getMake(customer_vchl), "SH"))

if int(getLastServDate(customer_vchl)) > 10:
customer_costs.append(getTyreInfo(SCL, getMake(customer_vchl)))
service_costs += getServCost(SCL, getMake(customer_vchl), "TY")
service_time += convertHHMMtoMin(getActivTime(SCL, getMake(customer_vchl), "TY"))

adjusted_cost = service_costs
treshold = 1.05 * float(getPayLimit(customer_vchl))
while customer_costs != []:
if adjusted_cost > treshold:
adjusted_cost -= cost(min(customer_costs))
customer_costs.remove(min(customer_costs))
else:
break

pickup_time = convertHHMMtoMin(getArivalTime(customerRec)) + service_time - 5

service_time = str(service_time//60).rjust(2, "0") + str(service_time % 60).rjust(2, "0")

for customer in contents(customerReg):
if CID == getCID(customer):
updateServiceTime(service_time, customer)
updateServiceCost(float(adjusted_cost), customer)
break
return "".join((str(pickup_time // 60).rjust(2,"0"), str(pickup_time % 60).rjust(2,"0")))
 

Attachments

  • Screenshot 2024-10-21 120510.png
    Screenshot 2024-10-21 120510.png
    188.6 KB · Views: 1

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,943
Messages
2,570,128
Members
46,611
Latest member
SusanneWis

Latest Threads

Top