📄 counter.java
字号:
package domain;
import java.util.ArrayList;
public class Counter {
public static void main(String[] args) {
}
/**
*
*/
public Counter() {
//do nothing.
}
public Counter(User user) {
this.user = user;
}
public Counter(User user, ArrayList<Goods> goodses) {
this.user = user;
this.goodses = goodses;
for (int i = 0; i < this.goodses.size(); i++) {
goodCounts.add(new Integer(1));
}
}
public Counter(User user, ArrayList<Goods> goodses, ArrayList<Integer> goodCounts) {
this.user = user;
this.goodses = goodses;
if (goodses.size() == goodCounts.size()) {
this.goodCounts = goodCounts;
}
else {
for (int i = 0; i < this.goodses.size(); i++) {
goodCounts.add(new Integer(1));
}
}
}
public int size() {
return goodses.size();
}
public boolean contains(Goods goods) {
return goodses.contains(goods);
}
public boolean contains(Goods goods, int count) {
int index = goodses.indexOf(goods);
return (index > -1) && (goodCounts.get(index).intValue() == count);
}
public double values() {
double sum = 0;
for (int i = 0; i < goodses.size(); i++) {
sum += value(i);
}
return sum;
}
public double value(int index) {
Goods goods = goodses.get(index);
int count = goodCounts.get(index).intValue();
return (goods.getPrice() * count);
}
public int indexOf(Goods goods) {
return goodses.indexOf(goods);
}
public void setGoods(Goods goods, int count, int index) {
if (count >0 && index >= 0 && goods != null) {
goodses.set(index, goods);
goodCounts.set(index, new Integer(count));
}
}
public Goods getGoods(int index) {
return goodses.get(index);
}
public void setGoodsCount(int count, int index) {
setGoods(goodses.get(index), count, index);
}
public int getGoodsCount(int index) {
return goodCounts.get(index).intValue();
}
public void setGoods(Goods goods, int index) {
setGoods(goods, getGoodsCount(index), index);
}
public void setGoods(int count, int index) {
setGoods(getGoods(index), count, index);
}
public void addGood(Goods goods, int count) {
if (count > 0) {
if (goodses.contains(goods)) {
int index = goodses.indexOf(goods);
Integer oldCount = goodCounts.get(index);
goodCounts.remove(index);
goodCounts.add(index, new Integer(oldCount.intValue() + count));
}
else {
goodses.add(goods);
goodCounts.add(new Integer(count));
}
}
}
/**
* @remove the specific good from the user's counter.
* @return boolean if operate successfully return true; else return false.
*/
public boolean removeGood(Goods goods) {
if (goodses.contains(goods)) {
goodCounts.remove(goodses.indexOf(goods));
return goodses.remove(goods);
}
return false;
}
/**
* @Clear the user's counter.
*/
public void clear() {
goodses.clear();
goodCounts.clear();
}
/**
* @return Returns the Goods.
*/
public ArrayList<Goods> getGoods() {
return goodses;
}
/**
* @param bookIds The Goods to set.
*/
public void setGoods(ArrayList<Goods> goodses) {
this.goodses = goodses;
}
/**
* @return Returns the user.
*/
public User getUser() {
return user;
}
/**
* @param userId The user to set.
*/
public void setUser(User user) {
this.user = user;
}
/**
* @return Returns the goodCounts.
*/
public ArrayList<Integer> getGoodCounts() {
return goodCounts;
}
/**
* @param goodCounts The goodCounts to set.
*/
public void setGoodCounts(ArrayList<Integer> goodCounts) {
this.goodCounts = goodCounts;
}
User user = null; /** the counter's user */
ArrayList<Goods> goodses = new ArrayList<Goods>(); /** the counter's goods */
ArrayList<Integer> goodCounts = new ArrayList<Integer>(); /** the counter's count */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -