Verified Commit d521f919 authored by Jonas Sulzer's avatar Jonas Sulzer 馃帗
Browse files

馃摝 NEW: Ex 1



Signed-off-by: Jonas Sulzer's avatarJonas Sulzer <jonas@violoncello.ch>
parent d6726b41
......@@ -2,28 +2,65 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="089645f1-f143-408a-be57-e098b4773895" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.gitignore" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/08-tableur.iml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cs108/Cell.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/cs108/Cell.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cs108/Spreadsheet.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/cs108/Spreadsheet.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Interface" />
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="28NbeKv4EqmvhULIUnAW8lneCVa" />
<component name="ProjectLevelVcsManager">
<ConfirmationsSetting value="1" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.proportion" value="0.0" />
<property name="project.structure.side.proportion" value="0.0" />
</component>
<component name="RunManager">
<configuration name="Spreadsheet" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="cs108.Spreadsheet" />
<module name="08-tableur" />
<option name="VM_PARAMETERS" value="--module-path $PROJECT_DIR$/../../ppoo/javafx-sdk-18.0.1/lib/ --add-modules javafx.controls" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="cs108.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Spreadsheet" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
......
package cs108;
import java.util.HashSet;
import java.util.Set;
public abstract class AbstractSubject implements Subject {
private Set<Observer> observerList = new HashSet<>();
public void addObserver(Observer observer) {
observerList.add(observer);
}
public void removeObserver(Observer observer) {
observerList.remove(observer);
}
protected void notifyObservers() {
for (Observer observer: observerList){
observer.update(this);
}
}
}
......@@ -3,7 +3,7 @@ package cs108;
import java.util.List;
import java.util.function.IntBinaryOperator;
public final class Cell {
public final class Cell extends AbstractSubject implements Observer {
private final int column, row;
private String contentString;
......@@ -38,6 +38,7 @@ public final class Cell {
public void setValue(int newValue) {
value = newValue;
super.notifyObservers();
}
public String getFormulaString() {
......@@ -45,8 +46,27 @@ public final class Cell {
}
public void setFormula(String newContentString, List<Cell> newArguments, IntBinaryOperator newOperator) {
for (Cell cell: arguments) {
cell.removeObserver(this);
}
for (Cell cell: newArguments) {
cell.addObserver(this);
}
contentString = newContentString;
arguments = newArguments;
operator = newOperator;
update(this);
}
@Override
public void update(Subject subject) {
switch (arguments.size()) {
case 2:
setValue(operator.applyAsInt(arguments.get(0).getValue(), arguments.get(1).getValue()));
break;
default:
setValue(Integer.parseInt(contentString));
}
notifyObservers();
}
}
package cs108;
@FunctionalInterface
public interface Observer {
void update(Subject subject);
}
......@@ -64,7 +64,7 @@ public final class Spreadsheet extends Application {
cellButton.setOnAction(e -> selectedCellProperty.set(cell));
gridPane.add(cellButton, cell.getColumn() + 1, cell.getRow() + 1);
// TODO: add observer to the cell, and update button's text with new value
cell.addObserver(observer -> cellButton.setText(String.valueOf(cell.getValue())) );
}
// Add selected cell name and edit field
......
package cs108;
public interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
}
Supports Markdown
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