Cześć wszystkim odwiedzającym ten wątek.
Biorę udział w bootcampie i przybywam do was z pytaniem. A więc do sedna, mam za zadanie zamienić poniższy kod ze strukturalnego na obiektowy.
import java.util.*;
import java.lang.*;
import java.io.*;
class Application
{
public static void main (String[] args) throws java.lang.Exception
{
String name = "Adam";
double age = 40.5;
double height = 178;
if(name != null) {
if(age > 30 && height > 160) {
System.out.println("User is older than 30 and higher then 160cm");
} else {
System.out.println("User is younger than 30 or lower than 160cm");
}
}
}
}
Nie wydaje się trudny, ale mam strasznie wymagającego mentora. Na chwilę obecną napisałem coś takiego:
package com;
public class Main {
public static void main(String[] args) {
Validator validator = new Validator();
validator.checkName();
}
}
class User{
private String name;
private double age;
private double height;
public User(String name, double age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public double getAge() {
return age;
}
public double getHeight() {
return height;
}
}
class Validator{
User user = new User("Adam", 40.5, 178);
public void checkName(){
if (user.getName() != null) {
checkAge();
}
}
public void checkAge(){
if (user.getAge() > 30) {
checkHeight();
}
else {
System.out.println("User is younger than 30 or lower than 160cm");
}
}
public void checkHeight(){
if (user.getHeight() > 160){
System.out.println("User is older than 30 and higher then 160cm");
}
else {
System.out.println("User is younger than 30 or lower than 160cm");
}
}
}
I teraz moje pytanie, czy Waszym zdaniem to jest poprawnie zmieniony kod? I jeśli nie, to jakie macie sugestie? Dzięki za każdą pomoc!