Commit e0ef8d99 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Restoring .gitinore files, committing enum chapter

parent 22c97800
/Makefile
*.iml
.idea
A1.log
*~
......
package de.hdm_stuttgart.mi.sd1.class_wrapper;
public class Driver {
public static void main(String[] args) {
......@@ -12,6 +10,10 @@ public class Driver {
System.out.println(phpIntro);
System.out.println(advancedJava);
System.out.println(Screwed2.getPrice(Day.SUNDAY, 2));
// Type violation error
//System.out.println(Screwed2.getPrice(2, Day.SUNDAY));
}
}
package de.hdm_stuttgart.mi.sd1.class_wrapper;
public class Screwed2 {
/**
* Charge double prices on weekends
* @param day Day of week
* @param amount
* @return the effective amount depending on day of week.
*/
static public int getPrice(final Day day, final int amount) {
if (Day.SATURDAY == day || Day.SUNDAY == day) {
return 2 * amount;
} else {
return amount;
}
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ package de.hdm_stuttgart.mi.sd1.class_wrapper_private;
public class Day {
private Day() {} // Disallow object creation outside this class
private Day() {} // Disallow object creation outside class
static public final Day
MONDAY = new Day(),
......
......@@ -9,5 +9,9 @@ public class Screwed {
//final Day PAST_SUNDAY = new Day();
//final Lecture phpIntro = new Lecture(PAST_SUNDAY, "PHP introduction");
//System.out.println(phpIntro.toString());
}
}
\ No newline at end of file
......@@ -6,13 +6,13 @@ package de.hdm_stuttgart.mi.sd1.use_enum;
public enum Day {
MONDAY("Monday"),
TUESDAY("Tuesday"),
MONDAY( "Monday"),
TUESDAY( "Tuesday"),
WEDNESDAY("Wednesday"),
THURSDAY("Thursday"),
FRIDAY("Friday"),
SATURDAY("Saturday"),
SUNDAY("Sunday");
THURSDAY( "Thursday"),
FRIDAY( "Friday"),
SATURDAY( "Saturday"),
SUNDAY( "Sunday");
final String name;
......
......@@ -9985,6 +9985,8 @@ System.out.println(Screwed2.
<figure xml:id="sd1_fig_statesByClassIdea">
<title>Enumeration by instances</title>
<para>Roadmap:</para>
<itemizedlist>
<listitem>
<para>Define a dedicated class representing the
......@@ -10002,10 +10004,11 @@ System.out.println(Screwed2.
</itemizedlist>
</figure>
<figure xml:id="sd1_fig_enumByClass">
<title>Printing lecture info</title>
<figure xml:id="sd1_fig_enumByClassInstances">
<title>Class instance per enumeration value</title>
<programlisting language="java">public class Day {
<programlisting language="java">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper/Day.java">Day</link> {
static public final Day
MONDAY = new Day(),
......@@ -10015,6 +10018,187 @@ System.out.println(Screwed2.
FRIDAY = new Day(),
SATURDAY = new Day(),
SUNDAY = new Day();
}</programlisting>
</figure>
<figure xml:id="sd1_fig_enumByClassNoSwitch">
<title><code language="java">switch</code> no longer works!</title>
<programlisting language="java">public static String <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper/Day.java">getDaysName</link>(final Day day) {
if (MONDAY == day) { // Switch no longer possible, sigh!
return "Monday";
} else if (TUESDAY == day) {
...
} else if (SUNDAY == day) {
return "Sunday";
} else {
return "Illegal day instance: " + day;
}
}</programlisting>
</figure>
<figure xml:id="sd1_fig_rewriteGetPrice">
<title> Re-writing <methodname>getPrice()</methodname></title>
<programlisting language="java">/**
* Charge double prices on weekends
* @param day Day of week
* @param amount
* @return the effective amount depending on day of week.
*/
static public int <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper/Screwed2.java">getPrice</link>(final Day day, final int amount) {
if (Day.SATURDAY == day || Day.SUNDAY == day) {
return 2 * amount;
} else {
return amount;
}
}</programlisting>
</figure>
<figure xml:id="sd1_fig_getPriceTypeViolationError">
<title>Argument mismatch error</title>
<programlisting language="java">// Class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper/Driver.java">Driver</link>
// o.K.
System.out.println(Screwed2.getPrice(Day.SUNDAY, 2));
// Argument mismatch causing type violation error
System.out.println(Screwed2.getPrice(2, Day.SUNDAY));</programlisting>
</figure>
<figure xml:id="sd1_fig_userCircumventProblem">
<title>Creating an undesired instance</title>
<informaltable border="0">
<tr>
<td valign="top"><programlisting language="java">Class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper/Screwed.java">Screwed</link>:
final Day PAST_SUNDAY = new Day();
final Lecture phpIntro = new Lecture(
PAST_SUNDAY, "PHP introduction");
System.out.println(phpIntro.toString());</programlisting></td>
<td valign="top"><screen>Lecture «PHP introduction» being
held each <emphasis role="red">Illegal day instance:
de.hdm_stuttgart.mi.sd1.
class_wrapper.Day@63961c42</emphasis></screen></td>
</tr>
</informaltable>
</figure>
</section>
<section xml:id="sd1_enum_sect_privateConstructor">
<title>Defining a private constructor</title>
<figure xml:id="sd1_fig_enumPrivateConstructor">
<title><code language="java">private</code> <classname>Day</classname>
constructor </title>
<programlisting language="none">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper_private/Day.java">Day</link> {
<emphasis role="red">// Disallow object creation outside class
private Day() {}</emphasis>
static public final Day
MONDAY = new Day(),
TUESDAY = new Day(),
...
SUNDAY = new Day();
}</programlisting>
</figure>
<figure xml:id="sd1_fig_preventUndesiredDayCreation">
<title>Preventing undesired <classname>Day</classname> instance
creation</title>
<informaltable border="0">
<tr>
<td valign="top"><programlisting language="java">Class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper_private/Screwed.java">Screwed</link>:
final Day PAST_SUNDAY = new Day();
final Lecture phpIntro = new Lecture(
PAST_SUNDAY, "PHP introduction");
System.out.println(phpIntro.toString());</programlisting></td>
<td valign="top"><para>Compile time error:</para><screen><emphasis
role="red">'Day()' has private access in
'de.hdm_stuttgart.mi.sd1.
class_wrapper_private.Day'</emphasis></screen></td>
</tr>
</informaltable>
</figure>
<figure xml:id="sd1_fig_privateDaysNameConstructor">
<title>Even better: Adding day name attribute</title>
<programlisting language="none">public class <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/class_wrapper_daysname/Day.java">Day</link> {
public final String <emphasis role="red">name</emphasis>;
private Day(final String name) {
<emphasis role="red">this.name</emphasis> = name;
}
static public final Day
MONDAY = new Day("Monday"),
...
SUNDAY = new Day("Sunday");
public String toString() { return <emphasis role="red">name</emphasis>; }
}</programlisting>
</figure>
</section>
<section xml:id="sd1_enum_sect_dayAsEnumType">
<title><code language="java">enum</code> replacing <code
language="java">class</code></title>
<figure xml:id="sd1_fig_useEnumDay">
<title><code language="java">enum</code>
<classname>Day</classname></title>
<programlisting language="java">public enum <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/use_enum/Day.java">Day</link> {
MONDAY("Monday"),
TUESDAY("Tuesday"),
...
SUNDAY("Sunday");
final String name;
Day(final String name) { this.name = name;}
public String toString() { return name;}
}</programlisting>
</figure>
<figure xml:id="sd1_fig_enumSwitchWorking">
<title><code language="java">switch</code> statements working
again</title>
<programlisting language="java">public enum <link
xlink:href="https://gitlab.mi.hdm-stuttgart.de/goik/GoikLectures/tree/master/Doc/Sd1/Ref/ObjectsAndClasses/Enum/src/main/java/de/hdm_stuttgart/mi/sd1/use_enum/Day.java">Day</link> {
...
public static String getItalianDayname(final Day day) {
switch (day) {
case MONDAY: return "Lunedì";
case TUESDAY: return "Martedì";
...
case SUNDAY: return "Domenica";
}
return null; // Actually unreachable, but static
// code analysis is limited
}
}</programlisting>
</figure>
</section>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment