-
Notifications
You must be signed in to change notification settings - Fork 0
/
Order.java
141 lines (130 loc) · 4.95 KB
/
Order.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.util.*;
public class Order {
private int orderId;
private static int counter=101;
private Customer customer;
private ArrayList<BillItem> billItems = new ArrayList<>();
private double total;
public Order(){
this.orderId = counter++;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public int getOrderId() {
return orderId;
}
public ArrayList<BillItem> getBillItems() {
return billItems;
}
public double getTotal() {
return (int)total;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public void setBillItems(ArrayList<BillItem> billItems) {
this.billItems = billItems;
}
public void setTotal(double total) {
this.total = total;
}
public void addBillItem(BillItem billItem){
for (Stock stock : StockRepo.getStockList()) {
if(stock.getProduct().getName().equals(billItem.getProduct().getName()) && stock.getQuantity() >= billItem.getQuantity()){
billItems.add(billItem);
stock.setQuantity(stock.getQuantity()-billItem.getQuantity());
calculateBillTotal();
return;
}
}
System.out.println("Stock unavailable");
}
public void updateBillItem(int id, int quantity){
for (BillItem billItem : billItems) {
if(billItem.getProduct().getId() == id){
for (Stock stock : StockRepo.getStockList()) {
if(stock.getProduct().getName().equals(billItem.getProduct().getName()) && quantity <= billItem.getQuantity()){
int leftQUantity = billItem.getQuantity()-quantity;
stock.setQuantity(stock.getQuantity()+leftQUantity);
billItem.setQuantity(quantity);
calculateBillTotal();
System.out.println("Quantity Updated");
return;
}
else if(stock.getProduct().getName().equals(billItem.getProduct().getName()) && quantity > billItem.getQuantity()){
int extraQuantity = quantity-billItem.getQuantity();
if(stock.getQuantity() >= extraQuantity){
stock.setQuantity(stock.getQuantity()-extraQuantity);
billItem.setQuantity(quantity);
calculateBillTotal();
System.out.println("Quantity Updated");
return;
}
else{
System.out.println("Given Quantity is not available");
return;
}
}
}
}
}
System.out.println("Invalid Product ID");
}
public void deleteBIllItem(int id){
for (BillItem billItem : billItems) {
if(billItem.getProduct().getId() == id){
for (Stock stock : StockRepo.getStockList()) {
if(stock.getProduct().getName().equals(billItem.getProduct().getName())){
stock.setQuantity(billItem.getQuantity());
billItems.remove(billItem);
calculateBillTotal();
System.out.println("Product removed");
return;
}
}
}
}
System.out.println("Invalid Product Id");
}
public void couponDiscount(int coupounCode){
for (Coupon coupon : CouponRepo.getCopounList()) {
if(coupon.getCouponCode() == coupounCode){
float discount = coupon.getDiscount();
double sum = this.total;
double discountAmount = sum * (discount/100);
sum = sum - discountAmount;
this.total = sum;
return;
}
}
System.out.println("Invalid CouponCode");
}
public void calculateBillTotal(){
double sum = 0;
for (BillItem billItem : billItems) {
sum+=billItem.getAmount();
}
this.total = sum;
}
public void viewOrderDetails(){
System.out.println("Order ID: " +this.orderId);
for (BillItem billItem : billItems) {
System.out.println(billItem);
}
System.out.println("\t\t\t\t Total: "+this.total);
}
@Override
public String toString(){
String s = "";
s+="Order ID: "+getOrderId()+"\n";
for (BillItem billItem : billItems) {
s+=billItem+"\n";
}
s+="\t\t\t\t Total: "+getTotal();
return s;
}
}