View sourcecode

The following files exists in this folder. Click to view.

bank.php

105 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
session_start
();

// Skapa startkonto med 1000 kr om inget finns
if (!isset($_SESSION['transactions'])) {
    
$_SESSION['transactions'] = [1000];
}

// Hämta transaktioner och räkna ut saldo
$transactions $_SESSION['transactions'];
$balance array_sum($transactions);

// Hantera formulär
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
$amount intval($_POST['amount']);
    
$action $_POST['action'];

    if (
$amount 0) {
        if (
$action === 'deposit') {
            
$_SESSION['transactions'][] = $amount;
        } elseif (
$action === 'withdraw') {
            if (
$balance $amount >= 0) {
                
$_SESSION['transactions'][] = -$amount;
            } else {
                
$error "Du kan inte ta ut mer pengar än du har på kontot!";
            }
        }
    } else {
        
$error "Beloppet måste vara större än 0.";
    }

    
$balance array_sum($_SESSION['transactions']);
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Sveriges enklaste bank</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-6">

            <div class="card shadow-sm">
                <div class="card-header bg-primary text-white">
                    <h3 class="text-center mb-0">Sveriges enklaste bank</h3>
                </div>

                <div class="card-body">
                    <p class="lead text-center">Saldo: 
                        <strong class="text-success"><?php echo $balance?> kr</strong>
                    </p>

                    <?php if (isset($error)): ?>
                        <div class="alert alert-danger" role="alert">
                            <?php echo $error?>
                        </div>
                    <?php endif; ?>

                    <form method="post" class="mb-3">
                        <div class="mb-3">
                            <label for="amount" class="form-label">Belopp (kr)</label>
                            <input type="number" name="amount" id="amount" class="form-control" required min="1">
                        </div>

                        <div class="d-grid gap-2">
                            <button type="submit" name="action" value="deposit" class="btn btn-success">Sätt in</button>
                            <button type="submit" name="action" value="withdraw" class="btn btn-warning">Ta ut</button>
                        </div>
                    </form>

                    <hr>
                    <h5>Transaktionshistorik</h5>
                    <ul class="list-group">
                        <?php foreach (array_reverse($transactions) as $t): ?>
                            <li class="list-group-item d-flex justify-content-between 
                                <?php echo ($t >= 'list-group-item-success' 'list-group-item-danger'); ?>">
                                <span><?php echo ($t >= 'Insättning' 'Uttag'); ?></span>
                                <strong><?php echo ($t >= '+' '') . $t?> kr</strong>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>

                <div class="card-footer text-center">
                    <form method="post" action="reset.php">
                        <button type="submit" class="btn btn-outline-danger btn-sm">Nollställ konto</button>
                    </form>
                </div>
            </div>

        </div>
    </div>
</div>


<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>