excercise 2 done
This commit is contained in:
62
week1_TinsaeGhilay/Main.java
Normal file
62
week1_TinsaeGhilay/Main.java
Normal file
@@ -0,0 +1,62 @@
|
||||
import java.io.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world. This is a demo of Greeter");
|
||||
// I think I hate me
|
||||
Greeter greeter = new Greeter("Fuck you");
|
||||
greeter.greet();
|
||||
}
|
||||
}
|
||||
|
||||
class Greeter {
|
||||
|
||||
String message;
|
||||
|
||||
// default message
|
||||
public Greeter(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
// greet
|
||||
public void greet() {
|
||||
this.message = getUserInput("What's your message?");
|
||||
String name = getUserInput("Who do you want to say that to?");
|
||||
|
||||
// if no message, we heve nothing to relay
|
||||
if(this.message == null){
|
||||
System.out.println("No messege to relay");
|
||||
return;
|
||||
}
|
||||
|
||||
// if no name, we have no name to relay massage to
|
||||
if(name == null){
|
||||
System.out.println("No name to relay message to");
|
||||
return;
|
||||
}
|
||||
|
||||
// else we print message
|
||||
System.out.println(message + "! " + name);
|
||||
}
|
||||
|
||||
// get user input
|
||||
private String getUserInput(String prompt) {
|
||||
System.out.print(prompt + ": ");
|
||||
|
||||
try {
|
||||
String input = "";
|
||||
int i;
|
||||
while ((i = System.in.read()) != '\n') {
|
||||
input = input + (char) i;
|
||||
}
|
||||
if (!input.isEmpty()) {
|
||||
return input;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading input");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user