# A New Car Price Calculator

### Venit Case Study 3.6,

By: Nicholas Duchon

## Table 4.3 - the data for this example:

 Category Option Code Price Delta from cheapest Delta from dearest Engine 6 cylinder S 150 0 -600 8 cylinder E 475 325 -275 Diesel D 750 600 0 Interior Trim Vinyl V 50 0 -750 Cloth C 225 175 -575 Leather L 800 750 0 Radio AM/FM/CD/DVD C 100 0 -300 with GPS P 400 300 0

## Analysis - the total cost is given by the following formula:

SellingPrice = BasePrice + EngineCost + TrimCost + RadioCost + ShippingCharge + Dealer Charge

Notes on variables:

• BasePrice - input by user as double
• EngineCost - code input by user, program determines corresponding cost
• TrimCost - as EngineCost
• ShippingCharge - \$500 always
• DealerCharge - \$175 always

What if:

• What if the user does not select a code from the defined ones?
1. Pester the user until they enter a valid code.
2. Take the cheapest option as the default.
3. Take the most expensive option as the default.

## Scenarios

Each scenario will require specifications of the 3 categories, and the base price.

 Input Calculation Expected output "The total price is: \$" + Notes 20000 S V C 20000 + 150 + 50 + 100 + 675 20,975 All valid selections 15000 D L P 15000 + 750 + 800 + 400 + 675 17,625 All valid selections 30000 D L P 30000 + 750 + 800 + 400 + 675 32,625 All valid selections 20000 q q r S F F V H H C 20000 + 150 + 50 + 100 + 657 20,975 Option 1 20000 q q r 20000 + 150 + 50 + 100 + 657 20,975 Option 2 20000 q q r 20000 + 750 + 800 + 400 + 675 22,625 Option 3

I have added some of the key features of pseudo-code we have discussed in class:

• Global variables are declared BEFORE main - variables that are directly available to all functions without using parameter passing. Note that Venit does not introduce parameter passing in functions until Chapter 7.
• All modules have a header line - declaring the start of the module and its name
• All modules, including main, have an explicit "end module" statement
• Indentation conventions are carefully followed
• This example is complete, not just code fragments
• The default cannot simply be a message to the user - some value MUST be assigned to all the variables in this program
• The shipping charge and dealer charge are set in variables at the start of the program, since they might change sometime in the future. This way, the values are easy to understand and if they need to be changed, the programmer only needs to look in one place.

## Option 2, the simplest

// Declare global variables:
Declare BasePrice, EngineCost, TrimCost, RadioCost, ShippingCharge, DealerCharge as Float
main
ShippingCharge = 500
DealerCharge = 175

call welcomeMessage
call getBasePrice
call getEngineCost
call getTrimCost
call displaySellingPrice
end main

module welcomeMessage
write "Welcome to the New Car Price Calculator"
write "Enter the base price, and codes for engine, trim and radio options"
write "This program will determine the final cost of the car"
write "including the shipping and dealer charges."
end module welcomeMessage

module getBasePrice
write "What is the base price for this model?"
input BasePrice
end module getBasePrice

module getEngineCost
declare code as character
write "Engine codes:"
write "  S - 6 cylinder, \$150"
write "  E - 8 cylinder, \$475"
write "  D - Diesel, \$750"
write "Select your engine code (S, E, D):"
input code
Select Case of code
Case "S"
EngineCost = 150
Break
Case "E"
EngineCost = 475
Break
Case "D"
EngineCost = 750
Break
Default
write "Invalid selection, defaulting to 6 cylinder"
EngineCost = 150
End Case
end module getEngineCost

module getTrimCost
declare code as character
write "Trim codes:"
write "  V - vinyl, \$50"
write "  C - cloth, \$225"
write "  L - leather, \$800"
write "Select your trim code (V, C, L):"
input code
Select Case of code
Case "V"
TrimCost = 50
Break
Case "C"

TrimCost = 225
Break
Case "L"

TrimCost = 800
Break
Default
write "Invalid selection, defaulting to cloth"

TrimCost = 50
End Case
end module getTrimCost

declare code as character
write "  C - AM/FM/CD/DVD, \$100"
write "  P - with GPS, \$400"
input code
Select Case of code
Case "C"

Break
Case "C"

Break
Default
write "Invalid selection, defaulting to AM/FM/CD/DVD"

End Case

