Skip to content

Commit fac0657

Browse files
committed
Read from properties file, add to ArrayList, and randomize skill level
1 parent d932b00 commit fac0657

11 files changed

+95
-2
lines changed

bin/applicationContext.xml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<!-- Load values.properties file for properties injections using java annotations -->
1414
<context:property-placeholder location="classpath:values.properties" />
1515

16+
<!-- Load skillLevel.properties file for properties injection into CurrentSkillLevel -->
17+
<context:property-placeholder location="classpath:skillLevel.properties" />
18+
1619
</beans>
1720

1821

Binary file not shown.
Binary file not shown.
Binary file not shown.

bin/skillLevel.properties

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
You're just about the worse player I've ever seen. GET OFF MY FIELD!
2+
You have two left feet or something? Get outta here kid.
3+
A real butterfingers here. Ha! Why don't you join the chess club instead.
4+
Not born with much talent are you? Put in the practice and you might get somewhere.
5+
I guess sports isn't really for you...but you can stay.
6+
You're not bad... Not good, but not bad.
7+
An average player like you will mostly be benched with a few opportunities to play.
8+
You've got some talent in you. All you need is a nice polish and you'll be good.
9+
With your skill level, you'll make varsity in no time.
10+
Mama mia. A kid with your athleticism doesn't come around often. You're the new captain.
11+
HOLY COW! You're a real prodigy kid. Let's get you signed already!

src/applicationContext.xml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
<!-- Load values.properties file for properties injections using java annotations -->
1414
<context:property-placeholder location="classpath:values.properties" />
1515

16+
<!-- Load skillLevel.properties file for properties injection into CurrentSkillLevel -->
17+
<context:property-placeholder location="classpath:skillLevel.properties" />
18+
1619
</beans>
1720

1821

src/com/gnguyen92/springdemoannotations/AnnotationDemo.java

+10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public static void main(String[] args) {
8282
* this.interfaceObject = interfaceObject;
8383
* }
8484
*
85+
* ------------- values.properties Field Injections -------------------------------------
86+
*
87+
* 1) Create a properties file in src folder -> file contains key=value pairs
88+
*
89+
* 2) Load the properties file into xml config file
90+
* <context:property-placeholder location="classpath:fileName.properties" />
91+
*
92+
* 3) In SportCoach class, declare instance variables
93+
*
94+
* 4) Above variable declaration, add @Value("${keyName}")
8595
*
8696
*
8797
*/

src/com/gnguyen92/springdemoannotations/CurrentSkillLevel.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
@Component
77
public class CurrentSkillLevel implements SkillLevel {
88

9+
910
String[] skillLevels = {
1011
"God you're awful! You should consider spending your time elsewhere...",
1112
"You're getting pretty good, at least intermediate level I'd say.",
1213
"Wow. You've improved so much! You're like a prodigy!"
1314
};
14-
15+
1516
public String getSkillLevel() {
1617
int randIndex = (int)Math.floor(Math.random() * skillLevels.length);
1718

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.gnguyen92.springdemoannotations;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import org.springframework.stereotype.Component;
12+
13+
@Component
14+
public class FileRandomSkill implements SkillLevel {
15+
16+
private String filepath = "C:/Users/gnguy/Desktop/Java/spring-demo-annotations/src/skillLevel.properties";
17+
private List<String> skillLevels;
18+
19+
// create default constructor to read file and store values into array
20+
public FileRandomSkill() throws IOException {
21+
File file = new File(filepath);
22+
23+
System.out.println(">> Reading from file " + filepath);
24+
System.out.println(">> File exists: " + file.exists());
25+
26+
// initialize the array
27+
skillLevels = new ArrayList<String>();
28+
29+
// read from the file
30+
try {
31+
BufferedReader br = new BufferedReader(
32+
new FileReader(file));
33+
34+
// Add each line into skillLevels[]
35+
String temp;
36+
while((temp = br.readLine()) != null) {
37+
skillLevels.add(temp);
38+
}
39+
40+
br.close();
41+
42+
} catch (FileNotFoundException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
// ArrayList length method: arrayList.size()
48+
// ArrayList element at index: arrayList.get(i)
49+
public String getSkillLevel() {
50+
int randomIndex = (int)Math.floor(Math.random() * skillLevels.size());
51+
return skillLevels.get(randomIndex);
52+
}
53+
54+
}

src/com/gnguyen92/springdemoannotations/WrestlingCoach.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public WrestlingCoach(){
2525
// @Autowire the SkillLevel dependency
2626
// @Qualifier specifies which SkillLevel interface implementation this class will use
2727
@Autowired
28-
@Qualifier("proSkillLevel")
28+
@Qualifier("fileRandomSkill")
2929
public void setSkillLevel(SkillLevel currentSkill){
3030
System.out.println(">> WrestlingCoach: inside @Autowired @Qualifier Setter setSkillLevel()");
3131
System.out.println("Name: " + name);

src/skillLevel.properties

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
You're just about the worse player I've ever seen. GET OFF MY FIELD!
2+
You have two left feet or something? Get outta here kid.
3+
A real butterfingers here. Ha! Why don't you join the chess club instead.
4+
Not born with much talent are you? Put in the practice and you might get somewhere.
5+
I guess sports isn't really for you...but you can stay.
6+
You're not bad... Not good, but not bad.
7+
An average player like you will mostly be benched with a few opportunities to play.
8+
You've got some talent in you. All you need is a nice polish and you'll be good.
9+
With your skill level, you'll make varsity in no time.
10+
Mama mia. A kid with your athleticism doesn't come around often. You're the new captain.
11+
HOLY COW! You're a real prodigy kid. Let's get you signed already!

0 commit comments

Comments
 (0)