From f72cca62287e86c5c56441855485034c135bb2e5 Mon Sep 17 00:00:00 2001 From: KristerTarnamaa Date: Mon, 29 May 2017 06:28:08 +0300 Subject: [PATCH 1/2] =?UTF-8?q?kodut=C3=B6=C3=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ pom.xml | 32 +++++++++++++++ src/main/java/app/Fruit.java | 35 ++++++++++++++++ src/main/java/app/Main.java | 50 +++++++++++++++++++++++ src/main/java/app/calculator.java | 6 +++ src/main/resources/application.properties | 1 + src/main/resources/public/home.html | 36 ++++++++++++++++ 7 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/app/Fruit.java create mode 100644 src/main/java/app/Main.java create mode 100644 src/main/java/app/calculator.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/public/home.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc60410 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target +.idea +app.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d0ac2a6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + app + app + jar + 1 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.2.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + + 1.8 + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/src/main/java/app/Fruit.java b/src/main/java/app/Fruit.java new file mode 100644 index 0000000..59c6004 --- /dev/null +++ b/src/main/java/app/Fruit.java @@ -0,0 +1,35 @@ +package app; + +public class Fruit implements Calculator{ + + int amount; + double price; + String name; + public Fruit(String name, int amount, double price){ + + this.name = name; + this.amount = amount; + this.price = price; + + } + public void increment(){ + + this.amount = this.amount+1; + + } + public double sum(){ + + return this.amount*price; + + } + public String getName(){ + + return this.name; + } + public String toString(){ + + return "Number of "+name+"s: "+this.amount+". Price for one: "+price+". Sum: "+sum(); + + } + +} \ No newline at end of file diff --git a/src/main/java/app/Main.java b/src/main/java/app/Main.java new file mode 100644 index 0000000..c694c47 --- /dev/null +++ b/src/main/java/app/Main.java @@ -0,0 +1,50 @@ +package app; + +import java.util.*; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@RestController +public class Main{ + + + Fruit apple = new Fruit("Apple", 1, 3); + Fruit banana = new Fruit("Banana", 1, 2); + + List fruits = new ArrayList(){{ + add(apple); + add(banana); + }}; + + @RequestMapping("/increment") + public String increment(String fruit){ + + for(Fruit f: fruits){ + if(f.getName() == fruit){ + f.increment(); + return f.toString(); + } + } + return "error"; + } + + @RequestMapping("/sum") + public String calculateSum(String fruit){ + + for(Fruit f: fruits){ + if(f.getName() == fruit){ + return "Sum: "+f.sum(); + } + } + return "Error"; + } + + public static void main(String[] args){ + + SpringApplication.run(Main.class, args); + + } +} \ No newline at end of file diff --git a/src/main/java/app/calculator.java b/src/main/java/app/calculator.java new file mode 100644 index 0000000..aea48e2 --- /dev/null +++ b/src/main/java/app/calculator.java @@ -0,0 +1,6 @@ +package app; + +public interface Calculator{ + public void increment(); + public double sum(); +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..839ca75 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port = 8080 \ No newline at end of file diff --git a/src/main/resources/public/home.html b/src/main/resources/public/home.html new file mode 100644 index 0000000..56615ec --- /dev/null +++ b/src/main/resources/public/home.html @@ -0,0 +1,36 @@ + + +Calculator + + + + + + + + + + + +
+ + \ No newline at end of file From d2243e37ecf04f76ce40711c85b7667671d60467 Mon Sep 17 00:00:00 2001 From: KristerTarnamaa Date: Tue, 30 May 2017 14:05:05 +0300 Subject: [PATCH 2/2] =?UTF-8?q?t=C3=B6=C3=B6tab?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/Calculator.java | 6 ++++++ src/main/java/app/Main.java | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 src/main/java/app/Calculator.java diff --git a/src/main/java/app/Calculator.java b/src/main/java/app/Calculator.java new file mode 100644 index 0000000..aea48e2 --- /dev/null +++ b/src/main/java/app/Calculator.java @@ -0,0 +1,6 @@ +package app; + +public interface Calculator{ + public void increment(); + public double sum(); +} \ No newline at end of file diff --git a/src/main/java/app/Main.java b/src/main/java/app/Main.java index c694c47..ea3fb37 100644 --- a/src/main/java/app/Main.java +++ b/src/main/java/app/Main.java @@ -23,7 +23,7 @@ public class Main{ public String increment(String fruit){ for(Fruit f: fruits){ - if(f.getName() == fruit){ + if(f.getName().equals(fruit)){ f.increment(); return f.toString(); } @@ -35,7 +35,7 @@ public String increment(String fruit){ public String calculateSum(String fruit){ for(Fruit f: fruits){ - if(f.getName() == fruit){ + if(f.getName().equals(fruit)){ return "Sum: "+f.sum(); } }