Created
September 12, 2025 06:47
-
-
Save Klrfl/9b49d268b6830ff43ba9a635e645ecdc to your computer and use it in GitHub Desktop.
Tugas praktikum Algoritma & Pemrograman pertemuan 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
int main() { | |
std::cout << "=== PARKING MANAGEMENT SYSTEM ===" << std::endl; | |
std::cout << "Vehicle types: " << std::endl; | |
std::cout << "1. Motorcycle (Rp 2000/hour)" << std::endl; | |
std::cout << "2. Car (Rp 5000/hour)" << std::endl; | |
std::cout << "3. Truck (Rp 10000/hour)" << std::endl; | |
int vehicle_type_input; | |
std::cout << "choose vehicle type (1-3): "; | |
std::cin >> vehicle_type_input; | |
std::string vehicle_type; | |
float base_rate; // harus pake float kak karena perhitungan diskon | |
switch (vehicle_type_input) { | |
case 1: | |
vehicle_type = "motorcycle"; | |
base_rate = 2000; | |
break; | |
case 2: | |
vehicle_type = "car"; | |
base_rate = 5000; | |
break; | |
case 3: | |
vehicle_type = "truck"; | |
base_rate = 10000; | |
break; | |
default: | |
std::cout << "unknown vehicle type" << std::endl; | |
return 1; | |
break; | |
} | |
int hours_parked; | |
std::cout << "Parking duration (hours): " << std::endl; | |
std::cin >> hours_parked; | |
if (hours_parked > 24) { | |
std::cout << "You cannot park more than 24 hours." << std::endl; | |
return 1; | |
} | |
/* rules: | |
* 2 jam pertama, charge 1x base_rate | |
* 3 jam selanjutnya, charge 1.5x base_rate | |
* 2 jam selanjutnya, charge 2x base_rate | |
* | |
* kepikiran pakai for loop, tapi kan ga boleh | |
* subtotal += 2 * base_rate; ini juga ilegal | |
*/ | |
/** ngitung jam di setiap segmen | |
* segmen pertama (2 jam pertama): ikutin hours_parked, kalau lebih dari 2 turunkan jadi 2 aja | |
* segmen kedua (3 jam setelahnya): hours_parked - segmen pertama, kalau lebih dari 3 turunkan jadi 3 aja | |
* segmen ketiga (sisa?): hours_parked - (segmen pertama + segmen kedua) | |
*/ | |
int first_seg_hour = hours_parked; | |
if (hours_parked > 2) { | |
first_seg_hour = 2; | |
} | |
float first_subtotal = first_seg_hour * base_rate; | |
float subtotal = first_subtotal; | |
int second_seg_hour = hours_parked - first_seg_hour; | |
if (second_seg_hour > 3) { | |
second_seg_hour = 3; | |
} | |
float second_base_rate = 0; | |
float second_subtotal = 0; | |
if (hours_parked > 2) { | |
second_base_rate = 1.5 * base_rate; | |
second_subtotal = second_seg_hour *(second_base_rate); | |
subtotal = subtotal + second_subtotal; | |
} | |
int third_seg_hour = hours_parked - (first_seg_hour + second_seg_hour); | |
float third_base_rate = 0; | |
float third_subtotal = 0; | |
if (hours_parked > 3) { | |
third_base_rate = 2 * base_rate; | |
third_subtotal = third_seg_hour * third_base_rate; | |
subtotal = subtotal + third_subtotal; | |
} | |
// input member | |
char member_input; | |
std::cout << "are you a member? (Y/N): "; | |
std::cin >> member_input; | |
if (member_input != 'Y' && member_input != 'N') { | |
std::cout << "invalid answer" << std::endl; | |
return 1; | |
} | |
bool is_member = member_input == 'Y'; | |
float total = subtotal; // kalau bukan member, total sama dgn subtotal karena tidak diskon | |
float discount = 0; | |
float member_discount = 0; | |
if (is_member) { | |
discount = 0.2; // 20% | |
member_discount = discount * subtotal; | |
total = subtotal - member_discount; | |
} | |
std::cout << "\n=== PAYMENT DETAILS ===" << std::endl; | |
std::cout << "vehicle type: " << vehicle_type << std::endl; | |
std::cout << "Duration: " << hours_parked << std::endl; | |
std::cout << "Base rate: " << base_rate << std::endl; | |
std::cout << "Breakdown:" << std::endl; | |
std::cout << "- Hours 1-2: " << first_seg_hour << " x Rp " << base_rate << "= Rp " << first_subtotal << std::endl; | |
if(second_base_rate != 0) { | |
std::cout << "- Hours 3-5: " << second_seg_hour << " x Rp " << second_base_rate << " = Rp " << second_subtotal << std::endl; | |
} | |
if(third_base_rate != 0) { | |
std::cout << "- Hours 6-7: " << third_seg_hour << " = x Rp " << third_base_rate << " = Rp " << third_subtotal << std::endl; | |
} | |
std::cout << "Subtotal: " << subtotal << std::endl; | |
if(discount != 0) std::cout << "Member discount: " << member_discount << std::endl; | |
std::cout << "TOTAL: " << total << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment