uni

Thing1's amazing uni repo
Log | Files | Refs | Submodules

commit 7f9cce01e4016edfe525685bf28c0cda388d7bd5
parent 5fa0ec070de559ac1472edff28d99d24ba6a5aa3
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Wed, 11 Mar 2026 11:07:17 +0000

commited work on worksheet

Diffstat:
MCS12320/workshop/5/src/Monster/Monster.java | 48+++++++++++++++++++++++++++++++++++++++++++-----
MCS12320/workshop/5/src/Monster/Treasure.java | 5+++++
2 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/CS12320/workshop/5/src/Monster/Monster.java b/CS12320/workshop/5/src/Monster/Monster.java @@ -1,10 +1,11 @@ package Monster; -import java.awt.Dimension; -import java.lang.reflect.Array; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.PrintWriter; import java.util.ArrayList; -import java.util.List; - -import Monster.Treasure; +import java.util.Scanner; /** * A big scary monster class @@ -33,6 +34,43 @@ public class Monster { this.healthPoints = 20; } + public Monster load(String path) throws java.io.FileNotFoundException { + Scanner sc = new Scanner(new BufferedReader(new FileReader(path))); + sc.useDelimiter("\r?\n|\n"); + + String type = sc.nextLine(); + String hairColor = sc.nextLine(); + String hp = sc.nextLine(); + + Monster newMonster = new Monster(MonsterType.valueOf(type), hairColor, true); + newMonster.setHealthPoints(Integer.parseInt(hp)); + + while (sc.hasNextLine()) { + String treasureType = sc.nextLine(); + String treasureColor = sc.nextLine(); + String treasurePp= sc.nextLine(); + + newMonster.addTreasure(new Treasure( + TreasureType.valueOf(treasureType), + treasureColor, + Integer.parseInt(treasurePp) + ) + ); + } + + sc.close(); + return newMonster; + } + + public void save(String path) throws java.io.IOException { + PrintWriter pr = new PrintWriter(new BufferedWriter(new FileWriter(path))); + + pr.printf("%s\n%s\n%d\n", this.monsterType, this.hairColor, this.healthPoints); + this.treasures.stream().forEach(t -> t.saveTreasure(pr)); + + pr.close(); + } + /** * Setter for the monster type * @param monsterType The new type of monster diff --git a/CS12320/workshop/5/src/Monster/Treasure.java b/CS12320/workshop/5/src/Monster/Treasure.java @@ -1,5 +1,6 @@ package Monster; +import java.io.PrintWriter; /** * stores the kind of Treasure @@ -44,4 +45,8 @@ public class Treasure { public String toString() { return "A " + this.treasureColor + " " + this.treasureType; } + + public void saveTreasure(PrintWriter pr) { + pr.printf("%s\n%s\n%d\n", this.treasureType, this.treasureColor, this.specialPowerPoints); + } };