bai_3

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.8 kB
2
Indexable
Never
import java.util.ArrayList;
import java.util.List;

public class Solution {
    class FS {
        private List<Disk> diskList;

        FS() {
            diskList = new ArrayList<>();
        };

        void add(Disk disk) {
            diskList.add(disk);
        }
    }

    // Component class
    abstract class Entry {
        private String name;

        public Entry(String name) {
            this.name = name;
        }

        abstract double getUsedSpace();
    }

    class Disk extends Entry {
        List<Entry> child;
        double totalSpace;

        public Disk(String name, double totalSpace) {
            super(name);
            this.totalSpace = totalSpace;
        };

        double getFreeSpace() {
            return totalSpace - getUsedSpace();
        }

        @Override
        double getUsedSpace() {
            double space = 0;

            for (Entry e : child) {
                space += e.getUsedSpace();
            }

            return space;
        }

        public void add(Entry e) {
            if (e instanceof Disk) {
                return;
            }

            child.add(e);
        }

        public void remove(Entry e) {
            child.remove(e);
        }
    }

    // Composite class
    class Folder extends Entry {
        List<Entry> child;

        Folder(String name) {
            super(name);
        }

        @Override
        double getUsedSpace() {
            double space = 0;

            for (Entry e : child) {
                space += e.getUsedSpace();
            }

            return space;
        }

        public void add(Entry e) {
            if (e instanceof Disk) {
                return;
            }

            child.add(e);
        }

        public void remove(Entry e) {
            child.remove(e);
        }
    }

    // Leaf class
    class File extends Entry {
        String type;
        double space;

        File(String name, String type, double space) {
            super(name);
            this.type = type;
            this.space = space;
        }

        @Override
        double getUsedSpace() {
            return space;
        }
    }

    // Leaf class
    class Shortcut extends Entry {
        Entry pointer;

        public Shortcut(String name) {
            super(name);
            pointer = null;
        }

        public void setPointer(Entry pointer) {
            if (pointer instanceof Disk || pointer instanceof Shortcut) {
                return;
            }

            this.pointer = pointer;
        }

        @Override
        double getUsedSpace() {
            return 1;
        }
    }
}