📄 grocerycounter.java
字号:
package grocerystore;
import java.util.*;
public class GroceryCounter implements Counter {
private Map products = new HashMap();
private List invoice = new LinkedList();
private double netTotal;
private String promoCode;
private int promoDiscount;
public void addProduct(String code, String desc, double price) {
Product p = new Product(code, desc, price);
products.put(code, p);
}
public void promo(String code, int discount) {
promoCode = code;
promoDiscount = discount;
}
public void read(String code, int pieces) throws NoSuchProduct{
Product p = (Product) products.get(code);
if (p == null)
throw new NoSuchProduct();
double price = pieces * p.getPrice();
if (isPromo(p))
price *= (100 - promoDiscount)/100.0;
invoice.add(new Item(p, pieces, price));
netTotal += price;
}
private boolean isPromo(Product p) {
return p.getCode().equals(promoCode);
}
public double total() {
return netTotal;
}
public double gross() {
return netTotal / (1 + VAT);
}
public double taxes() {
return netTotal / (1 + VAT) * VAT;
}
public void print() {
Collections.sort(invoice);
for(Iterator i=invoice.iterator(); i.hasNext(); ){
Item item = (Item)i.next();
System.out.print(item.getPieces()+ " x ");
System.out.print("\t");
System.out.print(item.getDesc());
System.out.print("\t");
System.out.println(item.getPrice());
}
}
public void close() {
invoice = new LinkedList();
netTotal = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -