-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStore.java
More file actions
118 lines (98 loc) · 3.69 KB
/
Store.java
File metadata and controls
118 lines (98 loc) · 3.69 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ragha
*/
public final class Store {
private final String name; private Products[] list; private static int counter=0;
public Store(String name, int size){
this.name=name;
this.list = new Products[size];
}
public boolean isExist(Products obj){
for (int i=0; i<list.length;i++)
if(list[i] == obj)
return true;
return false;
}
public void addProduct(Products obj){
if(counter<list.length){
list[counter]=obj;
counter++;
System.out.println("Successfully add ");
}
else
System.out.println("Sorry, cannot add "+obj.getName());
}
public void removeProduct(Products obj){
if(isExist(obj)){
for(int i=0;i<list.length;i++){
if(list[i]==obj){
list[i]=null;
if( i==(counter-1))//last index
counter--;
else //middle index or fist
{
for(int j=i;j<counter-1;j++)
list[j]=list[j+1];
list[counter-1]=null;
counter--;
}
}
}
}
else
System.out.println("Sorry, "+obj.getName()+" can not removed becuse it is not exist");
}
public double totalPrice(Products[] list2, Customer person){
double total=0;
for(Products obj : list2)
total+=obj.getPrice();
if(person.times().equals("Special customer")){
return (total-((total*25)/100));}
return total;
}
public static int getCounter(){return counter;}//we need in method remove test
public Products[] getList(){return list;}//we need in method remove test
public void buy(Products[] list2, Customer person){
boolean operationBuy=false;
for(Products obj: list2)
if(isExist(obj)==false){
operationBuy=true;
System.out.println("Sorry the product "+obj.getName()+" does not exist");}
if(operationBuy==false){
person.updateCustomer();
System.out.println(person);
System.out.println("bought : ");
for(Products obj: list2){
System.out.println(obj);
removeProduct(obj);
}
if(person.times().equals("Special customer"))
System.out.println("(After discount) with total price: "+totalPrice(list2,person)+" SR.");
else
System.out.println("with total price: "+totalPrice(list2,person)+" SR.");}
}
public String toString(){
String str="";
for(Products obj : list)
if(obj!=null){
if (obj instanceof SpecialProducts)
str+=((SpecialProducts)obj).toString()+"\n";
else
str+=obj.toString()+"\n";}
return name+" Store\nIt contains "+counter+" product(s)\n"+str;
}
public boolean Remove(int ID){
for(Products obj : list)
if (obj !=null){
if(obj.getID()==ID){
removeProduct(obj);
return true;}}
return false;
}
}