module displaySellingPrice
declare SellingPrice as Float
SellingPrice = BasePrice + EngineCost + TrimCost + RadioCost + ShippingCharge + DealerCharge
write "The total cost of this car is \$" + SellingPrice
end module displaySellingPrice

## Option 3, also simple

// Declare global variables:
Declare BasePrice, EngineCost, TrimCost, RadioCost, ShippingCharge, DealerCharge as Float
main
ShippingCharge = 500
DealerCharge = 175

call welcomeMessage
call getBasePrice
call getEngineCost
call getTrimCost
call displaySellingPrice
end main

module welcomeMessage
write "Welcome to the New Car Price Calculator"
write "Enter the base price, and codes for engine, trim and radio options"
write "This program will determine the final cost of the car"
write "including the shipping and dealer charges."
end module welcomeMessage

module getBasePrice
write "What is the base price for this model?"
input BasePrice
end module getBasePrice

module getEngineCost
declare code as character
write "Engine codes:"
write "  S - 6 cylinder, \$150"
write "  E - 8 cylinder, \$475"
write "  D - Diesel, \$750"
write "Select your engine code (S, E, D):"
input code
Select Case of code
Case "S"
EngineCost = 150
Break
Case "E"
EngineCost = 475
Break
Case "D"
EngineCost = 750
Break
Default
write "Invalid selection, defaulting to Diesel"
EngineCost = 750
End Case
end module getEngineCost

module getTrimCost
declare code as character
write "Trim codes:"
write "  V - vinyl, \$50"
write "  C - cloth, \$225"
write "  L - leather, \$800"
write "Select your trim code (V, C, L):"
input code
Select Case of code
Case "V"
TrimCost = 50
Break
Case "C"

TrimCost = 225
Break
Case "L"

TrimCost = 800
Break
Default
write "Invalid selection, defaulting to leather"

TrimCost = 800
End Case
end module getTrimCost

declare code as character
write "  C - AM/FM/CD/DVD, \$100"
write "  P - with GPS, \$400"
input code
Select Case of code
Case "C"

Break
Case "C"

Break
Default
write "Invalid selection, defaulting to AM/FM/CD/DVD with GPS"

End Case

module displaySellingPrice
declare SellingPrice as Float
SellingPrice = BasePrice + EngineCost + TrimCost + RadioCost + ShippingCharge + DealerCharge
write "The total cost of this car is \$" + SellingPrice
end module displaySellingPrice

## Option 1, rather more complex

// Declare global variables:
Declare BasePrice, EngineCost, TrimCost, RadioCost, ShippingCharge, DealerCharge as Float
main
ShippingCharge = 500
DealerCharge = 175

call welcomeMessage
call getBasePrice
call getEngineCost
call getTrimCost
call displaySellingPrice
end main

module welcomeMessage
write "Welcome to the New Car Price Calculator"
write "Enter the base price, and codes for engine, trim and radio options"
write "This program will determine the final cost of the car"
write "including the shipping and dealer charges."
end module welcomeMessage

module getBasePrice
write "What is the base price for this model?"
input BasePrice
end module getBasePrice

module getEngineCost
declare code as character
boolean invalidFlag
do
invalidFlag = false
write "Engine codes:"
write "  S - 6 cylinder, \$150"
write "  E - 8 cylinder, \$475"
write "  D - Diesel, \$750"
write "Select your engine code (S, E, D):"
input code
Select Case of code
Case "S"
EngineCost = 150
Break
Case "E"
EngineCost = 475
Break
Case "D"
EngineCost = 750
Break
Default
write "Invalid selection, try again"
invalidFlag = true
End Case
while (invalidFlag)
end module getEngineCost

module getTrimCost
declare code as character
boolean invalidFlag
do
invalidFlag = false

write "Trim codes:"
write "  V - vinyl, \$50"
write "  C - cloth, \$225"
write "  L - leather, \$800"
write "Select your trim code (V, C, L):"
input code
Select Case of code
Case "V"
TrimCost = 50
Break
Case "C"

TrimCost = 225
Break
Case "L"

TrimCost = 800
Break
Default
write "Invalid selection, try again"
invalidFlag = true
End Case
while (invalidFlag)
end module getTrimCost

declare code as character
boolean invalidFlag
do
invalidFlag = false
write "  C - AM/FM/CD/DVD, \$100"
write "  P - with GPS, \$400"
input code
Select Case of code
Case "C"

Break
Case "C"

Break
Default
write "Invalid selection, try again"

invalidFlag = true
End Case
while (invalidFlag)