001package myhw3.data;
002
003import myhw3.command.Command;
004
005/**
006 * Implementation of command to add or remove inventory.
007 * @see Data
008 */
009final class CmdAdd implements Command {
010        private boolean runOnce;
011        private InventorySet inventory;
012        private Record oldvalue;
013        private Video video;
014        private int change;
015        CmdAdd(InventorySet inventory, Video video, int change) {
016                this.inventory = inventory;
017                this.video = video;
018                this.change = change;
019        }
020        public boolean run() {
021                if (runOnce) {
022                        return false;
023                }
024                runOnce = true;
025                //System.out.println(inventory.get(video) + " " + video + " " + change);
026                try {
027                        oldvalue = inventory.addNumOwned(video, change);
028                        inventory.getHistory().add(this);
029                        //System.out.println("ok");
030                        return true;
031                } catch (IllegalArgumentException e) {
032                        //System.out.println("IAE");
033                        return false;
034                } catch (ClassCastException e) {
035                        //System.out.println("CCE");
036                        return false;
037                }
038        }
039        public void undo() {
040                inventory.replaceEntry(video,oldvalue);
041        }
042        public void redo() {
043                oldvalue = inventory.addNumOwned(video, change);
044        }
045}