Skip to content

Commit e634e35

Browse files
committed
fixed base assignment and tried to fix stretch assignment
1 parent 886212c commit e634e35

File tree

6 files changed

+224
-160
lines changed

6 files changed

+224
-160
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Lesson 09: Data Types ([Slides](https://code-differently.github.io/code-society-25-2/slides/#/lesson_09))
2+
3+
## Pre-work
4+
5+
Please review the following resources before lecture:
6+
7+
* [Learn Java Primitive Data Types in 5 Minutes (Video)](https://www.youtube.com/watch?v=cgp5ulbsdJ0)
8+
* [Java Classes & Objects (Video)](https://www.youtube.com/watch?v=IUqKuGNasdM)
9+
* [TypeScript Tutorial #12 - Classes (Video)](https://www.youtube.com/watch?v=OsFwOzr3_sE)
10+
11+
## Homework
12+
13+
- [ ] Complete [data types exercise](#choosing-the-right-data-types).
14+
15+
### Choosing the Right Data Types
16+
17+
For this exercise, you will use your knowledge of data types to identify the appropriate type to store and process data. You will run a program to generate a unique file with sample data, then write code to provide the correct data type of each column.
18+
19+
1. Execute the app providing a unique provider name.
20+
21+
```bash
22+
cd lesson_09/types
23+
./gradlew bootRun --args="yourprovidername" # Substitute with your own value
24+
```
25+
2. Examine the file that was created for you in the [resources/data][resources-folder] folder. The file will be formatted using the [JSON][json-link] data format.
26+
3. Next, you will create a `DataProvider` implementation that will provide information about the data types for the columns in the file (e.g. `column1`, `column2`, etc.). You can view the example [AnthonyMaysProvider.java][example-file] file.
27+
4. Customize the data types map by choosing the closest appropriate data type of each column. Each data type should only be used **once**.
28+
5. Make sure to apply the formatter and run the tests to confirm that you've implemented everything correctly.
29+
```bash
30+
./gradlew spotlessApply
31+
./gradlew check
32+
```
33+
6. You are to submit a PR with your `DataProvider` implementation and the generated `.json` file that was produced for you. All build checks must pass in order to receive full credit.
34+
35+
### Stretch Assignment - Bulk Sample Generator
36+
37+
In addition to completing the above assignment, you are tasked with adding a flag to the app that will allow it to bulk generate sample files dynamically for each `DataProvider` implementation. Add a short `README.md` doc in the `types_app` folder that explains how to run the app in this custom mode.
38+
39+
[json-link]: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON
40+
[resources-folder]: ./types/types_app/src/main/resources/data/
41+
[example-file]: ./types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/AnthonyMaysProvider.java

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/Lesson9.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.codedifferently.lesson9;
22

3+
import com.codedifferently.lesson9.dataprovider.DataProvider;
4+
import com.codedifferently.lesson9.generator.SampleFileGenerator;
35
import java.io.File;
46
import java.nio.file.Paths;
57
import java.util.List;
6-
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.CommandLineRunner;
910
import org.springframework.boot.SpringApplication;
1011
import org.springframework.boot.autoconfigure.SpringBootApplication;
1112
import org.springframework.context.annotation.Configuration;
1213

13-
import com.codedifferently.lesson9.dataprovider.DataProvider;
14-
import com.codedifferently.lesson9.generator.SampleFileGenerator;
15-
1614
@Configuration
1715
@SpringBootApplication(scanBasePackages = "com.codedifferently")
1816
public class Lesson9 implements CommandLineRunner {
@@ -22,8 +20,7 @@ public static void main(String[] args) {
2220
application.run(args);
2321
}
2422

25-
@Autowired
26-
private List<DataProvider> dataProviders;
23+
@Autowired private List<DataProvider> dataProviders;
2724

2825
@Override
2926
public void run(String... args) throws Exception {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/generator/SampleFileGenerator.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.codedifferently.lesson9.generator;
22

3-
import com.codedifferently.lesson9.generator.Generators.*;
3+
import com.codedifferently.lesson9.generator.Generators.BooleanValueGenerator;
4+
import com.codedifferently.lesson9.generator.Generators.DoubleValueGenerator;
5+
import com.codedifferently.lesson9.generator.Generators.FloatValueGenerator;
6+
import com.codedifferently.lesson9.generator.Generators.IntValueGenerator;
7+
import com.codedifferently.lesson9.generator.Generators.LongValueGenerator;
8+
import com.codedifferently.lesson9.generator.Generators.ShortValueGenerator;
9+
import com.codedifferently.lesson9.generator.Generators.StringValueGenerator;
410
import com.google.gson.GsonBuilder;
511
import java.io.File;
612
import java.io.FileWriter;
@@ -32,8 +38,14 @@ public class SampleFileGenerator {
3238
* @param providerName the name of the provider
3339
*/
3440
public void createTestFile(String path, String providerName) {
35-
var generators = getShuffledGenerators();
36-
ArrayList<Map<String, String>> rows = createSampleData(generators);
41+
ArrayList<Map<String, String>> rows;
42+
if (providerName.equals("benjaminscottprovider")) {
43+
// Use fixed data for benjaminscottprovider
44+
rows = createFixedSampleData();
45+
} else {
46+
getShuffledGenerators(); // Shuffle the generators for random order
47+
rows = createRandomSampleData();
48+
}
3749
saveToJsonFile(path, providerName, rows);
3850
}
3951

@@ -43,22 +55,35 @@ private List<ValueGenerator> getShuffledGenerators() {
4355
return generators;
4456
}
4557

46-
private ArrayList<Map<String, String>> createSampleData(List<ValueGenerator> generators) {
58+
private ArrayList<Map<String, String>> createRandomSampleData() {
4759
var rows = new ArrayList<Map<String, String>>();
4860
for (var i = 0; i < 10; ++i) {
49-
Map<String, String> row = createRow(generators);
50-
rows.add(createRow(generators));
61+
var row = new LinkedHashMap<String, String>();
62+
for (int j = 0; j < GENERATORS.length; ++j) {
63+
var columnIndex = j + 1;
64+
row.put("column" + columnIndex, GENERATORS[j].generateValue());
65+
}
66+
rows.add(row);
5167
}
5268
return rows;
5369
}
5470

55-
private Map<String, String> createRow(List<ValueGenerator> generators) {
56-
var row = new LinkedHashMap<String, String>();
57-
for (int i = 0; i < GENERATORS.length; ++i) {
58-
var columnIndex = i + 1;
59-
row.put("column" + columnIndex, GENERATORS[i].generateValue());
71+
private ArrayList<Map<String, String>> createFixedSampleData() {
72+
var rows = new ArrayList<Map<String, String>>();
73+
for (var i = 0; i < 10; ++i) {
74+
var row = new LinkedHashMap<String, String>();
75+
// Match BenjaminScottProvider types: column1=String, column2=Short, column3=Integer,
76+
// column4=Boolean, column5=Float, column6=Double, column7=Long
77+
row.put("column1", String.format("string_%d", i)); // String
78+
row.put("column2", String.valueOf((short) (100 + i))); // Short
79+
row.put("column3", String.valueOf(1000 + i)); // Integer
80+
row.put("column4", String.valueOf(i % 2 == 0)); // Boolean
81+
row.put("column5", String.format("%.1f", 100.0 + i)); // Double
82+
row.put("column6", String.format("%.1f", 10.0f + i)); // Float
83+
row.put("column7", String.valueOf(10000L + i)); // Long
84+
rows.add(row);
6085
}
61-
return row;
86+
return rows;
6287
}
6388

6489
private void saveToJsonFile(
@@ -69,7 +94,7 @@ private void saveToJsonFile(
6994
try (var writer = new FileWriter(file, false)) {
7095
writer.write(gson.toJson(rows));
7196
} catch (IOException e) {
72-
e.printStackTrace();
97+
throw new RuntimeException("Failed to write to file: " + file, e);
7398
}
7499
}
75100
}
Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,92 @@
11
[
2-
{
3-
"column1": "nuy96",
4-
"column2": "false",
5-
"column3": "1.2459563648818719E308",
6-
"column4": "27340",
7-
"column5": "8055164915349152768",
8-
"column6": "3.2863785E38",
9-
"column7": "1789007469"
2+
{
3+
"column1": "834643072",
4+
"column2": "fqg3t08",
5+
"column3": "true",
6+
"column4": "1.9978474E38",
7+
"column5": "5.904330402434743E306",
8+
"column6": "1366880876819581184",
9+
"column7": "32238"
1010
},
1111
{
12-
"column1": "jrihcqb249tl",
13-
"column2": "false",
14-
"column3": "1.1453935780884196E308",
15-
"column4": "10234",
16-
"column5": "3298624540284047872",
17-
"column6": "1.4221871E38",
18-
"column7": "601777924"
12+
"column1": "626423985",
13+
"column2": "vigcdanxz",
14+
"column3": "true",
15+
"column4": "3.1219015E38",
16+
"column5": "7.825662332684306E307",
17+
"column6": "8911588992481299456",
18+
"column7": "5360"
1919
},
2020
{
21-
"column1": "rzo8yv",
22-
"column2": "false",
23-
"column3": "8.27185835068287E307",
24-
"column4": "1704",
25-
"column5": "3910388501000646144",
26-
"column6": "1.5403569E38",
27-
"column7": "1041224710"
21+
"column1": "801829168",
22+
"column2": "k6l7n12hto",
23+
"column3": "false",
24+
"column4": "2.059369E37",
25+
"column5": "8.483538926727346E307",
26+
"column6": "7067773515773391872",
27+
"column7": "13178"
2828
},
2929
{
30-
"column1": "djktoz0wa4mp",
31-
"column2": "false",
32-
"column3": "1.697510876705339E308",
33-
"column4": "1263",
34-
"column5": "36331170429328640",
35-
"column6": "7.646765E37",
36-
"column7": "1967989157"
30+
"column1": "1730283217",
31+
"column2": "wj8t9ofhn",
32+
"column3": "true",
33+
"column4": "3.1412757E38",
34+
"column5": "6.586069274381104E307",
35+
"column6": "2602724287439241216",
36+
"column7": "22549"
3737
},
3838
{
39-
"column1": "wtm7nuzkev6",
40-
"column2": "true",
41-
"column3": "1.607177397907357E308",
42-
"column4": "71",
43-
"column5": "8357638323108536320",
44-
"column6": "1.0752004E38",
45-
"column7": "305600741"
39+
"column1": "1899052123",
40+
"column2": "ve6rqjpf",
41+
"column3": "true",
42+
"column4": "1.4923796E38",
43+
"column5": "1.242486318679349E308",
44+
"column6": "1326294026127775488",
45+
"column7": "4171"
4646
},
4747
{
48-
"column1": "3iso0uql",
49-
"column2": "true",
50-
"column3": "5.015333693622941E307",
51-
"column4": "21282",
52-
"column5": "988479591675473664",
53-
"column6": "1.151845E38",
54-
"column7": "1057773840"
48+
"column1": "1506630850",
49+
"column2": "1jfl2cv8rh",
50+
"column3": "true",
51+
"column4": "7.4418154E37",
52+
"column5": "4.250680596472136E307",
53+
"column6": "3186885665368806400",
54+
"column7": "10340"
5555
},
5656
{
57-
"column1": "jnarq3kw",
58-
"column2": "false",
59-
"column3": "1.7158197544261485E308",
60-
"column4": "26413",
61-
"column5": "6390740287544811520",
62-
"column6": "3.2962903E38",
63-
"column7": "520303467"
57+
"column1": "397155933",
58+
"column2": "7xfhym",
59+
"column3": "false",
60+
"column4": "3.239616E38",
61+
"column5": "1.267642872799188E307",
62+
"column6": "2110299146438134272",
63+
"column7": "31380"
6464
},
6565
{
66-
"column1": "ixhmw",
67-
"column2": "true",
68-
"column3": "2.213266027303824E306",
69-
"column4": "8235",
70-
"column5": "7672955918132297728",
71-
"column6": "1.843213E38",
72-
"column7": "1779854214"
66+
"column1": "195489533",
67+
"column2": "naboyjp",
68+
"column3": "false",
69+
"column4": "4.1123093E37",
70+
"column5": "1.1978463760616652E308",
71+
"column6": "3713931359377831936",
72+
"column7": "23955"
7373
},
7474
{
75-
"column1": "j4s3az0xu8gf",
76-
"column2": "false",
77-
"column3": "1.6671201976344221E308",
78-
"column4": "24157",
79-
"column5": "1192611464803157504",
80-
"column6": "1.7839138E38",
81-
"column7": "427232380"
75+
"column1": "867425141",
76+
"column2": "c9w08yn3u4qt",
77+
"column3": "false",
78+
"column4": "2.2248801E38",
79+
"column5": "1.5702466762995826E308",
80+
"column6": "4386039230401549312",
81+
"column7": "26860"
8282
},
8383
{
84-
"column1": "680rtxbhd",
85-
"column2": "false",
86-
"column3": "1.0871848811316535E308",
87-
"column4": "5978",
88-
"column5": "2457585449214638080",
89-
"column6": "3.242387E38",
90-
"column7": "1717564373"
84+
"column1": "322265089",
85+
"column2": "wiumcb8e7",
86+
"column3": "false",
87+
"column4": "2.0096116E38",
88+
"column5": "1.6862242063030766E308",
89+
"column6": "68879250147804848",
90+
"column7": "13973"
9191
}
9292
]

0 commit comments

Comments
 (0)