Guest User

Untitled

a guest
May 20th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.95 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Menue {
  5.  
  6.     // public static void printLeapyear(int year) {
  7.     // for (int i = 0; i <= year; i++) {
  8.     // System.out.println(i + ": " + isLeapyear(i));
  9.     // }
  10.     // }
  11.  
  12.     private static Calendar calendar = new Calendar();
  13.  
  14.     public static int[] parseInput() {// Hier wird das Datum eingelesen.
  15.  
  16.         while (true) {
  17.             System.out.println("Bitte geben Sie das Datum ein (dd.mm.yyyy):");
  18.             Scanner scan = new Scanner(System.in);
  19.             String scanned = scan.nextLine();
  20.             String[] splitted = scanned.split("\\.");
  21.             System.out.println(Arrays.toString(splitted));
  22.             if (splitted.length == 3) {
  23.                 try {
  24.                     int day = Integer.parseInt(splitted[0]);
  25.                     int month = Integer.parseInt(splitted[1]);
  26.                     int year = Integer.parseInt(splitted[2]);
  27.  
  28.                     if (isValidDate(day, month, year)) {
  29.                         int[] dateArray = { day, month, year };
  30.                         return dateArray;
  31.                     } else {
  32.                         System.out.println("Ungültiges Datum");
  33.                         System.out.println();
  34.                     }
  35.                 } catch (NumberFormatException e) {
  36.                     System.out
  37.                             .println("Tut mir Leid, das war kein lesbares Datum");
  38.                     System.out.println();
  39.                 }
  40.             } else {
  41.                 System.out.println("Tut mir Leid, das war kein lesbares Datum");
  42.                 System.out.println();
  43.             }
  44.         }
  45.  
  46.     }
  47.  
  48.     private static String addDateName() {// Hier wird der Name des Termins
  49.                                             // eingelesen
  50.         System.out.println("Bitte Terminnamen eingeben:");
  51.         Scanner scan = new Scanner(System.in);
  52.         String name = scan.nextLine();
  53.  
  54.         if (name.isEmpty()) {
  55.             return addDateName();
  56.         } else {
  57.  
  58.             System.out.println(name);
  59.             return name;
  60.         }
  61.  
  62.     }
  63.  
  64.     private static int[] addDateTime() {// Hier wird die Zeit des Termins
  65.                                         // eingelesen
  66.         System.out.println("Bitte Uhrzeit eingeben(hh:mm):");
  67.         Scanner scan = new Scanner(System.in);
  68.         String scannedTime = scan.nextLine();
  69.         String[] splitted = scannedTime.split("\\:");
  70.         if (splitted.length == 2){
  71.         try {
  72.             int hours = Integer.parseInt(splitted[0]);
  73.             int minutes = Integer.parseInt(splitted[1]);
  74.             if (isValidTime(hours, minutes)) {
  75.                 System.out.println("Zeit: " + hours + ":" + minutes + " Uhr");
  76.                 int[] timeArray = { hours, minutes };
  77.                 return timeArray;
  78.             } else {
  79.                 System.out.println("Fehler");
  80.                 return addDateTime();
  81.             }
  82.  
  83.         } catch (NumberFormatException e) {
  84.             System.out.println("Ungültige Zeitangabe!");
  85.             return addDateTime();
  86.         }
  87.         }
  88.         else {
  89.             System.out.println("Ungültige Zeitangabe!");
  90.             return addDateTime();
  91.         }
  92.  
  93.     }
  94.  
  95.     private static String addDateDescription() {
  96.         System.out.println("Bitte Beschreibung eingeben:");
  97.         Scanner scan = new Scanner(System.in);
  98.         String description = scan.nextLine();
  99.         if (description.isEmpty()) {
  100.             return addDateDescription();
  101.         } else {
  102.             System.out.println("Beschreibung: " + description);
  103.             return description;
  104.         }
  105.     }
  106.  
  107.     public static boolean isValidTime(int hours, int minutes) {
  108.         if ((hours < 24) && (hours >= 0) && (minutes < 60) && (minutes >= 0)) {
  109.             return true;
  110.         } else {
  111.             return false;
  112.         }
  113.     }
  114.  
  115.     public static boolean isLeapyear(int year) {
  116.         if ((year % 4) == 0) {
  117.             if ((year % 100) == 0) {
  118.                 if ((year % 400) == 0) {
  119.                     return true;
  120.                 } else {
  121.                     return false;
  122.                 }
  123.             } else {
  124.                 return true;
  125.             }
  126.         } else {
  127.             return false;
  128.         }
  129.     }
  130.  
  131.     public static boolean isValidDate(int day, int month, int year) {
  132.  
  133.         if (month > 0 && month < 13) {
  134.             switch (month) {
  135.             case 1:
  136.                 return ((day <= 31) && (day > 0));
  137.  
  138.             case 2:
  139.                 if ((isLeapyear(year))) {
  140.                     return ((day <= 29) && (day > 0));
  141.  
  142.                 } else {
  143.                     return ((day <= 28) && (day > 0));
  144.                 }
  145.             case 3:
  146.                 return ((day <= 31) && (day > 0));
  147.  
  148.             case 4:
  149.                 return ((day <= 30) && (day > 0));
  150.  
  151.             case 5:
  152.                 return ((day <= 31) && (day > 0));
  153.  
  154.             case 6:
  155.                 return ((day <= 30) && (day > 0));
  156.  
  157.             case 7:
  158.                 return ((day <= 31) && (day > 0));
  159.  
  160.             case 8:
  161.                 return ((day <= 31) && (day > 0));
  162.  
  163.             case 9:
  164.                 return ((day <= 30) && (day > 0));
  165.  
  166.             case 10:
  167.                 return ((day <= 31) && (day > 0));
  168.  
  169.             case 11:
  170.                 return ((day <= 30) && (day > 0));
  171.  
  172.             case 12:
  173.                 return ((day <= 31) && (day > 0));
  174.  
  175.             default:
  176.                 return false;
  177.  
  178.             }
  179.         } else {
  180.             return false;
  181.         }
  182.  
  183.     }
  184.  
  185.     public static void addDate() {
  186.         Date date = new Date(parseInput(), addDateTime(), addDateName(),
  187.                 addDateDescription());
  188.         calendar.addDate(date);
  189.  
  190.     }
  191.  
  192.     public void editDate() {
  193.         calendar.printCalendar();
  194.         System.out.println("Bitte geben Sie die Terminnummer des zu ändernden Termins ein!");
  195.         Scanner scan = new Scanner(System.in);
  196.         int scanned = scan.nextInt();
  197.  
  198.         if (scan.hasNextInt()) {
  199.             Date date = calendar.getDate(scanned);
  200.             scanNewDate();
  201.             scanNewTime();
  202.             scanNewName();
  203.             scanNewDescription();
  204.         } else {
  205.             System.out.println("Bitte die Terminnummer eingeben!");
  206.             editDate();
  207.         }
  208.  
  209.     }
  210.  
  211.     public int[] scanNewDate() {
  212.         System.out.println("Bitte geben Sie das neue Datum ein (dd.mm.yyyy):");
  213.         Scanner scan = new Scanner(System.in);
  214.         String scanned = scan.nextLine();
  215.         String[] splitted = scanned.split("\\.");
  216.         System.out.println(Arrays.toString(splitted));
  217.         if (splitted.length == 3) {
  218.             try {
  219.                 int newDay = Integer.parseInt(splitted[0]);
  220.                 int newMonth = Integer.parseInt(splitted[1]);
  221.                 int newYear = Integer.parseInt(splitted[2]);
  222.  
  223.                 if (isValidDate(newDay, newMonth, newYear)) {
  224.                     int[] newDateArray = { newDay, newMonth, newYear };
  225.                     Date.setDay(newDay);
  226.                 } else {
  227.                     System.out.println("Ungültiges Datum");
  228.                     System.out.println();
  229.                     return scanNewDate();
  230.                 }
  231.             } catch (NumberFormatException e) {
  232.                 System.out.println("Tut mir Leid, das war kein lesbares Datum");
  233.                 System.out.println();
  234.                 return scanNewDate();
  235.             }
  236.         } else {
  237.             System.out.println("Tut mir Leid, das war kein lesbares Datum");
  238.             System.out.println();
  239.             return scanNewDate();
  240.         }
  241.     }
  242.  
  243.     private static String scanNewName() {// Hier wird der neue Name des Termins
  244.                                             // eingelesen
  245.         System.out.println("Bitte neuen Terminnamen eingeben:");
  246.         Scanner scan = new Scanner(System.in);
  247.         String newName = scan.nextLine();
  248.  
  249.         if (newName.length() > 0) {
  250.             System.out.println(newName);
  251.             return newName;
  252.         } else {
  253.             return scanNewName();
  254.         }
  255.  
  256.     }
  257.  
  258.     private static int[] scanNewTime() {// Hier wird die neue Zeit des Termins
  259.                                         // eingelesen
  260.         System.out.println("Bitte Uhrzeit eingeben(hh:mm):");
  261.         Scanner scan = new Scanner(System.in);
  262.         String scannedTime = scan.nextLine();
  263.         String[] splitted = scannedTime.split("\\:");
  264.         if (splitted.length == 2){
  265.         try {
  266.             int newHours = Integer.parseInt(splitted[0]);
  267.             int newMinutes = Integer.parseInt(splitted[1]);
  268.             if (isValidTime(newHours, newMinutes)) {
  269.                 System.out.println("Zeit: " + newHours + ":" + newMinutes + " Uhr");
  270.                 int[] timeArray = { newHours, newMinutes };
  271.                 return timeArray;
  272.             } else {
  273.                 System.out.println("Fehler");
  274.                 return addDateTime();
  275.             }
  276.  
  277.         } catch (NumberFormatException e) {
  278.             System.out.println("Ungültige Zeitangabe!");
  279.             return addDateTime();
  280.         }
  281.         }
  282.         else {
  283.             System.out.println("Ungültige Zeitangabe!");
  284.             return addDateTime();
  285.         }
  286.  
  287.     }
  288.  
  289.     private static String scanNewDescription() {
  290.         System.out.println("Bitte Beschreibung eingeben:");
  291.         Scanner scan = new Scanner(System.in);
  292.         String newDescription = scan.nextLine();
  293.         if (newDescription.isEmpty()) {
  294.             return scanNewDescription();
  295.         } else {
  296.             System.out.println("Beschreibung: " + newDescription);
  297.             return newDescription;
  298.         }
  299.     }
  300.  
  301.     public static void main(String[] args) {
  302.  
  303.         for (int i = 1; i < 8; i++) {
  304.             int[] dateArray = { 12, 1, 2012 };
  305.             int[] timeArray = { 8, 00 };
  306.             String name = "Besprechung " + i;
  307.             String description = "Treffen mit Senfgesicht Nummer " + i;
  308.             Date date = new Date(dateArray, timeArray, name, description);
  309.             calendar.addDate(date);
  310.  
  311.         }
  312.  
  313.         boolean running = true;
  314.         while (running) {
  315.  
  316.             System.out.println("Was möchten Sie machen?");
  317.             System.out.println();
  318.             System.out.println("1. Termine anzeigen");
  319.             System.out.println("2. Termin hinzufügen");
  320.             System.out.println("3. Termine ändern");
  321.             System.out.println("4. Programm beenden");
  322.             Scanner scan = new Scanner(System.in);
  323.             String scanned = scan.nextLine();
  324.             int scannedInt = 0;
  325.             try {
  326.                 scannedInt = Integer.parseInt(scanned);
  327.             } catch (NumberFormatException e) {
  328.                 System.out.println("Bitte eine Zahl eingeben!");
  329.                 System.out.println();
  330.             }
  331.  
  332.             switch (scannedInt) {
  333.             case 1:
  334.                 calendar.printCalendar();
  335.                 System.out.println();
  336.                 break;
  337.             case 2:
  338.                 addDate();
  339.                 break;
  340.             case 3:
  341.                 System.out.println("Punkt 3");
  342.                 System.out.println();
  343.  
  344.                 break;
  345.             case 4:
  346.                 System.out.println("Programm beendet");
  347.                 running = false;
  348.                 break;
  349.  
  350.             default:
  351.                 System.out
  352.                         .println("Bitte einen der oben genannten Menüpunkte eingeben!");
  353.                 System.out.println();
  354.                 break;
  355.  
  356.             }
  357.         }
  358.     }
  359. }
Add Comment
Please, Sign In to add comment