changed the file structure
This commit is contained in:
parent
cd81159162
commit
b1c3582880
391 changed files with 675 additions and 0 deletions
BIN
Code/Steiner/oop_theorie_1/src/App.class
Normal file
BIN
Code/Steiner/oop_theorie_1/src/App.class
Normal file
Binary file not shown.
16
Code/Steiner/oop_theorie_1/src/App.java
Normal file
16
Code/Steiner/oop_theorie_1/src/App.java
Normal file
|
@ -0,0 +1,16 @@
|
|||
// App.java
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
Date my_birthday = new Date();
|
||||
|
||||
// Input birthday using the Date class method
|
||||
my_birthday.inputBirthday();
|
||||
|
||||
// Print the entered birthday
|
||||
System.out.println("Your birthday: " + my_birthday.day + " " + my_birthday.month + " " + my_birthday.year);
|
||||
|
||||
// Calculate and print the age
|
||||
int age = my_birthday.calculateAge();
|
||||
System.out.println("Your age is: " + age);
|
||||
}
|
||||
}
|
BIN
Code/Steiner/oop_theorie_1/src/Date.class
Normal file
BIN
Code/Steiner/oop_theorie_1/src/Date.class
Normal file
Binary file not shown.
66
Code/Steiner/oop_theorie_1/src/Date.java
Normal file
66
Code/Steiner/oop_theorie_1/src/Date.java
Normal file
|
@ -0,0 +1,66 @@
|
|||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.Month;
|
||||
import java.util.Locale;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Date {
|
||||
public int day;
|
||||
public String month;
|
||||
public int year;
|
||||
|
||||
// Method to get the current date as a formatted string
|
||||
public String getCurrentDate() {
|
||||
LocalDate now = LocalDate.now(); // Get current date only
|
||||
return now.toString();
|
||||
}
|
||||
|
||||
// Method to calculate the age based on the birthday
|
||||
public int calculateAge() {
|
||||
int monthNumber = getMonthNumber();
|
||||
if (monthNumber == -1) {
|
||||
throw new IllegalArgumentException("Invalid month name: " + month);
|
||||
}
|
||||
|
||||
LocalDate birthDate = LocalDate.of(year, monthNumber, day);
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
return Period.between(birthDate, currentDate).getYears();
|
||||
}
|
||||
|
||||
// Method to convert the month name to a month number
|
||||
private int getMonthNumber() {
|
||||
try {
|
||||
Month monthEnum = Month.valueOf(month.toUpperCase(Locale.ENGLISH));
|
||||
return monthEnum.getValue();
|
||||
} catch (IllegalArgumentException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Method to input and set the date fields (day, month, year)
|
||||
public void inputBirthday() {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
// Input for day
|
||||
System.out.println("Enter your birth day (e.g., 16):");
|
||||
this.day = scan.nextInt();
|
||||
|
||||
// Input for month with validation loop
|
||||
scan.nextLine(); // Consume the newline
|
||||
boolean validMonth = false;
|
||||
while (!validMonth) {
|
||||
System.out.println("Enter your birth month (e.g., November):");
|
||||
this.month = scan.nextLine();
|
||||
|
||||
if (getMonthNumber() != -1) {
|
||||
validMonth = true;
|
||||
} else {
|
||||
System.out.println("Invalid month name. Please try again.");
|
||||
}
|
||||
}
|
||||
|
||||
// Input for year
|
||||
System.out.println("Enter your birth year (e.g., 2005):");
|
||||
this.year = scan.nextInt();
|
||||
}
|
||||
}
|
BIN
Code/Steiner/oop_theorie_1/src/Time.class
Normal file
BIN
Code/Steiner/oop_theorie_1/src/Time.class
Normal file
Binary file not shown.
13
Code/Steiner/oop_theorie_1/src/Time.java
Normal file
13
Code/Steiner/oop_theorie_1/src/Time.java
Normal file
|
@ -0,0 +1,13 @@
|
|||
import java.time.LocalDateTime;
|
||||
|
||||
public class Time {
|
||||
public static void main(String[] args) {
|
||||
// Get the current date and time
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
// Format the current date and time
|
||||
|
||||
// Print the formatted date and time
|
||||
System.out.println("Current date and time: " + now);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue