Geburtsdatum einlesen

  • Themenstarter Themenstarter Timer123
  • Beginndatum Beginndatum
T

Timer123

Gast
Hallo Leute, mein Programm soll mir mein Geburtsdatum ausgeben
Allerdings sollen verschiedene Fehlermeldungen geworfen werden, sobald ein unrealistisches Datum eingetippt wird.

Meine Frage, warum schlägt die "IsFutureTimeException" nicht an wenn ich z.B. eingebe:

Geburtsjahr: 2100... das datum liegt doch in der Zukunft

Wenn ich aber eingebe: Geburtsjahr 2010

und Monat: 7

dann wirft er einen Exception..also dann funktioniert es

Kann mir einer bitte sagen wo der Fehler liegt? Glaube irgendwo in der Klasse "Year" aber keine Ahnung wo 🙁(

Hier meine Klassen:


Main

Java:
package edu.hm.cs.swe2.exceptions;

import edu.hm.cs.swe2.exceptions.datecomponents.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws Exception { 
		
		
		// Objekte Year, Month, Day werden referenziert
		Year year = new Year();
		Month month = new Month();
		Day day = new Day ();
		
		// boolische Variable um aus der drauffolgenden Schleife rauszuspringen
		boolean isValid = true;
		
		// Implementierung des im Computer vorhandenen Kalendars 
		Calendar rightNow = Calendar.getInstance();
		
		
		// Ausgabe von Year, Month und Day, falls die Eingabe gültig ist
		do {
			year.requestBirthYear(rightNow);
			month.requestBirthMonth(rightNow, year.getYear());
			day.requestBirthDay(rightNow, year.getYear(), month.getMonth());
			
			isValid = Day.validateDay(day.getDay(),month.getMonth(), year.getYear());
			
		} while (!isValid);
		
		// Ausgabe in geordneter Reihenfolge 
		System.out.println("Du hast am " + day + "." + month + "." + year + " Geburtstag");
	}
}

Year:

Java:
package edu.hm.cs.swe2.exceptions.datecomponents;

import java.io.*;
import java.util.*;
import edu.hm.cs.swe2.exceptions.exceptionclasses.IllegalMonthException;
import edu.hm.cs.swe2.exceptions.exceptionclasses.IsFutureDateException;


public class Year {

	// Klassenattribut
	private int year;

	// Costum- und Default-Konstruktor
	public Year(int year) {
		this.year = year;
	}

	public Year() {

	}

	// Methode, welche für die Eingabe einer Jahreszahl, welche
	// anschließend auf Richtigkeit überprüft wird, benötigt wird

	public void requestBirthYear(Calendar rightNow)
			throws NumberFormatException, IllegalMonthException,
			IsFutureDateException {
		int i = 0;

		while (true) {
			try {
				BufferedReader input = new BufferedReader(
						new InputStreamReader(System.in));
				System.out.println("Bitte gib dein Geburtsjahr ein:");
				i = Integer.parseInt(input.readLine());

				if (year > rightNow.get(Calendar.YEAR)) {
					throw new IsFutureDateException(
							"Eingegebenes Jahr liegt in der Zukunft, kann nicht dein Geburtsjahr sein!");
				}

				year = i;
				break;
			} catch (IsFutureDateException e) {
				e.printStackTrace(System.out);
			} catch (NumberFormatException e) {
				System.out.println("Ouch, das war keine Zahl!!!");
				e.printStackTrace(System.out);
			} catch (IOException e) {
				e.printStackTrace(System.out);
			}

		}
	}

	// Überschriebene toString-Methode mit der Jahresausgabe
	@Override
	public String toString() {
		return String.valueOf(year);
	}

	// Getter/Setter Methoden
	public void setYear(int year) {
		this.year = year;
	}

	public int getYear() {
		return year;
	}

}

Month:

Java:
package edu.hm.cs.swe2.exceptions.datecomponents;

import java.io.*;
import java.util.*;

import edu.hm.cs.swe2.exceptions.exceptionclasses.IllegalMonthException;
import edu.hm.cs.swe2.exceptions.exceptionclasses.IsFutureDateException;


public class Month {

	// Klassenattribut
	private int month;

	// Costum- und Default-Konstruktor
	public Month(int month) {
		this.month = month;
	}

	public Month() {

	}

	// Methode, welche für die Eingabe eines Monats, welche
	// anschließend auf Richtigkeit überprüft wird, benötigt wird
	public void requestBirthMonth(Calendar rightNow, int birthYear)
			throws NumberFormatException, IllegalMonthException,
			IsFutureDateException {

		int i = 0;

		while (i < 1 || i > 12) {
			try {
				BufferedReader input = new BufferedReader(
						new InputStreamReader(System.in));
				System.out.println("Bitte gib dein Geburtsmonat ein:");
				i = Integer.parseInt(input.readLine());

				if (i < 1 || i > 12)
					throw new IllegalMonthException(
							"Wert für Monat muss zwischen 1 und 12 liegen.");
				month = i;

				if (birthYear == rightNow.get(Calendar.YEAR)) {
					if (month > rightNow.get(Calendar.MONTH))
						throw new IsFutureDateException(
								"Eingegebenes Jahr liegt in der Zukunft, kann nicht dein Geburtsjahr sein!");
				}

			} catch (IllegalMonthException e) {
				e.printStackTrace(System.out);
			} catch (IsFutureDateException e) {
				e.printStackTrace(System.out);
			} catch (NumberFormatException e) {
				System.out.println("Ouch, das war keine Zahl!!!");
				e.printStackTrace(System.out);
			} catch (IOException e) {
				e.printStackTrace(System.out);
			}
		}
	}

