Skip to content

Commit c6378e6

Browse files
committed
Initial commit
0 parents  commit c6378e6

File tree

23 files changed

+1098
-0
lines changed

23 files changed

+1098
-0
lines changed

.gitignore

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# course config files
2+
*.yaml
3+
4+
# gradle
5+
.gradle/
6+
build
7+
8+
9+
# idea config
10+
.idea
11+
12+
# old stages
13+
/Encryption-Decryption (Java)/*
14+
!/Encryption-Decryption (Java)/task/
15+
/Encryption-Decryption (Java)/task/*
16+
!/Encryption-Decryption (Java)/task/src
17+
!/Encryption-Decryption (Java)/task/output.txt
18+
19+
# Project exclude paths
20+
/Topics/Factory method/Burger store/out/
21+
/Topics/Factory method/Burger store/out/production/classes/
22+
/Topics/Factory method/Clocks/out/
23+
/Topics/Factory method/Clocks/out/production/classes/
24+
/Topics/Factory method/Robot/out/
25+
/Topics/Factory method/Robot/out/production/classes/
26+
/Topics/Factory method/Robot factory/out/
27+
/Topics/Factory method/Robot factory/out/production/classes/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
i
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package encryptdecrypt;
2+
3+
import java.io.*;
4+
import java.util.Scanner;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
for (int i = 0; i < args.length; i += 2) {
9+
switch (args[i]) {
10+
case "-mode" -> mode = args[i + 1];
11+
case "-data" -> data = args[i + 1];
12+
case "-key" -> key = Integer.parseInt(args[i + 1]);
13+
case "-in" -> inputFileName = args[i + 1];
14+
case "-out" -> outputFileName = args[i + 1];
15+
case "-alg" -> alg = args[i + 1];
16+
}
17+
}
18+
19+
boolean readFromFile = !inputFileName.isEmpty();
20+
boolean writeToFile = !outputFileName.isEmpty();
21+
22+
if (!data.isEmpty() && !inputFileName.isEmpty()) {
23+
readFromFile = false;
24+
}
25+
26+
if (readFromFile){
27+
File file = new File(inputFileName);
28+
try (Scanner fileScanner = new Scanner(file)) {
29+
data = fileScanner.nextLine();
30+
} catch (FileNotFoundException e) {
31+
System.out.println("Error " + e.getMessage());
32+
}
33+
}
34+
35+
switch (mode) {
36+
case "enc" -> {
37+
if ("unicode".equals(alg)) {
38+
unicode(data, key, writeToFile);
39+
} else {
40+
shift(data, key, writeToFile);
41+
}
42+
}
43+
case "dec" -> {
44+
if ("unicode".equals(alg)) {
45+
unicodeDecrypt(data, key, writeToFile);
46+
} else {
47+
shiftDecrypt(data, key, writeToFile);
48+
}
49+
}
50+
}
51+
}
52+
53+
public static void unicode(String message, int key, boolean writeToFile) {
54+
StringBuilder sb = new StringBuilder();
55+
for (int i = 0; i < message.length(); i++) {
56+
char ch = message.charAt(i);
57+
int ascii = (int) ch + key;
58+
sb.append((char) ascii);
59+
}
60+
showResults(writeToFile, sb);
61+
}
62+
63+
public static void shift(String message, int key, boolean writeToFile) {
64+
StringBuilder sb = new StringBuilder();
65+
for (int i = 0; i < message.length(); i++) {
66+
char ch = message.charAt(i);
67+
if (ch >= 'A' && ch <= 'Z') {
68+
sb.append((char) ((ch - 'A' + key) % 26 + 'A'));
69+
} else if (ch >= 'a' && ch <= 'z') {
70+
sb.append((char) ((ch - 'a' + key) % 26 + 'a'));
71+
} else sb.append(ch);
72+
}
73+
showResults(writeToFile, sb);
74+
}
75+
76+
public static void unicodeDecrypt(String message, int key, boolean writeToFile) {
77+
StringBuilder sb = new StringBuilder();
78+
for (int i = 0; i < message.length(); i++) {
79+
char ch = message.charAt(i);
80+
int ascii = (int) ch - key;
81+
sb.append((char) ascii);
82+
}
83+
showResults(writeToFile, sb);
84+
}
85+
86+
public static void shiftDecrypt(String message, int key, boolean writeToFile) {
87+
StringBuilder sb = new StringBuilder();
88+
for (int i = 0; i < message.length(); i++) {
89+
char ch = message.charAt(i);
90+
if (ch >= 'A' && ch <= 'Z') {
91+
sb.append((char) (((ch - 'A' - key + 26) % 26) + 'A'));
92+
} else if (ch >= 'a' && ch <= 'z') {
93+
sb.append((char) (((ch - 'a' - key + 26) % 26) + 'a'));
94+
} else sb.append(ch);
95+
}
96+
showResults(writeToFile, sb);
97+
}
98+
99+
private static void showResults(boolean writeToFile, StringBuilder sb) {
100+
if (writeToFile) {
101+
try {
102+
fw = new FileWriter(outputFileName);
103+
fw.write(String.valueOf(sb));
104+
} catch (IOException e) {
105+
System.out.println("Error " + e.getMessage());
106+
} finally {
107+
try {
108+
if (fw != null) {
109+
fw.close();
110+
}
111+
} catch (IOException e) {
112+
System.out.println("Error " + e.getMessage());
113+
}
114+
}
115+
} else {
116+
System.out.print(sb);
117+
}
118+
}
119+
120+
static FileWriter fw = null;
121+
static String outputFileName = "";
122+
static String mode = "enc", data = "", inputFileName = "", alg = "shift";
123+
static int key = 0;
124+
}

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This is the *Encryption-Decryption (Java)* project I made myself.
2+
3+
4+
<p>Privacy is an important matter is the realm of the Internet. When sending a message, you want to be sure that no-one but the addressee with the key can read it. The entirety of the modern Web is encrypted - take https for example! Don’t stay behind: hop on the encryption/decryption train and learn the essential basics while implementing this simple project.</p><br/><br/>Learn more at <a href="https://hyperskill.org/projects/46?utm_source=ide&utm_medium=ide&utm_campaign=ide&utm_content=project-card">https://hyperskill.org/projects/46</a>
5+
6+
Here's the link to the project: https://hyperskill.org/projects/46
7+
8+
Check out my profile: https://hyperskill.org/profile/562786705
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class TestDrive {
2+
public static void main(String[] args) throws InterruptedException {
3+
/* write your code here */
4+
BurgerStore store = new BurgerStore();
5+
store.orderBurger("Chinese");
6+
store.orderBurger("American");
7+
store.orderBurger("Russian");
8+
}
9+
}
10+
11+
abstract class BurgerFactory {
12+
13+
abstract Burger createBurger(String type);
14+
15+
@SuppressWarnings("UnusedReturnValue")
16+
Burger orderBurger(String type) throws InterruptedException {
17+
Burger burger = createBurger(type);
18+
if (burger == null) {
19+
System.out.println("Sorry, we are unable to create this kind of burger\n");
20+
return null;
21+
}
22+
System.out.println("Making a " + burger.getName());
23+
burger.putBun();
24+
burger.putCutlet();
25+
burger.putSauce();
26+
Thread.sleep(1500L);
27+
System.out.println(burger.getName() + " ready" + "\n");
28+
return burger;
29+
}
30+
}
31+
32+
class BurgerStore extends BurgerFactory {
33+
@Override
34+
Burger createBurger(String type) {
35+
return switch (type) {
36+
case "Chinese" -> new ChineseBurger("Chinese Burger");
37+
case "American" -> new AmericanBurger("American Burger");
38+
case "Russian" -> new RussianBurger("Russian Burger");
39+
default -> null;
40+
};
41+
}
42+
}
43+
44+
abstract class Burger {
45+
private final String name;
46+
47+
Burger(String name) {
48+
this.name = name;
49+
}
50+
51+
String getName() {
52+
return name;
53+
}
54+
55+
void putBun() {
56+
System.out.println("Putting bun");
57+
}
58+
59+
void putCutlet() {
60+
System.out.println("Putting patty");
61+
}
62+
63+
void putSauce() {
64+
System.out.println("Putting sauce");
65+
}
66+
67+
}
68+
69+
class ChineseBurger extends Burger {
70+
/* write your code here */
71+
ChineseBurger(String name) {
72+
super(name);
73+
}
74+
}
75+
76+
class AmericanBurger extends Burger {
77+
/* write your code here */
78+
AmericanBurger(String name) {
79+
super(name);
80+
}
81+
}
82+
83+
class RussianBurger extends Burger {
84+
/* write your code here */
85+
RussianBurger(String name) {
86+
super(name);
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h2>Burger store</h2>
2+
<div class="step-text">
3+
<p></p><p>In this task, you will have to write some working code by yourself! But don't worry: you can find some hints in the correct output and in parts of the code.</p><p>Let's create the <code class="java">BurgerStore</code>. We will stick to the good old classic and simply create a burger with <code class="java">Bun</code>, <code class="java">Patty</code> and <code class="java">Sauce</code>. Don't forget that <code class="java">FactoryMethod</code><em> </em>does not include details — it only knows the general creation process. </p><br><b>Sample Input:</b><br><pre><code class="language-no-highlight"></code></pre><br><b>Sample Output:</b><br><pre><code class="language-no-highlight">Making a Chinese Burger<br>Putting bun<br>Putting patty<br>Putting sauce<br>Chinese Burger ready<br><br>Making a American Burger<br>Putting bun<br>Putting patty<br>Putting sauce<br>American Burger ready<br><br>Making a Russian Burger<br>Putting bun<br>Putting patty<br>Putting sauce<br>Russian Burger ready</code></pre><br><br><br><font color="gray">Memory limit: 256 MB</font><br><font color="gray">Time limit: 5 seconds</font><br><br>
4+
</div>
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.Scanner;
2+
3+
/* Product - Clock */
4+
interface Clock {
5+
void tick();
6+
}
7+
8+
/* Concrete Product - Sand Clock */
9+
class SandClock implements Clock {
10+
11+
@Override
12+
public void tick() {
13+
System.out.println("...sand noise...");
14+
}
15+
}
16+
17+
/* Concrete Product - Digital Clock */
18+
class DigitalClock implements Clock {
19+
20+
@Override
21+
public void tick() {
22+
System.out.println("...pim...");
23+
}
24+
}
25+
26+
/* Concrete Product - Mechanical Clock */
27+
class MechanicalClock implements Clock {
28+
29+
@Override
30+
public void tick() {
31+
System.out.println("...clang mechanism...");
32+
}
33+
}
34+
35+
class ClockFactory {
36+
37+
/* It produces concrete clocks corresponding their types : Digital, Sand or Mechanical */
38+
public Clock produce(String type) {
39+
// write your code here ...
40+
return "Sand".equals(type) ? new SandClock()
41+
: "Digital".equals(type) ? new DigitalClock()
42+
: new MechanicalClock();
43+
}
44+
}
45+
46+
public class Main {
47+
public static void main(String[] args) {
48+
final Scanner scanner = new Scanner(System.in);
49+
final String type = scanner.next();
50+
final ClockFactory clockFactory = new ClockFactory();
51+
final Clock clock = clockFactory.produce(type);
52+
clock.tick();
53+
scanner.close();
54+
}
55+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h2>Clocks</h2>
2+
<div class="step-text">
3+
<p>The given classes are the <code class="java">Clock</code> interface of products, specified clocks, and the factory class <code class="java">ClockFactory</code> to produce instances.</p>
4+
<p>Your task is to implement the factory method <code class="java">produce</code>. It should return a clock according to the specified type string:</p>
5+
<ul>
6+
<li>"Sand" — <code class="java">SandClock</code>;</li>
7+
<li>"Digital" — <code class="java">DigitalClock</code>;</li>
8+
<li>"Mechanical" — <code class="java">MechanicalClock</code>.</li>
9+
</ul>
10+
<p>Please, do not change the provided code of the clock classes.</p><br><b>Sample Input:</b><br><pre><code class="language-no-highlight">Digital</code></pre><br><b>Sample Output:</b><br><pre><code class="language-no-highlight">...pim...</code></pre><br><br><br><font color="gray">Memory limit: 256 MB</font><br><font color="gray">Time limit: 5 seconds</font><br><br>
11+
</div>

0 commit comments

Comments
 (0)