I made it pretty far in writing the code for an assignment. However, I am struggling to figure out how to finish the code. Can anyone show me how to finish it?
4 ビュー (過去 30 日間)
古いコメントを表示
PROBLEM:
Most major airports have separate lots for long term and short term parking. The cost to park depends on the lot you select and how long you stay. Consider this rate structure from the Salt Lake International Airport during the summer of 2016.
Long term economy parking:
the first hour is $2 and each additional hour or fraction thereof is $1.
Daily max $9
weekly max $60
Short term parking:
the first 30 mins are $2 and each additional 20 min or fraction thereof is $1
Daily max $32
Create a program that asks the user the following: which lot are you using? how many weeks, hours, days, and minutes did you park? your program shoud then calculate the parking bill.
Daily maximum of $9 means $9 for each full day; weekly maximum of $60 means $60 for each full week, etc... Assume the user enters positive integers for minutes/days/weeks/hours and that the user will enter appropriate number of days/weeks/hours. Program should account for user entering minutes greater than 60 and if the user enters an incorrect lot type. Be sure to consider the situations when hours and/or minutes are entered as zero - test and account for these cases.
CODE I HAVE SO FAR:
%clear the workspace and command window
clear, clc;
%program description
disp('This program will calculate airport parking fees.');
%{
list of variables with a short description:
lotType = type of parking lot chosen by the user (long-term/short-term)
weeks = user value for number of weeks parked
days = user value for number of days parked
hours = user value for number of hours parked
minutes = user value for number of minutes parked
weekly_long_max = maximum amount of money one can pay per week for long term parking ($)
daily_long_max = maximum amount of money one can pay per day for long term parking($)
max_minutes_short = maximum amount of money one can pay in 30 minutes for short term parking ($)
daily_short_max = maximum amount of money one can pay per day for short
term parking ($)
weekly_short_max = maximum amount of money one can pay per week for short term parking ($)
%}
%variable initialization
disp('Enter "1" for long-term parking.')
disp('Enter "2" for short-term parking.')
lotType = input('Which lot are you using?: ');
disp('How many weeks, hours, days, and minutes did you park?')
weeks = input('Please enter weeks first: ');
days = input('Please enter days next: ');
hours = input('Please enter hours next: ');
minutes = input('Please enters minutes next: ');
%calculations
if lotType == 1 %long-term parking
disp('Ok, for long-term parking:')
if weeks == 0
weekly_charge = 0;
elseif weeks >= 1
weekly_charge = 60*weeks;
end
if days == 0
daily_charge = 0;
elseif days >= 1
daily_charge = 9*days;
end
if hours == 0
hourly_charge = 0;
elseif hours >= 1
hourly_charge = 2 + (hours - 1);
end
if (weeks == 0 & days == 0 & hours == 0 & minutes == 0)
money_charged_per_minute = 0;
elseif (weeks == 0 & days == 0 & hours == 0 & minutes == 1)
money_charged_per_minute = 2;
elseif minutes >=1
money_charged_per_minute = 1;
end
end
if lotType == 2
disp('Ok, for short-term parking:')
if weeks == 0
weekly_charge = 0;
else
weeks >= 1;
weekly_charge = (weeks*7)*32;
end
end
if days == 0
daily_charge = 0;
elseif days >= 1
daily_charge = days*32;
end
total_minutes = (hours*60) + minutes
if total_minutes >= 1
money_charged_per_minute = 2 %first 30 minutes = $2
total_minutes_calc =
end
0 件のコメント
回答 (1 件)
Sam Chak
2025 年 2 月 21 日
Hi @Antonio
I believe that your app should allow users to input their parking start date-time and exit date-time, almost similar to booking an airline ticket. The app should be capable of automatically calculating relevant details. It is important to make the app user-friendly.
Below is a code snippet; it is not a complete solution. You can extend it to calculate the total weeks, remaining months, and remaining hours, and then potentially use a 'switch-case' statement to enable the program to apply the appropriate parking rates.
% Enter the departure and return datetimes
parkDate = datetime('2025-02-21 14:00', 'InputFormat', 'yyyy-MM-dd HH:mm');
exitDate = datetime('2025-03-03 13:00', 'InputFormat', 'yyyy-MM-dd HH:mm');
% Calculate the difference
timeDifference = exitDate - parkDate;
% Extract total days and remaining hours
numDays = floor(days(timeDifference)); % Use floor to get complete days
totalHours = hours(timeDifference);
numHours = rem(totalHours, 24); % Remaining hours after full days
% Total parking duration
fprintf('Total Days: %d\n', numDays);
fprintf('Remaining Hours: %d\n', numHours);
3 件のコメント
Sam Chak
2025 年 2 月 21 日
Basically, this is a relatively simple matrix problem. To adhere to the assignment requirements and continue from your current progress, after applying the rules, you should assign or store the user data in a parking duration column vector. This will allow the app to perform the multiplication with the parking rates row vector to determine the total parking fee.
%% Parking duration
% W D H
D = [ 1, 2, 23];
%% Parking rates
R = [60; % Weekly rate
9; % Daily rate
1]; % Hourly rate
%% Total parking fee
F = D*R % Total amount
Sam Chak
2025 年 2 月 21 日
@Antonio, By the way, I would like to highlight one issue: does the special first-hour rate apply if a vehicle has been parked for a full day—specifically, 26 hours, which is equivalent to 1 full day plus 2 hours?
PROBLEM:
Most major airports have separate lots for long term and short term parking. The cost to park depends on the lot you select and how long you stay. Consider this rate structure from the Salt Lake International Airport during the summer of 2016.
Long term economy parking:
the first hour is $2 and each additional hour or fraction thereof is $1.
Daily max $9
weekly max $60
Short term parking:
the first 30 mins are $2 and each additional 20 min or fraction thereof is $1
Daily max $32
Task:
Create a program that asks the user the following: which lot are you using? how many weeks, hours, days, and minutes did you park? your program shoud then calculate the parking bill.
Daily maximum of $9 means $9 for each full day; weekly maximum of $60 means $60 for each full week, etc... Assume the user enters positive integers for minutes/days/weeks/hours and that the user will enter appropriate number of days/weeks/hours. Program should account for user entering minutes greater than 60 and if the user enters an incorrect lot type. Be sure to consider the situations when hours and/or minutes are entered as zero - test and account for these cases.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!