	// Überschriebene toString-Methode mit der Monatsausgabe
	@Override
	public String toString() {
		return String.valueOf(month);
	}

	// Getter/Setter - Methoden
	public void setMonth(int month) {
		this.month = month;
	}

	public int getMonth() {
		return month;
	}

Day:

Java:
package edu.hm.cs.swe2.exceptions.datecomponents;

import java.io.*;
import java.util.Calendar;

import edu.hm.cs.swe2.exceptions.exceptionclasses.IllegalDayException;
import edu.hm.cs.swe2.exceptions.exceptionclasses.IsFutureDateException;

public class Day {

	// Klassenattribut
	private int day;

	
	// Costum- und Default-Konstruktor 
	public Day(int day) {
		this.day = day;
	}

	public Day() {

	}
	
	
	// Methode, welche für die Eingabe eines Tages, welche
	// anschließend auf Richtigkeit überprüft wird, benötigt wird
	public void requestBirthDay(Calendar rightNow, int birthYear, int birthMonth) throws NumberFormatException,
		IllegalDayException, IsFutureDateException {

		int i = 0;

		while (i < 1 || i > 31){
			try {
				BufferedReader input = new BufferedReader(
						new InputStreamReader(System.in));
				System.out.println("Bitte gib dein Geburtstag ein:");
				i = Integer.parseInt(input.readLine());
				
				if (i < 1 || i > 31)
					throw new IllegalDayException(
							"Wert für Monat muss zwischen 1 und 31 liegen.");
				day = i ;
				
				
				if (birthYear == rightNow.get(Calendar.YEAR) && birthMonth == rightNow.get(Calendar.MONTH))
				{
					if (day > rightNow.get(Calendar.DAY_OF_MONTH))
					{
						throw new IsFutureDateException("Eingegebenes Jahr liegt in der Zukunft, kann nicht dein Geburtsjahr sein!");
					}
				}
				
				
				//if jahr und monat gleich
				// if tag > aktueller tag
				

			} catch (NumberFormatException e) {
				System.out.println("Ouch, das war keine Zahl!!!");
				e.printStackTrace(System.out);
			} catch (IOException e) {
				e.printStackTrace(System.out);
			} catch (IllegalDayException e) {
				  e.printStackTrace(System.out);
			} catch (IsFutureDateException e)
			{
				e.printStackTrace(System.out);
			}
		}
	}
	
	// Überschriebene toString-Methode mit der Tagesausgabe
	@Override
	public String toString() {
		return String.valueOf(day);
	}
	
	// statische Methode, die einen Wahrheitswertzurücklieft;
	// sie wird benötigt, um den Monaten unterschiedlich lange Tage zuzuweisen,
	// sowie Schaltjahre und die Tage des Februars (28,29) zu berücksichtigen
	public static boolean validateDay(int birthDay, int birthMonth,	int birthYear) throws IllegalDayException {
		boolean isValid = true;
		int daysInMonth = 0;

		switch (birthMonth) {
		case 1:
			daysInMonth = 31;
			break;
		case 2:
			if (birthYear % 4 == 0) {
				daysInMonth = 29;
			} else {
				daysInMonth = 28;
			}
			break;
		case 3:
			daysInMonth = 31;
			break;
		case 4:
			daysInMonth = 30;
			break;
		case 5:
			daysInMonth = 31;
			break;
		case 6:
			daysInMonth = 30;
			break;
		case 7:
			daysInMonth = 31;
			break;
		case 8:
			daysInMonth = 31;
			break;
		case 9:
			daysInMonth = 30;
			break;
		case 10:
			daysInMonth = 31;
			break;
		case 11:
			daysInMonth = 30;
			break;
		case 12:
			daysInMonth = 31;
			break;
		}

		if (birthDay > daysInMonth)
		{
			isValid = false;
		}
		

		
		return isValid;

	}

	// Getter/Setter - Methoden
	public void setDay(int day) {
		this.day = day;
	}

	public int getDay() {
		return day;
	}

}

Ausnahmeklassen:

Java:
package edu.hm.cs.swe2.exceptions.exceptionclasses;


public class IllegalMonthException extends Exception {

	
	// Default-Konstruktor 
	public IllegalMonthException()
	{
				
	}
	
	// Konstruktor der Ausnahmeklasse mit einem Stringparameter
	public IllegalMonthException(String message)
	{
		super(message);	
	}  

	
	
}

Java:
package edu.hm.cs.swe2.exceptions.exceptionclasses;


public class IllegalDayException extends Exception {

	// Default-Konstruktor 
	public IllegalDayException()
	{
				
	}
	
	// Konstruktor der Ausnahmeklasse mit einem Stringparameter
	public IllegalDayException(String message)
	{
		super(message);	
	}  

}

Java:
package edu.hm.cs.swe2.exceptions.exceptionclasses;


public class IsFutureDateException extends Exception {

	// Default-Konstruktor 
	public IsFutureDateException()
	{
				
	}
	
	// Konstruktor der Ausnahmeklasse mit einem Stringparameter
	public IsFutureDateException(String message)
	{
		super(message);	
	}  

	
	
}


Hoffe einer von euch findet da was

mfg
 
Zuletzt bearbeitet von einem Moderator:
Java:
                i = Integer.parseInt(input.readLine());
 
                if (year > rightNow.get(Calendar.YEAR)) {
                    throw new IsFutureDateException(
                            "Eingegebenes Jahr liegt in der Zukunft, kann nicht dein Geburtsjahr sein!");
                }
 
                year = i;

year ist zum zeitpunkt der prüfung noch 0
 

Zurück
Oben