Skip to content
Snippets Groups Projects
Commit e745c96c authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# Mac file finder metadata
.DS_Store
# Windows file metadata
._*
# Thumbnail image caches
Thumbs.db
ethumbs.db
# MS Office temporary file
~*
# Emacs backup file
*~
# Common
[Bb]in/
[Bb]uild/
[Oo]bj/
[Oo]ut/
[Tt]mp/
[Xx]86/
[Ii][Aa]32/
[Xx]64/
[Xx]86_64/
[Xx]86-64/
[Aa]rm
[Aa]32
[Tt]32
[Aa]64
*.tmp
*.bak
*.bk
*.swp
# Java files
*.class
javadoc/
# Maven
target/
# JetBrains (IntelliJ IDEA, PyCharm, etc) files
.idea/
cmake-build-*/
*.iml
*.iws
*.ipr
# Eclipse files
.settings/
.project
.classpath
.buildpath
.loadpath
.factorypath
local.properties
pom.xml 0 → 100644
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.unl.cse.soft160.switches</groupId>
<artifactId>switches</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>switches</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package edu.unl.cse.soft160.switches;
import java.util.Scanner;
public class IfSwitch {
private static void welcomeUser() {
System.out.println("Welcome to the number converter!");
}
private static int getInput(Scanner scanner) {
System.out.print("Enter a number between 1 and 3: ");
String numberText = scanner.nextLine();
int number = Integer.parseInt(numberText);
return number;
}
private static void outputResult(String outputString) {
System.out.println("You entered " + outputString + ".");
}
private static String convertToWord(int value) {
String numberText;
if (value == 1) {
numberText = "one";
} else if (value == 2) {
numberText = "two";
} else if (value == 3) {
numberText = "three";
} else {
numberText = "an out-of-range value";
}
return numberText;
}
/*
private static String convertToWord(int value) {
String numberText;
switch (value) {
case 1:
numberText = "one";
break;
case 2:
numberText = "two";
break;
case 3:
numberText = "three";
break;
default:
numberText = "an out-of-range value";
}
return numberText;
}
*/
public static void main(String... arguments) {
welcomeUser();
Scanner scanner = new Scanner(System.in);
int inputNumber = getInput(scanner);
scanner.close();
String numberString = convertToWord(inputNumber);
outputResult(numberString);
}
}
package edu.unl.cse.soft160.switches;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import static org.junit.Assert.*;
public class IfSwitchTest {
private static String assemble(String... lines) {
return String.join("\n", lines) + "\n";
}
private static String runMain(String... inputs) {
InputStream in = System.in;
PrintStream out = System.out;
System.setIn(new ByteArrayInputStream(assemble(inputs).getBytes()));
ByteArrayOutputStream collector = new ByteArrayOutputStream();
System.setOut(new PrintStream(collector));
IfSwitch.main();
System.setIn(in);
System.setOut(out);
return collector.toString();
}
private static void testConvert(int inputNumber, String outputWord) {
String expectedOutput = "Welcome to the number converter!\n" +
"Enter a number between 1 and 3: " +
"You entered " + outputWord + ".\n";
String actualOutput = runMain(Integer.toString(inputNumber));
assertEquals(expectedOutput, actualOutput);
}
@Test
public void testConvertOne() {
testConvert(1, "one");
}
@Test
public void testConvertTwo() {
testConvert(2, "two");
}
@Test
public void testConvertThree() {
testConvert(3, "three");
}
@Test
public void testConvertZero() {
testConvert(0, "an out-of-range value");
}
@Test
public void testConvertFour() {
testConvert(4, "an out-of-range value");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment