Upload Assignment 11

This commit is contained in:
Siwat Sirichai 2020-11-10 11:56:36 +07:00
parent 1ff61e69ec
commit 27b840e0df
7 changed files with 184 additions and 0 deletions

View file

@ -0,0 +1,94 @@
public class Witch {
private static int totalWitches = 0;
private String name;
private String school;
private int magicPower;
final int MAX_LEVEL = 100;
final int MIN_LEVEL = 0;
// Constructors
public Witch() {
this.name = "no one";
this.school = "unknown school";
this.magicPower = 10;
Witch.totalWitches++;
}
public Witch(String name, String school, int magicPower) {
this.name = name;
this.school = school;
this.magicPower = magicPower;
Witch.totalWitches++;
}
public Witch(Witch w) {
this.magicPower = w.getMagicPower();
this.name = w.getName();
this.school = w.getSchool();
Witch.totalWitches++;
}
// Getters
public static int getTotalWitches() {
return Witch.totalWitches;
}
public String getName() {
return this.name;
}
public int getMagicPower() {
return this.magicPower;
}
public String getSchool() {
return this.school;
}
// Setters
public static void setTotalWitches(int totalWitches) {
Witch.totalWitches = totalWitches;
}
public void setName(String name) {
this.name = name;
}
public void setMagicPower(int magicPower) {
if (magicPower < MIN_LEVEL)
magicPower = MIN_LEVEL;
if (magicPower > MAX_LEVEL)
magicPower = MAX_LEVEL;
this.magicPower = magicPower;
}
public void setSchool(String school) {
this.school = school;
}
// Utilities
public String toString() {
return String.format("Witchname: %s, School: %s, Magic Power: %s", this.name, this.school, this.magicPower);
}
public int compareTo(Witch w) {
if (this.magicPower > w.magicPower)
return 1;
else if (this.magicPower == w.magicPower)
return 0;
else if (this.magicPower < w.magicPower)
return -1;
return 9999; // This statement should not be called
}
public void levelUp(int increase) {
if (increase < 0)
return;
if (this.magicPower + increase > MAX_LEVEL)
this.magicPower = MAX_LEVEL;
else
this.magicPower += increase;
}
}

View file

@ -0,0 +1,48 @@
public class WitchGame {
static final String LINE_BREAK = "====================";
public static void main(String[] args) {
//Object Creation and Initialization
Witch witch1 = new Witch();
Witch witch2 = new Witch("Akko", "Luna Nova", 25);
//toString() method test
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Printing 2 witches", LINE_BREAK);
System.out.format("%s%n%s%n", witch1.toString(), witch2.toString());
//Getters and Setters test
witch1.setName("Hermione Granger");
witch1.setSchool("Hogwarts");
witch1.setMagicPower(85);
//compareTo(Witch w) test
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Printing the comparison", LINE_BREAK);
System.out.format("%s%n", parseCompareTo(witch1,witch2));
//Copy Constructor test
Witch witch3 = new Witch(witch1);
witch3.levelUp(80);
System.out.format("%s%n", parseCompareTo(witch2,witch3));
//Print all witch
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Now, printing all witches.", LINE_BREAK);
System.out.format("%s%n%s%n%s%n",witch1.toString(),witch2.toString(),witch3.toString());
//Print Total witches
System.out.format("%s%n%s%n%s%n", LINE_BREAK, "Now, printing total number of witches", LINE_BREAK);
System.out.println(Witch.getTotalWitches());
}
static String parseCompareTo(Witch witch1,Witch witch2) {
return switch (witch1.compareTo(witch2)) {
case 1:
yield witch1.toString()+" has higher magic power.";
case 0:
yield "2 witch have equal magic power.";
case -1:
yield witch2.toString()+" has higher magic power.";
default:
yield "";
};
}
}