Advertisement
Frostcreep

Untitled

Oct 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. package model;
  2.  
  3. import IO.XLBufferedReader;
  4. import expr.Environment;
  5. import util.XLException;
  6.  
  7. import java.io.FileNotFoundException;
  8. import java.util.Map;
  9. import java.util.Observable;
  10. import java.util.TreeMap;
  11. import java.util.regex.Pattern;
  12.  
  13. public class Sheet extends Observable implements Environment {
  14.  
  15.   private final ExpCellFactory expCF;
  16.   private final CommentCellFactory commentCF;
  17.   private final ErrCellFactory errCF;
  18.   private Map<String, Cell> cellMap;
  19.  
  20.   public Sheet() {
  21.     cellMap = new TreeMap<>();
  22.     expCF = new ExpCellFactory();
  23.     commentCF = new CommentCellFactory();
  24.     errCF = new ErrCellFactory();
  25.   }
  26.  
  27.   public void load(String filename) throws FileNotFoundException {
  28.     XLBufferedReader bufferedReader = new XLBufferedReader(filename);
  29.     cellMap = new TreeMap<>();
  30.     bufferedReader.load(this);
  31.     setChanged();
  32.     notifyObservers(cellMap);
  33.   }
  34.  
  35.   // När du ska skicka errors gör notyifyObserver("ErrorMeddelande")
  36.   public void put(String address, String value) {
  37.     if (!isValidAddress(address)) {
  38.       notifyObservers("Not a valid address!");
  39.     } else {
  40.       try {
  41.         if (value.isEmpty()) {
  42.           clear(address);
  43.         } else if (value.charAt(0) == '#') {
  44.           CommentCell cell = (CommentCell) commentCF.cell(value);
  45.           cellMap.put(address, cell);
  46.         } else {
  47.           try {
  48.             ExpCell cell = (ExpCell) expCF.cell(value);
  49.             cellMap.put(address, cell);
  50.           } catch (Exception e) {
  51.             throw new XLException(e.getMessage());
  52.           }
  53.         }
  54.       } catch (Exception es) {
  55.         setChanged();
  56.         notifyObservers(es.getMessage());
  57.       }
  58.       setChanged();
  59.       notifyObservers(cellMap);
  60.     }
  61.   }
  62.  
  63.   private boolean isValidAddress(String address) {
  64.     address = address.toLowerCase();
  65.     return Pattern.matches("[a-h][1][0]|[a-h][1-9]", address);
  66.   }
  67.  
  68.   // Om det är en CommentCell returnera cellens värde utan #
  69.   // Om det är en ExprCell returnera cellens slutgiltiga värde t.ex. 1+3 = 6
  70.   // Vad händer om tom sträng
  71.   public String getValue(String address) {
  72.     Cell cell = cellMap.getOrDefault(address, null);
  73.     if (cell instanceof CommentCell) {
  74.       return cell.toString().substring(1);
  75.     } else {
  76.       try {
  77.         ErrCell tempErr = (ErrCell) errCF.cell(null);
  78.         cellMap.put(address, tempErr);
  79.         Environment env = cellAddress -> cellMap.get(cellAddress).value(this);
  80.  
  81.         Double value = cell.value(env);
  82.         cellMap.put(address, cell);
  83.         return value.toString();
  84.       } catch (Exception e) {
  85.         clear(address);
  86.         setChanged();
  87.         notifyObservers(e.getMessage());
  88.       }
  89.     }
  90.     return "";
  91.   }
  92.  
  93.   // Om det är en CommentCell returnera cellens kommentar med #
  94.   // Om det är en ExprCell returnera cellens uttryck t.ex. "1+3"
  95.   public String getExpression(String address) {
  96.     Cell cell = cellMap.getOrDefault(address, null);
  97.     if (cell != null) {
  98.       return cell.toString();
  99.     } else {
  100.       return "";
  101.     }
  102.   }
  103.  
  104.   public void clear(String address) {
  105.     cellMap.remove(address);
  106.     setChanged();
  107.     notifyObservers(cellMap);
  108.   }
  109.  
  110.   public void clearAll() {
  111.     cellMap = new TreeMap<>();
  112.     setChanged();
  113.     notifyObservers(cellMap);
  114.   }
  115.  
  116.   @Override
  117.   public double value(String name) {
  118.     var value = cellMap.getOrDefault(name, null).value(this);
  119.     if (value != null) {
  120.       return value;
  121.     } else {
  122.       notifyObservers("No such value");
  123.       return 0.0;
  124.     }
  125.   }
  126.  
  127.   public Map getCellMap() {
  128.     return cellMap;
  129.   }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement