blob: 85cc5798cc86d9900a0765f67f72186fb66e4178 (
plain)
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
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "subscription.h"
#include "user.h"
Subscription::Subscription(User *user) : m_user(user)
{
Q_ASSERT(user);
}
//! [calculate-price]
void Subscription::calculatePrice()
{
const auto oldPrice = m_price;
m_price = qRound(calculateDiscount() * int(m_duration) * basePrice());
if (m_price != oldPrice)
emit priceChanged();
}
//! [calculate-price]
//! [set-duration]
void Subscription::setDuration(Duration newDuration)
{
if (newDuration != m_duration) {
m_duration = newDuration;
calculatePrice();
emit durationChanged();
}
}
//! [set-duration]
//! [calculate-discount]
double Subscription::calculateDiscount() const
{
switch (m_duration) {
case Monthly:
return 1;
case Quarterly:
return 0.9;
case Yearly:
return 0.6;
}
Q_ASSERT(false);
return -1;
}
//! [calculate-discount]
//! [calculate-base-price]
int Subscription::basePrice() const
{
if (m_user->country() == User::Country::AnyTerritory)
return 0;
return (m_user->country() == User::Country::Norway) ? 100 : 80;
}
//! [calculate-base-price]
//! [update-validity]
void Subscription::updateValidity()
{
bool isValid = m_isValid;
m_isValid = m_user->country() != User::Country::AnyTerritory && m_user->age() > 12;
if (m_isValid != isValid)
emit isValidChanged();
}
//! [update-validity]
|