Joelkuehle

Cache Class

Jun 18th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. package de.rechner.Taschenrechner;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. public class Cache {
  6.  
  7.     private LinkedList<Float> cacheList;
  8.     private int anzahlEintrage;
  9.    
  10.     public Cache() {
  11.         cacheList = new LinkedList<>();
  12.         anzahlEintrage = 0;
  13.     }
  14.  
  15.     public int getAnzahlEintrage() {
  16.         return anzahlEintrage;
  17.     }
  18.  
  19.     public LinkedList<Float> getCacheList() {
  20.         return cacheList;
  21.     }
  22.    
  23.     public void addEintrag (float ergebnis) {
  24.         if (anzahlEintrage < 10) {
  25.             try {
  26.                 cacheList.add(0, Float.valueOf(ergebnis));
  27.                 anzahlEintrage = cacheList.size();
  28.                 Rechner.sayln("Ergebnis wurde erfolgreich im Cache gespeichert.");
  29.             } catch (IndexOutOfBoundsException e) {
  30.                 Rechner.sayln("Die gewaehlte Stelle ist nicht verfuegbar. Bitte andere Stelle waehlen.");
  31.                 e.printStackTrace();
  32.             }
  33.         }
  34.         else {
  35.             try {
  36.             cacheList.remove(anzahlEintrage);
  37.             cacheList.add(Float.valueOf(ergebnis));
  38.             } catch (IndexOutOfBoundsException e) {
  39.                 Rechner.sayln("Die gewaehlte Stelle ist nicht verfuegbar. Bitte andere Stelle waehlen.");
  40.                 e.printStackTrace();
  41.             }
  42.         }
  43.     }
  44.    
  45.     public float getCacheErgebnis(int stelle) {
  46.         if (stelle < 10 && anzahlEintrage > stelle) {
  47.             try {
  48.                 float current = cacheList.get(stelle).floatValue();
  49.                 return current;
  50.             } catch (IndexOutOfBoundsException e) {
  51.                 Rechner.sayln("Die gewaehlte Stelle ist nicht verfuegbar. Bitte andere Stelle waehlen. 0 wird zurueck gegeben.");
  52.                 e.printStackTrace();
  53.                 return 0;
  54.             }
  55.         }
  56.         else {
  57.             Rechner.sayln("Die gewuenschte Stelle existiert nicht, denn sie liegt ueber 10. 0 wird zurueckgegeben.");
  58.             return 0;
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment