Selasa, 16 Juni 2026

Rust 1

 // Rust version that feels like Pascal

use std::io;


// 1. Pascal "record" = Rust struct

struct Product {

    id: u32,

    name: String,

    price: u64, // price in cents, like Pascal 'integer'

}


struct Order {

    id: u32,

    product: Product,

    quantity: u32,

    total: u64,

    paid: bool,

}


// 2. Pascal "function" = Rust fn

fn calculate_total(product: &Product, quantity: u32) -> u64 {

    product.price * quantity as u64

}


// 3. Mock payment function, like Pascal procedure with result

fn process_payment(amount: u64, card_number: &str) -> bool {

    println!("Processing payment of Rp{}.00...", amount / 100);

    

    // Simple logic like Pascal if-then-else

    if card_number.len() < 16 {

        println!("Payment failed: Invalid card number");

        false

    } else {

        println!("Payment successful!");

        true

    }

}


// 4. Main program, like Pascal "begin ... end."

fn main() {

    // Declare variables like Pascal var section

    let product = Product {

        id: 1,

        name: String::from("Glass Mug"),

        price: 149900, // Rp1,499.00

    };

    

    println!("Product: {} - Rp{}.00", product.name, product.price / 100);

    

    // Read input like Pascal readln

    println!("Enter quantity:");

    let mut input = String::new();

    io::stdin().read_line(&mut input).expect("Failed to read");

    let quantity: u32 = input.trim().parse().unwrap_or(1);

    

    let total = calculate_total(&product, quantity);

    

    let mut order = Order {

        id: 1001,

        product,

        quantity,

        total,

        paid: false,

    };

    

    println!("Total to pay: Rp{}.00", order.total / 100);

    

    println!("Enter card number:");

    let mut card = String::new();

    io::stdin().read_line(&mut card).expect("Failed to read");

    

    // Pascal-style if statement

    if process_payment(order.total, card.trim()) {

        order.paid = true;

        println!("Order {} completed!", order.id);

    } else {

        println!("Order {} failed.", order.id);

    }

    

    // Pascal writeln

    println!("Order status: {}", if order.paid { "PAID" } else { "UNPAID" });

}







Tidak ada komentar: