6. Anwendungsübung – Steuerberechnung mit Objekten (impots_03)
Wir greifen die zuvor behandelte Übung (Abschnitte 4.2 und 4.3) wieder auf, um sie mit PHP-Code zu lösen, der eine Klasse verwendet.
<?php
// definition of a Tax class
class Impôts {
// attributes: the 3 data tables
private $limites;
private $coeffR;
private $coeffN;
// manufacturer
public function __construct($IMPOTS) {
// $IMPOTS: name of the file containing data from tables $limites, $coeffR, $coeffN
// does the $IMPOTS file exist?
if (!file_exists($IMPOTS)) {
throw new Exception("Le fichier $IMPOTS n'existe pas");
}//if
// file block playback
$tables = file($IMPOTS);
if (!$tables) {
throw new Exception("Erreur lors de l'exploitation du fichier $IMPOTS");
}//if
// create the 3 tables - assume the rows are syntactically correct
$u = new Utilitaires(); // p// for utility functions
$this->limites = explode(":", $u->cutNewLineChar($tables[0]));
$this->coeffR = explode(":", $u->cutNewLineChar($tables[1]));
$this->coeffN = explode(":", $u->cutNewLineChar($tables[2]));
}
// --------------------------------------------------------------------------
public function calculer($marié, $enfants, $salaire) {
// $marié : yes, no
// $enfants : number of children
// $salaire: annual salary
// number of shares
$marié = strtolower($marié);
if ($marié == "oui")
$nbParts = $enfants / 2 + 2;
else
$nbParts=$enfants / 2 + 1;
// an additional 1/2 share if at least 3 children
if ($enfants >= 3)
$nbParts+=0.5;
// taxable income
$revenuImposable = 0.72 * $salaire;
// family quotient
$quotient = $revenuImposable / $nbParts;
// is set at the end of the limit table to stop the following loop
$this->limites[count($this->limites) - 1] = $quotient;
// tAX CALCULATION
$i = 0;
while ($quotient > $this->limites[$i])
$i++;
// because $quotient has been placed at the end of the $limites array, the previous loop
// cannot exceed the table $limites
// now we can calculate the tax
return floor($revenuImposable * $this->coeffR[$i] - $nbParts * $this->coeffN[$i]);
}
}
// a class of utility functions
class Utilitaires {
public function cutNewLinechar($ligne) {
// delete the end-of-line mark from $ligne if it exists
$L = strlen($ligne); // lo// line length
while (substr($ligne, $L - 1, 1) == "\n" or substr($ligne, $L - 1, 1) == "\r") {
$ligne = substr($ligne, 0, $L - 1);
$L--;
}//wh//while
// end
return($ligne);
}
}
// test -----------------------------------------------------
// definition of constants
$DATA = "data.txt";
$RESULTATS = "resultats.txt";
$IMPOTS = "impots.txt";
// the data required to calculate the tax has been placed in the IMPOTS file
// one line per table in the form
// "val1":"val2":"val3",...
// create a Tax object
try {
$I = new Impôts($IMPOTS);
} catch (Exception $e) {
print $e->getMessage();
exit;
}
// create a utilities object
$u = new Utilitaires();
// reading user data
$data = fopen($DATA, "r");
if (!$data) {
print "Impossible d'ouvrir en lecture le fichier des données [$DATA]\n";
exit;
}
// open results file
$résultats = fopen($RESULTATS, "w");
if (!$résultats) {
print "Impossible de créer le fichier des résultats [$RESULTATS]\n";
exit;
}
// use the current line of the data file
while ($ligne = fgets($data, 100)) {
// remove any end-of-line marker
$ligne = $u->cutNewLineChar($ligne);
// we retrieve the 3 fields married,children,salary which form $ligne
list($marié, $enfants, $salaire) = explode(",", $ligne);
// tax calculation
$impôt = $I->calculer($marié, $enfants, $salaire);
// enter the result
fputs($résultats, "$marié:$enfants:$salaire:$impôt\n");
// following data
}// while
// close files
fclose($data);
fclose($résultats);
// end
print "Terminé\n";
exit;
?>
Die Datei [taxes.txt]:
12620:13190:15640:24740:31810:39970:48360:55790:92970:127860:151250:172040:195000:0
0:0.05:0.1:0.15:0.2:0.25:0.3:0.35:0.4:0.45:0.5:0.55:0.6:0.65
0:631:1290.5:2072.5:3309.5:4900:6898.5:9316.5:12106:16754.5:23147.5:30710:39312:49062
Die Datei [data.txt]
Ergebnisse: die bereits in den Versionen mit Tabellen und Dateien erzielten Ergebnisse.
Die Datei [results.txt]
oui:2:200000:22504
non:2:200000:33388
oui:3:200000:16400
non:3:200000:22504
oui:5:50000:0
non:0:3000000:1354938
Kommentare
- Zeile 4: die Klasse „Taxes“. Sie kapselt Daten und Methoden zur Berechnung von Steuern:
- Daten: Zeilen 7–9 – dies sind die drei Daten-Arrays, die zur Berechnung der Steuer verwendet werden. Diese drei Arrays werden vom Konstruktor in Zeile 13 aus einer Textdatei initialisiert.
- Methoden: Zeile 32 – die Methode
*calculate* berechnet die Steuer. - Zeilen 17, 22: Wenn der Konstruktor das „Taxes“-Objekt nicht erstellen kann, löst er eine Ausnahme aus.
- Zeile 25: Die Klasse „Utilities“ wurde definiert, um die Funktion „cutNewLineChar“ zu kapseln.
- Zeilen 26–28: Initialisierung der drei privaten Felder der Klasse „Taxes“
- Zeile 32: Die Methode
*calculateder Klasse*Taxesberechnet die Steuer eines Steuerzahlers. - Zeilen 65–78: Die Klasse „Utilities“
- Zeilen 89–94: Erstellung eines „Taxes“-Objekts mit Ausnahmebehandlung. Bei dieser Erstellung wird die Textdatei $IMPOTS verwendet, um die drei privaten Felder des „Taxes“-Objekts zu füllen.
- Zeile 96: Erstellung eines „Utilities“-Objekts für den Zugriff auf die Funktion „cutNewLineChar“.