Last active
January 31, 2016 20:15
-
-
Save Viacheslav77/e01f008e6e333b1a47d2 to your computer and use it in GitHub Desktop.
Список студентов 1) список должен вводиться с клавиатуры 2) должна быть возможность удалять студента по номеру 3) при введении неправильных данных программа должна кидать исключение и обрабатывала его с выводом соотв. сообщений на экраню
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Student; | |
| import java.text.ParseException; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| import java.util.Scanner; | |
| import Student.Inpt.NumeralExceptions; | |
| import Student.Inpt.TextExceptions; | |
| public class Inpt { | |
| public static class InputStrNum { | |
| private String io; | |
| Scanner s = new Scanner(System.in); | |
| public String getIo(){ | |
| return io; | |
| } | |
| public String getName(String txt){ | |
| io=null; | |
| while(io==null){ | |
| System.out.print("Введите " + txt + " студента: "); | |
| try { | |
| setName(); | |
| } catch (TextExceptions e) { | |
| System.out.println (e.getMessage()); | |
| } | |
| } | |
| return io; | |
| } | |
| public Date getDate(){ | |
| SimpleDateFormat sdf =new SimpleDateFormat("d.mm.yyyy"); | |
| Date dt = null; | |
| io=null; | |
| while(io==null){ | |
| System.out.print("Введите дату рождения в формате d.mm.yyyy "); | |
| try { | |
| setDate(); | |
| } catch (NumeralExceptions e) { | |
| System.out.println(e.getMessage()); | |
| } | |
| try{ | |
| dt=sdf.parse(io); | |
| } catch(ParseException e){ | |
| io = null; | |
| System.out.println("Неправильный формат ввода даты :)"); | |
| } | |
| } | |
| return dt; | |
| } | |
| public String setDate() throws NumeralExceptions{ | |
| io = s.next(); | |
| StringBuilder sb = new StringBuilder(io); | |
| for(int i = 0; i < sb.length(); i++){ | |
| if (sb.charAt(i)>=97&&sb.charAt(i)<=122){ | |
| throw new NumeralExceptions (" Введите цифры в нужном формате :)"); | |
| } | |
| } | |
| return io; | |
| } | |
| public void setName() throws TextExceptions { | |
| io = s.next(); | |
| StringBuilder sb = new StringBuilder(io); | |
| for(int i = 0; i < sb.length(); i++){ | |
| StringBuilder sb1 = new StringBuilder ("-0123456789"); | |
| for(int j = 0; j < sb1.length(); j++){ | |
| if (sb.charAt(i)==sb1.charAt(j)){ | |
| io = null; | |
| throw new TextExceptions (" Введите буквы :)"); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| public static class TextExceptions extends Exception { | |
| public TextExceptions (String message){ | |
| super(message); | |
| } | |
| @Override | |
| public String getMessage(){ | |
| return "TextExceptions" + super.getMessage(); | |
| } | |
| } | |
| public static class NumeralExceptions extends Exception { | |
| public NumeralExceptions (String message){ | |
| super(message); | |
| } | |
| @Override | |
| public String getMessage(){ | |
| return "NumeralExceptions" + super.getMessage(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Student; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| import Student.Inpt.InputStrNum; | |
| import Student.Inpt.NumeralExceptions; | |
| import Student.Inpt.TextExceptions; | |
| /*Модифицировать проект «Список студентов» так, чтобы | |
| * 1) список вводился с клавиатуры | |
| * 2) была возможность удалять студента по номеру | |
| * 3) при введении неправильных данных (пустое имя, неправильная дата) | |
| * программа кидала исключение и обрабатывала его с выводом соотв. сообщений на экран.*/ | |
| public class MyClass { | |
| public static void main(String[] args) { | |
| StudentList sl = new StudentList(); | |
| InputStrNum inp = new InputStrNum(); | |
| while (inp.getName("(y) eсли Вы хотите ввести нового студента \nили Вы можете воспользоваться созданным списком введя любую букву, \nбез ввода нового").equals("y")) { | |
| sl.add(new Student(inp.getName("имя"), inp.getName("фамилию"), inp.getDate())); | |
| } | |
| base(sl); | |
| prn(sl); | |
| System.out.println("\nУдаляем студента Svetta"); | |
| int n = sl.find("Svetta"); | |
| System.out.println("Ищем в списке имя. \nОно находится под номером :" + (n+1) + "\n Удаляем ...\n----------------------------------"); | |
| sl.del(n); | |
| prn(sl); | |
| } | |
| public static void prn(StudentList sl){ | |
| SimpleDateFormat stf =new SimpleDateFormat("d.MM.yyyy"); | |
| System.out.println("Список студентов:"); | |
| System.out.println("----------------------------------"); | |
| for(int i = 0; i< (sl.getListLength()-1); i++){ | |
| System.out.println((i+1) | |
| + ". " + sl.get(i).getName() | |
| + " " + sl.get(i).getSurname() | |
| + " " + stf.format(sl.get(i).getBirth()) ); | |
| } | |
| } | |
| public static void base(StudentList sl){ | |
| sl.add(new Student("Taras ", "Necaynoy", new Date(86, 01, 11))); | |
| sl.add(new Student("Vasyga", "Pupkinyo" , new Date(70, 3, 28))); | |
| sl.add(new Student("Nastya", "Supkinto", new Date(70, 3, 28))); | |
| sl.add(new Student("Svetta", "Nagttnko", new Date(86, (1-1),11))); | |
| sl.add(new Student("Vattsa", "Pupkttin" , new Date(70, 3, 28))); | |
| sl.add(new Student("Nastya", "Suttpkin", new Date(70, 3, 28))); | |
| sl.add(new Student("Ndstya", "Subtpkin", new Date(70, 3, 28))); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Student; | |
| import java.util.Date; | |
| public class Student { | |
| private String name = null; | |
| private String surname; | |
| private Date birth; | |
| public Student(String name, String surname, Date birth) { | |
| this.name = name; | |
| this.surname = surname; | |
| this.birth = birth; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public String getSurname() { | |
| return surname; | |
| } | |
| public void setSurname(String surname) { | |
| this.surname = surname; | |
| } | |
| public Date getBirth() { | |
| return birth; | |
| } | |
| public void setBirth(Date birth) { | |
| this.birth = birth; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Student; | |
| import java.lang.reflect.Array; | |
| public class StudentList { | |
| private Student[] list = new Student[1]; | |
| private int p = 0; | |
| public void add(Student s) { | |
| if(p == list.length){ | |
| Student[] tmp = new Student[list.length*2]; | |
| System.arraycopy(list, 0, tmp, 0, list.length); | |
| list=tmp; | |
| } | |
| list[p++] = s; | |
| } | |
| public Student get(int n) { | |
| return list[n]; | |
| } | |
| public int getListLength() { | |
| return list.length; | |
| } | |
| public int find(String name) { | |
| for (int i = 0; i < list.length-1; i++) { | |
| if (list[i].getName().equalsIgnoreCase(name)){ | |
| //System.out.print(list[i].getName()+" "); | |
| return i; | |
| } | |
| } | |
| return -1; | |
| } | |
| public void del(int n){ | |
| //list[n] = null; | |
| if(n<0 || n>list.length){ | |
| return; | |
| } | |
| Student [] tmp = new Student [list.length-1]; | |
| System.arraycopy(list, 0, tmp, 0, n); | |
| System.arraycopy(list, n+1, tmp, n, tmp.length-n); | |
| list=tmp; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment