Advertisement
spiny94

Untitled

Sep 8th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. package grocerystore;
  2.  
  3. import java.util.Comparator;
  4.  
  5. public class CompareName implements Comparator<Product>{
  6.     public int compare(Product a,Product b){
  7.         return a.getName().compareTo(b.getName());
  8.     }
  9. }package grocerystore;
  10.  
  11. public interface Counter {
  12.    
  13.     // following attributes are public, static, and final, implicitly
  14.    
  15.     double VAT = 0.2;
  16.    
  17.    
  18.     // following methods are public and abstract, implicitly
  19.    
  20.     void read(String code, int pieces) throws NoSuchProduct;
  21.     void print();
  22.     void close();
  23.     double total();
  24.     double gross();
  25.     double taxes();
  26. }
  27. package grocerystore;
  28.  
  29. import java.util.HashMap;
  30. import java.util.Iterator;
  31. import java.util.Map.Entry;
  32. import java.util.Set;
  33. import java.util.TreeMap;
  34. import java.util.Map;
  35.  
  36. public class GroceryCounter implements Counter {
  37.     Map<String,Product> inventory = new HashMap<String,Product>();
  38.     Map<Product,Integer> shopping = new TreeMap<Product,Integer>(new CompareName());
  39.     double total = 0.0;
  40.     Product discProd;
  41.     double discPrice;
  42.  
  43.     public void addProduct(String code, String desc, double price) {
  44.         Product p = new Product(code, desc, price);
  45.         inventory.put(code, p);
  46.     }
  47.  
  48.     public void promo(String code, int discount) {
  49.         Product p = inventory.get(code);
  50.         if(p==null)
  51.             discProd = null;
  52.         else {
  53.             discProd = p;
  54.             discPrice = p.getPrice()*(1-discount/100.);
  55.         }
  56.     }
  57.  
  58.     public void read(String code, int pieces) throws NoSuchProduct{
  59.         Product p = inventory.get(code);
  60.         if(p==null)
  61.             throw(new NoSuchProduct(code));
  62.         Integer previous = shopping.get(p);
  63.         previous = previous==null ? 0 : previous;
  64.         shopping.put(p, pieces+previous);
  65.         double price = p==discProd ? discPrice : p.getPrice();
  66.         total += pieces * price;
  67.     }
  68.    
  69.     public double total() {
  70.         return total;
  71.     }
  72.  
  73.     public double gross() {
  74.         return total + taxes();
  75.     }
  76.  
  77.     public double taxes() {
  78.         return total * VAT;
  79.     }
  80.  
  81.     public String invoice() {
  82.         Set<Map.Entry<Product,Integer>> s =  shopping.entrySet();
  83.         Iterator<Map.Entry<Product,Integer>> it = s.iterator();
  84.         String inv = "";
  85.         while(it.hasNext()){
  86.             Map.Entry<Product,Integer> entry = it.next();
  87.             Product p = entry.getKey();
  88.             int n = entry.getValue();
  89.             double price = p==discProd ? discPrice : p.getPrice();
  90.             inv += n+" x "+p.getName()+" \t> "+n*price+"\n";
  91.  
  92.         }
  93.         inv += "\nTotal: \t> "+total+"\n";
  94.         inv += "VAT: \t> "+taxes()+"\n";
  95.         inv += "Gross: \t> "+gross()+"\n";
  96.         return inv;
  97.     }
  98.    
  99.     public void print() {
  100.  
  101.         System.out.println(invoice());
  102.     }
  103.    
  104.     public void close() {
  105.         shopping.clear();
  106.         total=0.0;
  107.     }
  108. }
  109. package grocerystore;
  110.  
  111. public class NoSuchProduct extends Exception {
  112.     String code;
  113.     public NoSuchProduct(String c){
  114.         code=c;
  115.     }
  116.     public String getMessage(){
  117.         return "Unexisting code "+code;
  118.     }
  119.  
  120. }package grocerystore;
  121.  
  122. public class Product {
  123.     String code;
  124.     String name;
  125.     double price;
  126.    
  127.     public Product(String c, String n, double p){
  128.         code=c;
  129.         name=n;
  130.         price=p;
  131.     }
  132.    
  133.     public String getName(){
  134.         return name;
  135.     }
  136.    
  137.     public double getPrice(){
  138.         return price;
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement