Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution avec SortedMap + ... #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added lib/fest-assert-1.4.jar
Binary file not shown.
Binary file added lib/fest-util-1.1.6.jar
Binary file not shown.
Binary file added lib/log4j-1.2.16.jar
Binary file not shown.
Binary file added lib/mail.jar
Binary file not shown.
Binary file added lib/testng-6.3.1.jar
Binary file not shown.
36 changes: 36 additions & 0 deletions src/ch/genevaJug/fooBarQix/FooBarQix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.genevaJug.fooBarQix;


import org.apache.log4j.Logger;

public class FooBarQix {

private final static Logger logger = Logger.getLogger(FooBarQix.class);

/**
* @param args
*/
public static void main(String[] args) {
logger.debug("START FooBarQix");
FooBarQix fooBarQix = new FooBarQix();
fooBarQix.display(100);
logger.debug("END FooBarQix");
}

public void display(int nbLine) {
Window window = new Window();

FooBarQixConverter converter = FooBarQixConverter.getInstance();
for (int number = 1; number <= nbLine; number++) {
try {
String value = converter.convert(number);
window.addValue(value);
} catch (Exception e) {
logger.error("Unable to convert value "+number, e);
// Error policy, continue and hope user will not see the error
// => we display the number. But we send by mail the error so know they are a error.
window.addValue(String.valueOf(number));
}
}
}
}
62 changes: 62 additions & 0 deletions src/ch/genevaJug/fooBarQix/FooBarQixConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ch.genevaJug.fooBarQix;

import java.util.Collections;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;


/**
*
* @author Daniel
*
*/
public class FooBarQixConverter {


// Unmodifiable SortedMap.
private final SortedMap<Integer, String> specialValues;
private FooBarQixConverter () {
SortedMap<Integer, String> specialValuesTmp = new TreeMap<Integer, String>();
specialValuesTmp.put(5, "Bar");
specialValuesTmp.put(3, "Foo");
specialValuesTmp.put(7, "Qix");
specialValues = Collections.unmodifiableSortedMap(specialValuesTmp);
}

// Singleton with static solution
private final static FooBarQixConverter instance = new FooBarQixConverter();
public static FooBarQixConverter getInstance() {
return instance;
}

/**
* Convert with following rules:
* if number can be divided by 3,5,7 add special Values
* if number contain char 3,5,7 add special Values
* if no special Values return the number
* @param number
* @return
*/
public String convert(int number) {
final String numberStr = String.valueOf(number);
StringBuilder result = new StringBuilder();

// if number can be divided by 3,5,7 add special Values
for (Entry<Integer, String> entry : specialValues.entrySet()) {
if (number % entry.getKey() == 0) {
result.append(entry.getValue());
}
}

// if number contain char 3,5,7 add special Values
for (char ch : numberStr.toCharArray()) {
int key = Character.digit(ch, 10);
if (specialValues.containsKey(key)) {
result.append(specialValues.get(key));
}
}

return result.length() == 0 ? numberStr : result.toString();
}
}
28 changes: 28 additions & 0 deletions src/ch/genevaJug/fooBarQix/Window.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ch.genevaJug.fooBarQix;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.Document;

public class Window extends JFrame {
private static final long serialVersionUID = 1L;

private JTextPane textPane = new JTextPane();

public Window () {
this.setTitle("FooBarQix");
this.setSize(300,500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(new JScrollPane(textPane));
this.setVisible(true);
}

public void addValue(String value) {
Document doc = textPane.getDocument();
textPane.setCaretPosition(doc.getLength());
textPane.replaceSelection(value + "\n");
}

}
29 changes: 29 additions & 0 deletions src/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
log4j.rootLogger=ERROR, console, file
#log4j.rootLogger=ERROR, console, file, mail

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{ISO8601}] %t %-5p %C{1}:%-4L %n%m %n

#log4j.appender.logfile.Threshold=INFO
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{ISO8601}] %t %-5p [%c{1}] - %n%m%n
log4j.appender.file.File=log/FooBarQixLogger.log
log4j.appender.file.MaxFileSize=512KB
log4j.appender.file.MaxBackupIndex=1

log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.SMTPHost=smtp.server
log4j.appender.mail.SMTPUsername=mail Authenticated by stmp server
log4j.appender.mail.SMTPPassword=password from Authenticated mail
log4j.appender.mail.Subject=Error!
log4j.appender.mail.To=to mail
log4j.appender.mail.From=from mail
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=Date : %d{ISO8601}%nThread : %t%nLevel : %-5p%nLogger : %c%n%m%n%n
log4j.appender.mail.BufferSize=10

# limit categories
log4j.logger.ch.genevaJug.fooBarQix.FooBarQix=DEBUG
53 changes: 53 additions & 0 deletions src/test/ch/genevaJug/fooBarQix/FooBarQixConverterTestJunit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package test.ch.genevaJug.fooBarQix;

import org.fest.assertions.Assertions;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import ch.genevaJug.fooBarQix.FooBarQixConverter;


public class FooBarQixConverterTestJunit {
@DataProvider(name = "intToValue")
public Object[][] getIntValueArray(){
return new Object[][] {
{1,"1"},
{2,"2"},
{3,"FooFoo"},
{4,"4"},
{5,"BarBar"},
{6,"Foo"},
{7,"QixQix"},
{8,"8"},
{9,"Foo"},
{10,"Bar"},
{11,"11"},
{12,"Foo"},
{13,"Foo"},
{14,"Qix"},
{15,"FooBarBar"},
{16,"16"},
{17,"Qix"},
{18,"Foo"},
{19,"19"},
{20,"Bar"},
{21,"FooQix"},
{22,"22"},
{23,"Foo"},
{30,"FooBarFoo"},
{33,"FooFooFoo"},
{35,"BarQixFooBar"},
{51,"FooBar"},
{53,"BarFoo"},
{55,"BarBarBar"},
{57,"FooBarQix"},
{75,"FooBarQixBar"},
};
}

@Test(dataProvider = "intToValue")
public void shouldReturnValue(int i, String value) {
FooBarQixConverter converter = FooBarQixConverter.getInstance();
Assertions.assertThat(converter.convert(i)).isEqualTo(value);
}
}