Skip to content

Commit

Permalink
Merge pull request #122 from kymckay/standard
Browse files Browse the repository at this point in the history
Standardise source formatting
  • Loading branch information
kymckay authored Apr 1, 2021
2 parents 77fa88e + 6095567 commit b2cb3fe
Show file tree
Hide file tree
Showing 19 changed files with 678 additions and 684 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ trim_trailing_whitespace = true
end_of_line = lf

[*.java]
indent_style = tab
indent_style = space
indent_size = 4
3 changes: 0 additions & 3 deletions data/test/menu.csv

This file was deleted.

3 changes: 0 additions & 3 deletions data/test/orders.csv

This file was deleted.

62 changes: 31 additions & 31 deletions src/main/java/sim/controllers/SpeedController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@
import sim.views.ServerGUI;

public class SpeedController {
private Server model;
private ServerGUI view;

public SpeedController(Server model, ServerGUI view) {
this.model = model;
this.view = view;

// register SliderListener with the gui
view.addSetListener(new SliderListener());
}

// inner SliderListener class responds when user selects a new simulation speed
public class SliderListener implements ChangeListener {

@Override
public void stateChanged(ChangeEvent e) {
// gets speed selected on the slider
int speed = view.getSpeed();

if (speed < 0) {
// speed has been decreased
model.setSpeed(BASE_SPEED * (speed * -1));
} else if (speed > 0) {
// speed has been increased
model.setSpeed(BASE_SPEED / (speed));
} else {
// speed set to normal
model.setSpeed(BASE_SPEED);
}
}
}
private Server model;
private ServerGUI view;

public SpeedController(Server model, ServerGUI view) {
this.model = model;
this.view = view;

// register SliderListener with the gui
view.addSetListener(new SliderListener());
}

// inner SliderListener class responds when user selects a new simulation speed
public class SliderListener implements ChangeListener {

@Override
public void stateChanged(ChangeEvent e) {
// gets speed selected on the slider
int speed = view.getSpeed();

if (speed < 0) {
// speed has been decreased
model.setSpeed(BASE_SPEED * (speed * -1));
} else if (speed > 0) {
// speed has been increased
model.setSpeed(BASE_SPEED / (speed));
} else {
// speed set to normal
model.setSpeed(BASE_SPEED);
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/sim/interfaces/Observer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

//Reference: PatternsExampleCode
public interface Observer {
public void update();
public void update();
}
6 changes: 3 additions & 3 deletions src/main/java/sim/interfaces/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Reference: PatternsExampleCode
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
28 changes: 14 additions & 14 deletions src/main/java/sim/model/CoffeeShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CoffeeShop implements Subject, Observer {
private Menu menu;

public CoffeeShop(Menu menu, int numStaff) {
Logger.getInstance().add("Simulation initiated");
Logger.getInstance().add("Simulation initiated");
this.menu = menu;

// Producer inserts input file of orders into shared queue for staff
Expand Down Expand Up @@ -75,26 +75,26 @@ public Kitchen getKitchen() {

@Override
public void registerObserver(Observer o) {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.add(o);
}
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.add(o);
}
}

@Override
public void removeObserver(Observer o) {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.remove(o);
}
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.remove(o);
}
}

@Override
public void notifyObservers() {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
for (Observer o : observers) o.update();
}
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
for (Observer o : observers) o.update();
}
}

@Override
Expand All @@ -107,7 +107,7 @@ public void update() {
// When kitchen is done coffee shop can close
// Output log, report and notify observers
if (kitchen.isDone()) {
Logger.getInstance().add("Simulation completed");
Logger.getInstance().add("Simulation completed");
Logger.getInstance().writeReport("log.txt");
new ReportWriter(kitchen.getServedCustomers(), menu).write(new File("report.txt"));
notifyObservers();
Expand Down
60 changes: 30 additions & 30 deletions src/main/java/sim/model/Kitchen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sim.interfaces.Subject;

public class Kitchen implements Runnable, Subject {
private SharedQueue kitchenQueue;
private SharedQueue kitchenQueue;

// Kitchen is not always serving a customer
private Optional<Customer> currentCustomer;
Expand All @@ -22,15 +22,15 @@ public class Kitchen implements Runnable, Subject {
// Set once server is finished serving
private boolean done;

public Kitchen(SharedQueue kitchenQueue) {
this.kitchenQueue = kitchenQueue;
public Kitchen(SharedQueue kitchenQueue) {
this.kitchenQueue = kitchenQueue;

// No customer to start with
currentCustomer = Optional.empty();
}
}

@Override
public void run() {
@Override
public void run() {

// Service continues as long as customers are still due to arrive or customers
// are in the queue
Expand All @@ -41,7 +41,7 @@ public void run() {
Customer toServe = currentCustomer.get();

notifyObservers();
log.add("The kitchen starts preparing an order for %s", toServe);
log.add("The kitchen starts preparing an order for %s", toServe);

try {
// Time to process order depends on number of items
Expand All @@ -66,36 +66,36 @@ public void run() {
}

public Optional<Customer> getCurrentCustomer() {
return currentCustomer;
return currentCustomer;
}

public List<Customer> getServedCustomers() {
return completed;
}

@Override
public void registerObserver(Observer o) {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.add(o);
}
}

@Override
public void removeObserver(Observer o) {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.remove(o);
}
}

@Override
public void notifyObservers() {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
for (Observer o : observers) o.update();
}
}
public void registerObserver(Observer o) {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.add(o);
}
}

@Override
public void removeObserver(Observer o) {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
observers.remove(o);
}
}

@Override
public void notifyObservers() {
// Synchronized to avoid concurrent modifications while notifying
synchronized(observers) {
for (Observer o : observers) o.update();
}
}

public boolean isDone() {
return done;
Expand Down
92 changes: 46 additions & 46 deletions src/main/java/sim/model/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@
import java.time.format.DateTimeFormatter;

public class Logger {
// Don't want millisecond precious for a log file
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

private static Logger instance;
private StringBuilder sBuilder;

private Logger() {
sBuilder = new StringBuilder();
}

/**
* Logs a new string in the log (adds time and newline)
* @param string
*/
public synchronized void add(String string) {
sBuilder.append(String.format("%s %s%n",
LocalDateTime.now().format(formatter),
string
));
}

/**
* Logs a new simulation event in the log (adds time and newline)
* @param event event message where %s will be replaced with the customer name
* @param customer customer object the event relates to
*/
public synchronized void add(String event, Customer customer) {
add(String.format(event, customer.getName()));
}

// Lazy-loads the singleton instance of the class
// Synchronized whole method to avoid double-checked locking
// (shown in lectures, but discouraged in modern Java practice)
public static synchronized Logger getInstance() {
if (instance == null)
instance = new Logger();
return instance;
}

//writes log to file
public void writeReport(String filename) {
// Don't want millisecond precious for a log file
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

private static Logger instance;
private StringBuilder sBuilder;

private Logger() {
sBuilder = new StringBuilder();
}

/**
* Logs a new string in the log (adds time and newline)
* @param string
*/
public synchronized void add(String string) {
sBuilder.append(String.format("%s %s%n",
LocalDateTime.now().format(formatter),
string
));
}

/**
* Logs a new simulation event in the log (adds time and newline)
* @param event event message where %s will be replaced with the customer name
* @param customer customer object the event relates to
*/
public synchronized void add(String event, Customer customer) {
add(String.format(event, customer.getName()));
}

// Lazy-loads the singleton instance of the class
// Synchronized whole method to avoid double-checked locking
// (shown in lectures, but discouraged in modern Java practice)
public static synchronized Logger getInstance() {
if (instance == null)
instance = new Logger();
return instance;
}

//writes log to file
public void writeReport(String filename) {
try (FileWriter orderWriter = new FileWriter(filename)) {
orderWriter.write(sBuilder.toString());
} catch(IOException e) {
e.printStackTrace();
}
}
orderWriter.write(sBuilder.toString());
} catch(IOException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit b2cb3fe

Please sign in to comment.