-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathInvoice.java
More file actions
83 lines (69 loc) · 2.09 KB
/
Invoice.java
File metadata and controls
83 lines (69 loc) · 2.09 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
import java.util.ArrayList;
import java.util.List;
public class Invoice {
private static final float TAX_RATE = 0.094f; // نرخ مالیات (9.4%)
private int state;
private final Customer customer; // مشتری
private final List<Item> items; // لیست آیتم های فاکتور
private float totalPrice; // قیمت کل
public Invoice(Customer customer) {
this.state = -1;
this.customer = customer;
this.items = new ArrayList<>();
this.totalPrice = 0.0f;
}
public int getState() {
return state;
}
public Customer getCustomer() {
return customer;
}
public boolean addItem(Item item) {
if (state == -1) { // فقط در حال ثبت سفارش
items.add(item);
recalculateTotalPrice();
return true;
}
else {
return false;
}
}
public boolean removeItem(Item item) {
if (state == -1) { // فقط در حال ثبت سفارش
for (Item existingItem : items) {
if (existingItem.getFood().equals(item.getFood())) {
items.remove(existingItem);
recalculateTotalPrice();
return true;
}
}
}
return false;
}
public void nextStage() {
if (state < 3)
state++;
}
public List<Item> getItems() {
return items; // لیست آیتم های فاکتور را برمی گرداند
}
public int getTotalPrice() {
return (int)Math.ceil(totalPrice);
}
private void recalculateTotalPrice() {
totalPrice = 0.0f;
for (Item item : items) {
totalPrice += item.getCount() * item.getFood().getPrice();
}
totalPrice += totalPrice * TAX_RATE;
}
@Override
public String toString() {
return "Invoice{" +
"state=" + state +
", customer=" + customer +
", items=" + items +
", totalPrice=" + totalPrice +
'}';
}
}