diff --git a/Day 40 (object oriented design)/Improve the design - not refactored/CoinInventory.java b/Day 40 (object oriented design)/Improve the design - not refactored/CoinInventory.java new file mode 100644 index 0000000..98e1739 --- /dev/null +++ b/Day 40 (object oriented design)/Improve the design - not refactored/CoinInventory.java @@ -0,0 +1,26 @@ + +public class CoinInventory { + private int[] coinCounts; //cp (pennies), sp (dimes), ep (half-dollars), gp (dollar bills), pp (five dollar bills) + private int weight; //The weight is the sum of all the coins. + + public CoinInventory() { + coinCounts = new int[5]; + } + + public void setCoins(int[] a) { + coinCounts = a; + } + + public int[] getCoins() { + return coinCounts; + } + + public void setWeight(int w) { + weight = w; + } + + public int getWeight() { + return weight; + } + +} diff --git a/Day 40 (object oriented design)/Improve the design - not refactored/Item.java b/Day 40 (object oriented design)/Improve the design - not refactored/Item.java new file mode 100644 index 0000000..0ff9e1e --- /dev/null +++ b/Day 40 (object oriented design)/Improve the design - not refactored/Item.java @@ -0,0 +1,18 @@ + +public class Item { + private String description; + private int weight; + + public Item(String description, int weight) { + this.description = description; + this.weight = weight; + } + + public int getWeight() { + return weight; + } + + public String getDescription() { + return description; + } +} diff --git a/Day 40 (object oriented design)/Improve the design - not refactored/Player.java b/Day 40 (object oriented design)/Improve the design - not refactored/Player.java new file mode 100644 index 0000000..0ea561c --- /dev/null +++ b/Day 40 (object oriented design)/Improve the design - not refactored/Player.java @@ -0,0 +1,77 @@ +//The design among these 3 classes needs improvement, and the code has a couple of bugs, too. +//Improve the design and fix the bugs. +//The public interface of Player should remain the same as it is now. + +//Hints: + //Avoid duplicate data + //Methods and instance data should have high cohesion + //Coupling should be loose between classes + +import java.util.*; + +public class Player { + private String name; + private CoinInventory coinInventory; + private ArrayList otherInventory; + private int encumbrance; + + public Player(String name) { + this.name = name; + coinInventory = new CoinInventory(); + otherInventory = new ArrayList(); + encumbrance = 0; + } + + public void addItem(Item item) { + otherInventory.add(item); + } + + public void collectMoney(int cp, int sp, int ep, int gp, int pp) { + int[] moneys = coinInventory.getCoins(); + moneys[0] += cp; + moneys[1] += sp; + moneys[2] += ep; + moneys[3] += gp; + moneys[4] += pp; + coinInventory.setCoins(moneys); + } + + public double getRunningSpeed() { + double speed = 5 * (1 - getEncumbrance() / 1000.0); //Speed 5 if unencumbered; speed 0 if 1000 units carried or more. + if (speed < 0) { + return 0; + } + else { + return speed; + } + } + + public double getMoneyValue() { + int[] moneys = coinInventory.getCoins(); + return .01 * moneys[0] + .1 * moneys[1] + .5 * moneys[2] + 1 * moneys[3] + 5 * moneys[4]; + } + + public int getEncumbrance() { + int[] moneys = coinInventory.getCoins(); + return moneys[0] + moneys[1] + moneys[2] + moneys[3] + moneys[4] + encumbrance; + } + + //Test method. + public static void main(String[] args) { + Player p = new Player("Whistlestick"); + Item sword = new Item("Magic sword +2", 20); + Item shield = new Item("Normal shield", 40); + p.addItem(sword); + p.addItem(shield); + if (p.getEncumbrance() != 60) throw new RuntimeException("getEncumbrance() should be 60, not " + p.getEncumbrance()); + p.collectMoney(10,2,0,40,2); + if (p.getEncumbrance() != 60+54) throw new RuntimeException("getEncumbrance() should be 114, not " + p.getEncumbrance()); + if (p.getMoneyValue() != 50.3) throw new RuntimeException("getMoneyValue() should be 50.3, not " + p.getMoneyValue()); + p.collectMoney(0,0,0,6,0); + if (p.getEncumbrance() != 120) throw new RuntimeException("getEncumbrance() should be 120, not " + p.getEncumbrance()); + if (p.getRunningSpeed() != 4.4) throw new RuntimeException("getRunningSpeed() should be 4.4, not " + p.getRunningSpeed()); + System.out.println("All tests passed."); + } + + +} \ No newline at end of file diff --git a/Day 40 (object oriented design)/Improve the design - refactored/CoinInventory.java b/Day 40 (object oriented design)/Improve the design - refactored/CoinInventory.java new file mode 100644 index 0000000..614e7f6 --- /dev/null +++ b/Day 40 (object oriented design)/Improve the design - refactored/CoinInventory.java @@ -0,0 +1,26 @@ + +public class CoinInventory { + private int[] coinCounts; //cp (pennies), sp (dimes), ep (half-dollars), gp (dollar bills), pp (five dollar bills) + + public CoinInventory() { + coinCounts = new int[5]; + } + + public void collectMoney(int cp, int sp, int ep, int gp, int pp) { + coinCounts[0] += cp; + coinCounts[1] += sp; + coinCounts[2] += ep; + coinCounts[3] += gp; + coinCounts[4] += pp; + } + + public double getMoneyValue() { + return .01 * coinCounts[0] + .1 * coinCounts[1] + .5 * coinCounts[2] + 1 * coinCounts[3] + 5 * coinCounts[4]; + } + + public int getWeight() { + return coinCounts[0] + coinCounts[1] + coinCounts[2] + coinCounts[3] + coinCounts[4]; + } + + +} diff --git a/Day 40 (object oriented design)/Improve the design - refactored/Item.java b/Day 40 (object oriented design)/Improve the design - refactored/Item.java new file mode 100644 index 0000000..0ff9e1e --- /dev/null +++ b/Day 40 (object oriented design)/Improve the design - refactored/Item.java @@ -0,0 +1,18 @@ + +public class Item { + private String description; + private int weight; + + public Item(String description, int weight) { + this.description = description; + this.weight = weight; + } + + public int getWeight() { + return weight; + } + + public String getDescription() { + return description; + } +} diff --git a/Day 40 (object oriented design)/Improve the design - refactored/Player.java b/Day 40 (object oriented design)/Improve the design - refactored/Player.java new file mode 100644 index 0000000..22d97be --- /dev/null +++ b/Day 40 (object oriented design)/Improve the design - refactored/Player.java @@ -0,0 +1,69 @@ +//The design among these 3 classes needs improvement, and the code has a couple of bugs, too. +//Improve the design and fix the bugs. +//The public interface of Player should remain the same as it is now. + +//Hints: + //Avoid duplicate data + //Methods and instance data should have high cohesion + //Coupling should be loose between classes + +import java.util.*; + +public class Player { + private String name; + private CoinInventory coinInventory; + private ArrayList otherInventory; + + public Player(String name) { + this.name = name; + coinInventory = new CoinInventory(); + otherInventory = new ArrayList(); + } + + public void addItem(Item item) { + otherInventory.add(item); + } + + public void collectMoney(int cp, int sp, int ep, int gp, int pp) { + coinInventory.collectMoney(cp, sp, ep, gp, pp); + } + + public double getRunningSpeed() { + double speed = 5 * (1 - getEncumbrance() / 1000.0); //Speed 5 if unencumbered; speed 0 if 1000 units carried or more. + if (speed < 0) { + return 0; + } + else { + return speed; + } + } + + public double getMoneyValue() { + return coinInventory.getMoneyValue(); + } + + public int getEncumbrance() { + int encumbrance = 0; + for(Item i : otherInventory) encumbrance += i.getWeight(); + return coinInventory.getWeight() + encumbrance; + } + + //Test method. + public static void main(String[] args) { + Player p = new Player("Whistlestick"); + Item sword = new Item("Magic sword +2", 20); + Item shield = new Item("Normal shield", 40); + p.addItem(sword); + p.addItem(shield); + if (p.getEncumbrance() != 60) throw new RuntimeException("getEncumbrance() should be 60, not " + p.getEncumbrance()); + p.collectMoney(10,2,0,40,2); + if (p.getEncumbrance() != 60+54) throw new RuntimeException("getEncumbrance() should be 114, not " + p.getEncumbrance()); + if (p.getMoneyValue() != 50.3) throw new RuntimeException("getMoneyValue() should be 50.3, not " + p.getMoneyValue()); + p.collectMoney(0,0,0,6,0); + if (p.getEncumbrance() != 120) throw new RuntimeException("getEncumbrance() should be 120, not " + p.getEncumbrance()); + if (p.getRunningSpeed() != 4.4) throw new RuntimeException("getRunningSpeed() should be 4.4, not " + p.getRunningSpeed()); + System.out.println("All tests passed."); + } + + +} \ No newline at end of file diff --git a/Day 40 (object oriented design)/notes.txt b/Day 40 (object oriented design)/notes.txt new file mode 100644 index 0000000..30ce94d --- /dev/null +++ b/Day 40 (object oriented design)/notes.txt @@ -0,0 +1,17 @@ +Designing good classes +---------------------- +* To guide design on the class level, ask: "How will this class be used?" + +* A class should represent one coherent abstraction or concept. + +* Look for code reuse opportunities (would this class be useful elsewhere?) + +* When working with a class externally, you should not have to understand the internal implementation. + +* Every constructor and public method should leave the instance variables in a consistent and correct state for the next public method call. + +* Generally avoid going over 7 (plus or minus 2) instance variables in a class. + +* Do not repeat data unless truly necessary. + +* Do not give variables more scope than they require.