Store
timor
java
3 years ago
2.8 kB
1
Indexable
Never
import java.util.ArrayList; import java.util.Collections; public class Store { private ArrayList<Tablet> tablets; private int countI; private int countA; private int countW; /** * default constructor */ public Store() { countA = 0; countI = 0; countW = 0; tablets = new ArrayList<Tablet>(); } /** * copy constructor */ public Store(Store other) { countA = other.countA; countI = other.countI; countW = other.countW; for (Tablet otherTablet : other.tablets) { tablets.add(new Tablet(otherTablet)); } } public int getCountA() { return countA; } public int getCountI() { return countI; } public int getCountW() { return countW; } /** * * @param tablet - the tablet that we want to search on the store * @return if the tablet is on the store we return true' if not false we use the * isSame method to know if the tablet exist in the store */ public boolean tabletExistInStore(Tablet tablet) { int i = 0; boolean isExist = false; while (i < tablets.size()) { if (tablet.isSame(tablets.get(i)) == true) { isExist = true; } i++; } return isExist; } /** * * @param tablet the tablet that we want to add to store * first if the store have exactly tablet that we want to add then we set the highest * price we use for this the price setter , * then we check the tablet system and add to count of the system * after that we add the tablet. */ public void addTablet(Tablet tablet) { int i = 0; if (tabletExistInStore(tablet) == true) { while (i < tablets.size()) { if (tablet.isSame(tablets.get(i)) == true) { if (tablet.getPrice() > tablets.get(i).getPrice()) { tablets.get(i).setPrice(tablet.getPrice()); } else if (tablet.getPrice() < tablets.get(i).getPrice()) { tablet.setPrice(tablets.get(i).getPrice()); } } i++; } } else { if (tablet.getSystem() == 'I') { countI++; } else if (tablet.getSystem() == 'A') { countA++; } else { countW++; } tablets.add(tablet); } } public void sortStore() { int n = tablets.size(); for (int i = n - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (tablets.get(j).getSystem() == 'I' && tablets.get(j + 1).getSystem() == 'A') { Collections.swap(tablets, j, j + 1); } else if (tablets.get(j).getSystem() == 'A' && tablets.get(j + 1).getSystem() == 'W') { Collections.swap(tablets, j, j + 1); } else if (tablets.get(j).getSystem() == 'I' && tablets.get(j + 1).getSystem() == 'W') { Collections.swap(tablets, j, j + 1); } } } for (int p = 0; p < n; p++) { tablets.get(p).printTablet(); } } }