Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package grocerystore;
- import java.util.Comparator;
- public class CompareName implements Comparator<Product>{
- public int compare(Product a,Product b){
- return a.getName().compareTo(b.getName());
- }
- }package grocerystore;
- public interface Counter {
- // following attributes are public, static, and final, implicitly
- double VAT = 0.2;
- // following methods are public and abstract, implicitly
- void read(String code, int pieces) throws NoSuchProduct;
- void print();
- void close();
- double total();
- double gross();
- double taxes();
- }
- package grocerystore;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.Map;
- public class GroceryCounter implements Counter {
- Map<String,Product> inventory = new HashMap<String,Product>();
- Map<Product,Integer> shopping = new TreeMap<Product,Integer>(new CompareName());
- double total = 0.0;
- Product discProd;
- double discPrice;
- public void addProduct(String code, String desc, double price) {
- Product p = new Product(code, desc, price);
- inventory.put(code, p);
- }
- public void promo(String code, int discount) {
- Product p = inventory.get(code);
- if(p==null)
- discProd = null;
- else {
- discProd = p;
- discPrice = p.getPrice()*(1-discount/100.);
- }
- }
- public void read(String code, int pieces) throws NoSuchProduct{
- Product p = inventory.get(code);
- if(p==null)
- throw(new NoSuchProduct(code));
- Integer previous = shopping.get(p);
- previous = previous==null ? 0 : previous;
- shopping.put(p, pieces+previous);
- double price = p==discProd ? discPrice : p.getPrice();
- total += pieces * price;
- }
- public double total() {
- return total;
- }
- public double gross() {
- return total + taxes();
- }
- public double taxes() {
- return total * VAT;
- }
- public String invoice() {
- Set<Map.Entry<Product,Integer>> s = shopping.entrySet();
- Iterator<Map.Entry<Product,Integer>> it = s.iterator();
- String inv = "";
- while(it.hasNext()){
- Map.Entry<Product,Integer> entry = it.next();
- Product p = entry.getKey();
- int n = entry.getValue();
- double price = p==discProd ? discPrice : p.getPrice();
- inv += n+" x "+p.getName()+" \t> "+n*price+"\n";
- }
- inv += "\nTotal: \t> "+total+"\n";
- inv += "VAT: \t> "+taxes()+"\n";
- inv += "Gross: \t> "+gross()+"\n";
- return inv;
- }
- public void print() {
- System.out.println(invoice());
- }
- public void close() {
- shopping.clear();
- total=0.0;
- }
- }
- package grocerystore;
- public class NoSuchProduct extends Exception {
- String code;
- public NoSuchProduct(String c){
- code=c;
- }
- public String getMessage(){
- return "Unexisting code "+code;
- }
- }package grocerystore;
- public class Product {
- String code;
- String name;
- double price;
- public Product(String c, String n, double p){
- code=c;
- name=n;
- price=p;
- }
- public String getName(){
- return name;
- }
- public double getPrice(){
- return price;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement