Skip to content

Commit 364dbe2

Browse files
authored
Merge pull request #213 from AlexandrAnatoliev/fix-issue-53
Fix issue 53
2 parents 40fbc95 + b470cfb commit 364dbe2

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Cooking Measurement Converter
2+
Simple utility to convert between common cooking measurements.
3+
4+
> **Author:** Alexandr Anatoliev
5+
6+
> **GitHub:** [AlexandrAnatoliev](https://github.com/AlexandrAnatoliev)
7+
8+
---
9+
10+
## Features
11+
* Supports common cooking measures:
12+
- Milliliters -> ml
13+
- Cups -> cup
14+
- Tablespoons -> tbsp
15+
- Teaspoons -> tsp
16+
* Enchanced console output with ANSI colors
17+
* Provides clear error messages for invalid input
18+
19+
---
20+
21+
## Project structure
22+
23+
```
24+
CookingMeasurementConverter/
25+
├── README.md
26+
└── src
27+
├── Colors.java
28+
├── Converter.java
29+
└── Measures.java
30+
```
31+
32+
---
33+
34+
## Compilation
35+
To compile the source classes:
36+
```
37+
javac -d bin src/*.java
38+
```
39+
40+
---
41+
42+
## Usage
43+
44+
* Navigate to `bin/` directory
45+
```
46+
cd bin/
47+
```
48+
* Run program
49+
```
50+
java Converter <arguments>
51+
```
52+
* Run from root directory
53+
```
54+
java -cp bin Converter <arguments>
55+
```
56+
* Input format:
57+
- [value] The quantity to convert
58+
- [from units] Source measurement unit (ml, cup, tbsp, tsp)
59+
- [to units] Target measurement unit (ml, cup, tbsp, tsp)
60+
```
61+
[value] [from units] [to units]
62+
```
63+
64+
---
65+
66+
## Examples of use
67+
```
68+
java -cp bin/ Converter 1 cup tbsp
69+
1 cup = 16 tbsp
70+
```
71+
72+
```
73+
java -cp bin/ Converter invalid input arguments
74+
75+
ERROR: For input string: "invalid"
76+
77+
Input format:
78+
Converter [value] [from units] [to units]
79+
Units:
80+
Milliliters -> ml
81+
Cups -> cup
82+
Tablespoons -> tbsp
83+
Teaspoons -> tsp
84+
Example use:
85+
java -cp bin/ Converter 1 cup tbsp
86+
```
87+
88+
---
89+
90+
## Requirements
91+
* Java 8 or higher
92+
* Terminal/console that supports ANSI colors codes
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Enum representation supported ANSI color codes for console output.
3+
*
4+
* @version 0.1.0
5+
* @since 2.12.2025
6+
* @author AlexandrAnatoliev
7+
*/
8+
public enum Colors {
9+
RED("\u001B[31m"),
10+
GREEN("\u001B[32m"),
11+
YELLOW("\u001B[33m"),
12+
/** Reset ANSI color code */
13+
RESET("\u001B[0m");
14+
15+
private final String code;
16+
17+
Colors(String code) {
18+
this.code = code;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return code;
24+
}
25+
26+
/**
27+
* Convenience method to colorize text
28+
*
29+
* @param text The text to colorize
30+
* @return The colorized string
31+
*/
32+
public String apply(String text) {
33+
return this + text + RESET;
34+
}
35+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Cooking Measurement Converter
3+
*
4+
* Simple utility to convert between common cooking measurements
5+
*
6+
* USAGE:
7+
* Input format:
8+
* [value] [from units] [to units]
9+
* Units:
10+
* Milliliters -> ml
11+
* Cups -> cup
12+
* Tablespoons -> tbsp
13+
* Teaspoons -> tsp
14+
* Example use:
15+
* java -cp bin/ Converter 1 cup tbsp
16+
*
17+
* @version 0.1.0
18+
* @since 2.12.2025
19+
* @author AlexandrAnatoliev
20+
*/
21+
public class Converter {
22+
/** Main entry point for the Converter application
23+
*
24+
* @param args command line arguments
25+
* [value] Value of user's product
26+
* [from units] Units need to convert from
27+
* [to units] Units need to convert to
28+
*/
29+
public static void main(String[] args) {
30+
if (args.length > 2) {
31+
try {
32+
int value = Integer.parseInt(args[0]);
33+
Measures from = parse(args[1]);
34+
Measures to = parse(args[2]);
35+
36+
if (from == Measures.ERROR || to == Measures.ERROR) {
37+
printExample();
38+
}
39+
else {
40+
System.out.println(value + " " + args[1] + " = "
41+
+ convert(value, from, to) + " " + args[2]);
42+
}
43+
} catch (IllegalArgumentException e) {
44+
System.out.println(Colors.RED.apply("\nERROR: " + e.getMessage()));
45+
printExample();
46+
}
47+
}
48+
else {
49+
printExample();
50+
}
51+
}
52+
53+
/**
54+
* Converts value from one units to other units
55+
*
56+
* @param value Value of user's product
57+
* @param from Units need to convert from
58+
* @param to Units need to convert to
59+
* @return int Result of converting
60+
*/
61+
public static int convert(int value, Measures from, Measures to) {
62+
return value * from.inMl / to.inMl;
63+
}
64+
65+
/**
66+
* Parses input string to Measures value
67+
*
68+
* @param input Measure in string
69+
* @throws IllegalArgumentException If input is illegal argument
70+
* @return int Result of parsing
71+
*/
72+
public static Measures parse(String input) {
73+
try {
74+
return Measures.valueOf(input.toUpperCase());
75+
} catch (IllegalArgumentException e) {
76+
System.out.println(Colors.RED.apply("ERROR: " + e.getMessage()));
77+
return Measures.ERROR;
78+
}
79+
}
80+
81+
/**
82+
* Prints usage instructions
83+
*/
84+
public static void printExample() {
85+
System.out.println(Colors.YELLOW.apply("\nInput format:"));
86+
System.out.println("Converter [value] [from units] [to units]");
87+
System.out.println(Colors.YELLOW.apply("Units:"));
88+
System.out.println("Milliliters -> ml");
89+
System.out.println("Cups -> cup");
90+
System.out.println("Tablespoons -> tbsp");
91+
System.out.println("Teaspoons -> tsp");
92+
System.out.println(Colors.YELLOW.apply("Example use:"));
93+
System.out.println("java -cp bin/ Converter 1 cup tbsp");
94+
}
95+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Enum representation of common cooking measures with conversation to milliliters.
3+
*
4+
* @version 0.1.0
5+
* @since 2.12.2025
6+
* @author AlexandrAnatoliev
7+
*/
8+
public enum Measures {
9+
ML(1),
10+
CUP(250),
11+
TBSP(15),
12+
TSP(5),
13+
ERROR(-1);
14+
15+
public final int inMl;
16+
17+
Measures(int inMl) {
18+
this.inMl = inMl;
19+
}
20+
}

0 commit comments

Comments
 (0)