duancuo1234 2019-06-20 02:14
浏览 99

库存应用程序不会显示结果

I'm supposed to make a php program for an inventory management application.

I have the main program, as well as a test program to make sure everything is working properly

I do have the code working for the most part

inventory.php

class Product
{
// ----------------------------------------- Properties -----------------------------------------
private $product_name = "no name";
private $product_code = 0;
private $product_price = 0;
private $product_quantity = 0;
private $product_needs = "no needs";
private $error_message = "??";

// ---------------------------------- Set Methods ----------------------------------------------
function set_product_name($value)
{
$error_message = TRUE;
(ctype_alpha($value) && strlen($value) <= 20) ? $this->product_name = $value : $this->error_message = FALSE;
return $this->error_message;
}
function set_product_code($value)
{
$error_message = TRUE;
(ctype_digit($value) && ($value > 0 && $value <= 6)) ? $this->product_code = $value : $this->error_message = FALSE;
return $this->error_message;
}
function set_product_price($value)
{
$error_message = TRUE;
(ctype_digit($value) && ($value > 0 && $value <= 6)) ? $this->product_price = $value : $this->error_message = FALSE;
return $this->error_message;
}
function set_product_quantity($value)
{
$error_message = TRUE;
(ctype_digit($value) && ($value > 0 && $value <= 6)) ? $this->product_quantity = $value : $this->error_message = FALSE;
return $this->error_message;
}
function set_product_needs($value)
{
$error_message = TRUE;
(ctype_alpha($value) && strlen($value) <= 40) ? $this->product_needs = $value : $this->error_message = FALSE;
return $this->error_message;
}
// ----------------------------------------- Get Methods ------------------------------------------------------------
function get_product_name()
{
return $this->product_name;
}
function get_product_code()
{
return $this->product_code;
}
function get_product_price()
{
return $this->product_price;
}
function get_product_quantity()
{
return $this->product_quantity;
}
function get_product_needs()
{
return $this->product_needs;
}
function get_properties()
{
return "$this->product_name,$this->product_code,$this->product_price,$this->product_quantity,$this->product_needs.";
}
}
?>

lab.php

<?php
Require_once("inventory.php");
$lab = new Product;
// ------------------------------Set Properties--------------------------
$product_error_message = $lab->set_product_name('Hinge');
print $product_error_message == TRUE ? 'Name update successful<br/>' : 'Name update not successful<br/>';

$product_error_message = $lab->set_product_code('45435');
print $product_error_message == TRUE ? 'Code update successful<br />' : 'Code update not successful<br />';

$product_error_message = $lab->set_product_price('7.50');
print $product_error_message == TRUE ? 'Price update successful<br />' : 'Product update not successful<br />';

$product_error_message = $lab->set_product_quantity('75');
print $product_error_message == TRUE ? 'Quantity update successful<br />' : 'Quantity update not successful<br />';

$product_error_message = $lab->set_product_needs('Wrap in plastic');
print $product_error_message == TRUE ? 'Needs update successful<br/>' : 'Needs update not successful<br/>';
// ------------------------------Get Properties--------------------------
print $lab->get_product_name() . "<br/>";
print $lab->get_product_code() . "<br />";
print $lab->get_product_price() . "<br />";
print $lab->get_product_quantity() . "<br />";
print $lab->get_product_needs() . "<br />";
$product_properties = $lab->get_properties();
list($product_name, $product_code, $product_price, $product_quantity, $product_needs) = explode(',', $product_properties);
print "Name: $product_name. Code: $product_code. Price: $product_price. Quantity: $product_quantity. Needs: $product_needs";
?>

But the only thing that actually prints out is the name

Name update successful
Code update not successful
Product update not successful
Quantity update not successful
Needs update not successful
Hinge
0
0
0
no needs
Name: Hinge. Code: 0. Price: 0. Quantity: 0. Needs: no needs.

What part do I need to change to get everything to print?

  • 写回答

2条回答 默认 最新

  • doujie7497 2019-06-20 03:04
    关注

    Your Product class Product should look like this

    class Product {
    
        private $product_name = "no name";
        private $product_code = 0;
        private $product_price = 0;
        private $product_quantity = 0;
        private $product_needs = "no needs";
    
    
        function get_product_name() {
            return $this->product_name;
        }
    
        function set_product_name($value) {
            $this->product_name = $value;
        }
    
        function get_product_code() {
            return $this->product_code;
        }
    
        function set_product_code($value) {
            $this->product_code = $value;
        }
    
        function get_product_price() {
            return $this->product_price;
        }
    
        function set_product_price($value) {
            $this->product_price = $value;
        }
    
        function get_product_quantity() {
            return $this->product_quantity;
        }
    
        function set_product_quantity($value) {
            $this->product_quantity = $value;
        }
    
        function get_product_needs() {
            return $this->product_needs;
        }
    
        function set_product_needs($value) {
            $this->product_needs = $value;
        }
    
        function get_properties() {
            return $this->product_name . ' ' . $this->product_code . ' ' . $this->product_price . ' ' . $this->product_quantity . ' ' . $this->product_needs;
        }
    }
    

    So you can create a instance and get the correct values

    //lab.php
    
    $product = new Product();
    $product->set_product_code(1234);
    
    print $product->get_product_code(); // print 1234
    

    All validations should be made outside Product Model

    $product_quantity = '7.5';
    
    if(is_numeric($product_quantity)) { // check if is numeric
    
        $product->set_product_quantity($product_quantity); // set the product quantity
    
        print "Quantity update successful<br />"; // show successful messages
    } else {
        print "Quantity update is not successful<br />";
    }
    
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决