Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.rechner.Taschenrechner;
- import java.util.LinkedList;
- public class Cache {
- private LinkedList<Float> cacheList;
- private int anzahlEintrage;
- public Cache() {
- cacheList = new LinkedList<>();
- anzahlEintrage = 0;
- }
- public int getAnzahlEintrage() {
- return anzahlEintrage;
- }
- public LinkedList<Float> getCacheList() {
- return cacheList;
- }
- public void addEintrag (float ergebnis) {
- if (anzahlEintrage < 10) {
- try {
- cacheList.add(0, Float.valueOf(ergebnis));
- anzahlEintrage = cacheList.size();
- Rechner.sayln("Ergebnis wurde erfolgreich im Cache gespeichert.");
- } catch (IndexOutOfBoundsException e) {
- Rechner.sayln("Die gewaehlte Stelle ist nicht verfuegbar. Bitte andere Stelle waehlen.");
- e.printStackTrace();
- }
- }
- else {
- try {
- cacheList.remove(anzahlEintrage);
- cacheList.add(Float.valueOf(ergebnis));
- } catch (IndexOutOfBoundsException e) {
- Rechner.sayln("Die gewaehlte Stelle ist nicht verfuegbar. Bitte andere Stelle waehlen.");
- e.printStackTrace();
- }
- }
- }
- public float getCacheErgebnis(int stelle) {
- if (stelle < 10 && anzahlEintrage > stelle) {
- try {
- float current = cacheList.get(stelle).floatValue();
- return current;
- } catch (IndexOutOfBoundsException e) {
- Rechner.sayln("Die gewaehlte Stelle ist nicht verfuegbar. Bitte andere Stelle waehlen. 0 wird zurueck gegeben.");
- e.printStackTrace();
- return 0;
- }
- }
- else {
- Rechner.sayln("Die gewuenschte Stelle existiert nicht, denn sie liegt ueber 10. 0 wird zurueckgegeben.");
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment