我在python oop中进行了实验室测试。
创建一个名为
ShoppingCart
的类。创建一个不带参数的构造函数并设置
total
属性为零,并初始化一个名为的空dict属性items
。创建一个需要
add_item
,item_name
和quantity
的方法price
item_name
个论点。此方法应添加添加项的成本 到当前的总价值。它还应该添加一个条目 items dict使得键是remove_item
,值是 物品的数量。创建一个需要类似参数的方法
add_item
checkout
。它应该删除已添加到的项目 购物车并不是必需的。这种方法应该扣除成本 从当前总数中删除的项目,还更新项目 dict相应的。如果要删除的项目数量超过当前数量 在购物车中的该项目,假设该项目的所有条目都是 被删除。
创建一个方法
cash_paid
,它接收cash_paid
并返回 付款余额的价值。如果Shop
不够 支付总额,返还"现金支付不够"。创建一个名为
quantity
的类,它有一个不带的构造函数 参数并将名为ShoppingCart
的属性初始化为100.制作 确定Shop继承自class ShoppingCart(object): def __init__(self): self.total = 0 self.items = dict() def add_item(self, item_name, quantity, price): if item_name != None and quantity >= 1: self.items.update({item_name: quantity}) if quantity and price >= 1: self.total += (quantity * price) def remove_item(self, item_name, quantity, price): self.total -= (quantity * price) try: if quantity >= self.items[item_name]: self.items.pop(item_name, None) self.items[item_name] -= quantity except(KeyError, RuntimeError): pass def checkout(self, cash_paid): balance = 0 if cash_paid < self.total: return "Cash paid not enough" balance = cash_paid - self.total return balance class Shop(ShoppingCart): def __init__(self): self.quantity = 100 def remove_item(self): self.quantity -= 1
。在Shop类中,覆盖remove_item方法,以便调用 购买没有参数的remove_item将数量减一。
这是我的代码
import unittest
class ShoppingCartTestCases(unittest.TestCase):
def setUp(self):
self.cart = ShoppingCart()
self.shop = Shop()
def test_cart_property_initialization(self):
self.assertEqual(self.cart.total, 0, msg='Initial value of total not correct')
self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')
def test_add_item(self):
self.cart.add_item('Mango', 3, 10)
self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')
def test_remove_item(self):
self.cart.add_item('Mango', 3, 10)
self.cart.remove_item('Mango', 2, 10)
self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')
def test_checkout_returns_correct_balance(self):
self.cart.add_item('Mango', 3, 10)
self.cart.add_item('Orange', 16, 10)
self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')
def test_shop_is_instance_of_shopping_cart(self):
self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')
def test_shop_remove_item_method(self):
for i in range(15):
self.shop.remove_item()
self.assertEqual(self.shop.quantity, 85)
和单元测试
select
pb.clientid
from passbooking as pb
inner join (
( select pb.clientid, count(pb.ldate) as TotalCancels
from passbooking as pb
where pb.ldate >= 20170201
and pb.ldate <= 20170228
and (pb.schedulestatus = 430 or pb.schedulestatus = 420)
group by pb.clientid)
) as tcxl
on pb.clientid = tcxl.clientid
inner join (
(select pb.clientid, count(pb.ldate) as OnDemandCancels
from passbooking as pb
where pb.ldate >= 20170201
and pb.ldate <= 20170228
and (pb.schedulestatus = 430 or pb.schedulestatus = 420)
and pb.bookingpurpose <> 'P-DayCt')
group by pb.clientid)
)as odcxl
on pb.clientid = odcxl.clientid
inner join
(
(select pb.clientid, count(pb.ldate) as DayCenterCancels
from passbooking as pb
where pb.ldate >= 20170201
and pb.ldate <= 20170228
and (pb.schedulestatus = 430 or pb.schedulestatus = 420)
and pb.bookingpurpose = 'P-DayCt')
group by pb.clientid)
)as dccxl
on pb.clientid = dccxl.clientid
代码传递了所有测试用例。但是,当我尝试提交时,它失败并说它不符合所有测试规范。我已经仔细检查了它并捕获了可能出现的任何错误,但它仍然是相同的。我不知道自己错过了什么。有办法解决这个问题吗?
答案 0 :(得分:0)
在remove_item
方法中,您必须在执行任何示例之前检查if item_name in self.items:
:
if item_name in self.items:
if quantity < self.items[item_name] and quantity > 0:
self.items[item_name] -= quantity
self.total -= price*quantity
答案 1 :(得分:0)
class ShoppingCart(object):
def __init__(self):
self.total = 0
self.items = {}
def add_item(self, item_name, quantity, price):
self.total += (quantity * price)
self.items = {item_name : quantity}
def remove_item(self, item_name, quantity, price):
self.total -= (quantity * price)
if quantity > self.items[item_name]:
del self.items[item_name]
self.items[item_name] -= quantity
def checkout(self, cash_paid):
balance = 0
if cash_paid < self.total:
return "Cash paid not enough"
balance = cash_paid - self.total
return balance
class Shop(ShoppingCart):
def __init__(self):
ShoppingCart.__init__(self)
self.quantity = 100
def remove_item(self):
self.quantity -= 1
答案 2 :(得分:0)
class ShoppingCart {
constructor() {
this.total = 0;
this.items = {};
}
add_item(item_name, quantity, price) {
// this.items = { item_name:quantity };
this.items[item_name] = quantity;
this.total += (quantity * price);
}
remove_item(item_name, quantity, price) {
// confusion set it
var prevQuantity = this.items[item_name];
if (prevQuantity) {
if (prevQuantity >= quantity) {
var currentQuantity = prevQuantity - quantity;
this.items[item_name] = currentQuantity;
this.total -= (quantity * price);
} else {
delete this.items[item_name];
this.total -= (prevQuantity * price);
}
}
}
checkout(cash_paid) {
var balance = 0;
if (cash_paid < this.total) {
return "Cash paid not enough.";
} else {
balance = cash_paid - this.total;
return balance;
}
}
}
class Shop extends ShoppingCart {
constructor() {
super();
this.quantity = 100;
}
remove_item() {
this.quantity--;
}
}
let cart = new ShoppingCart();
cart.add_item("Mango", 3, 10);
cart.add_item("Orange", 16, 10);
cart.checkout(265)