-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions.php
More file actions
109 lines (86 loc) · 2.67 KB
/
transactions.php
File metadata and controls
109 lines (86 loc) · 2.67 KB
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
106
107
108
109
<?php
$obj = new transaction();
class transaction {
public $form = '<br>
<FORM action="transactions.php?class=transactions" method="post">
<fieldset>
<LABEL for="amount">Amount: </LABEL>
<INPUT type="number" name="amount" id="amount"><BR>
<LABEL for="source">Source: </LABEL>
<INPUT type="text" name="source" id="source"><BR>
<INPUT type="radio" name="type" value="debit"> Debit<BR>
<INPUT type="radio" name="type" value="credit"> Credit<BR>
<INPUT type="checkbox" name="moretranstype" value="yes"> More Trans<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</fieldset>
</FORM>';
public function __construct(){
if($_SERVER['REQUEST_METHOD']=='GET'){
$this->get();
}else{
$this->post();
}
}
public function get(){
echo $this->form;
}
public function post(){
if($_POST['type']=='debit'){
echo 'You have spent $' . $_POST['amount'] . ' at ' . $_POST['source'] . ".<br><br>" . $this->form;
}else{
echo 'You have credited $' . $_POST['amount'] . ' to your account from ' . $_POST['source'] . '.<br><br>' . $this->form;
}
$obj = new calculation();
}
}
class calculation{
public $old_bal;
public $new_bal;
public function __construct($old_bal = null, $new_bal = null){
$this->set_old_balance();
if($_POST['type'] == 'credit'){
$this->credit();
}elseif($_POST['type'] == 'debit'){
$this->debit();
}
$this->message();
$old_bal = $this->old_bal;
$new_bal = $this->new_bal;
$handle = fopen('transactions1.csv', 'a+');
$data = array();
$data['amount'] = $_POST['amount'];
$data['source'] = $_POST['source'];
$data['type'] = $_POST['type'];
$data['balance'] = $new_bal;
fputcsv($handle, $data);
$transaction = fgetcsv($handle, 0, ',');
print_r($transaction);
fclose($handle);
print_r($data);
}
public function credit(){
$this->new_bal = $this->old_bal + $_POST['amount'];
}
public function debit(){
$this->new_bal = $this->old_bal - $_POST['amount'];
}
public function message(){
echo 'Your old balance was ' . $this->old_bal . '. Your new balance is ' . $this->new_bal . '.';
}
public function set_old_balance(){
if(($handle = fopen('transactions1.csv', 'a+'))!==FALSE){
while(($record = fgetcsv($handle, 0, ','))!==FALSE){
$data[] = $record;
}
fclose($handle);
}
if(isset($data)){
$array_length = count($data);
$this->old_bal = $data[$array_length - 1][3];
print_r($data);
}else{
$this->old_bal = 0;
}
}
}
?>