Last active
August 29, 2015 14:07
-
-
Save OlgaKulikova/fcb00868f4f7d1a1e8d0 to your computer and use it in GitHub Desktop.
Проект Студент
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; | |
// Модифицировать проект «Список студентов» так, чтобы | |
// 1) список вводился с клавиатуры | |
// 2) была возможность удалять студента по номеру | |
// 3) при введении неправильных данных (пустое имя, неправильная дата) программа кидала исключение | |
// и обрабатывала его с выводом соотв. сообщений на экран. | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Scanner; | |
public class MyClass { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd"); | |
StudentList sl = new StudentList(); | |
System.out.println("Заполните данные студентов."); | |
System.out.println("Для создания нового студента введите \"Enter\""); | |
System.out.println("Для завершения coздания списка введите \"Создать\""); | |
while (! scan.nextLine().equals("Создать")) { | |
System.out.println("Имя"); | |
String name = scan.nextLine(); | |
while (true) { | |
if (name.length() == 0) { | |
System.out.println("Вы не ввели имя! Введите снова!"); | |
name = scan.nextLine(); | |
} else { | |
break; | |
} | |
} | |
System.out.println("Фамилия"); | |
String surname = scan.nextLine(); | |
while (true) { | |
if (surname.length() == 0) { | |
System.out.println("Вы не ввели фамилию! Введите снова!"); | |
surname = scan.nextLine(); | |
} else { | |
break; | |
} | |
} | |
while (true) { | |
try { | |
System.out.println("Дата рождения в формате гггг.мм.дд"); | |
String birth = scan.nextLine(); | |
Date birthday = sdf.parse(birth); | |
sl.add(new Student(name, surname, birthday)); | |
break; | |
} catch (ParseException e) { | |
System.out.println("Неверная дата"); | |
} | |
} | |
} | |
System.out.println("Введите имя, чтобы найти дату рождения"); | |
int n = sl.find(scan.nextLine()); | |
if (n == -1) { | |
System.out.println("Неверные данные!"); | |
} else { | |
System.out.println(sl.get(n).getBirth().toString()); | |
} | |
System.out.println("Введите фамилию, чтобы найти имя"); | |
int sn = sl.findSurname(scan.nextLine()); | |
if (n == -1) { | |
System.out.println("Неверные данные!"); | |
} else { | |
System.out.println(sl.get(sn).getName()); | |
} | |
try { | |
System.out.println("Введите дату рождения, чтобы найти имя и фамилию"); | |
int db = sl.findBirth(sdf.parse(scan.nextLine())); | |
System.out.println(sl.get(db).getName() + " " + sl.get(db).getSurname()); | |
} catch (ParseException e) { | |
System.out.println("Неверная дата"); | |
} | |
sl.printList(); | |
System.out.println("Введите порядковый номер, чтобы удалить из списка"); | |
String num = scan.nextLine(); | |
if (num.length() == 0) { | |
System.out.println("Неверные данные!"); | |
} else { | |
sl.deleteNumber(Integer.parseInt(num)); | |
} | |
sl.printList(); | |
} | |
} |
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; | |
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.util.Date; | |
public class StudentList { | |
private Student[] list = new Student[100]; | |
private int p = 0; | |
public void add(Student s) { | |
if (p == list.length - 1) { | |
Student[] t = new Student[list.length * 2]; | |
for (int i = 0; i < list.length; i++) { | |
t[i] = list[i]; | |
} | |
list = t; | |
} | |
list[p++] = s; | |
} | |
public Student get(int n) { | |
return list[n]; | |
} | |
public int find(String name) { | |
for (int i = 0; i < p; i++) { | |
if (list[i].getName().equalsIgnoreCase(name)) | |
return i; | |
} | |
return -1; | |
} | |
public int findSurname(String surname) { | |
for (int i = 0; i < p; i++) { | |
if (list[i].getSurname().equalsIgnoreCase(surname)) | |
return i; | |
} | |
return -1; | |
} | |
public int findBirth(Date birth) { | |
for (int i = 0; i < p; i++) { | |
if (list[i].getBirth().equals(birth)) | |
return i; | |
} | |
return -1; | |
} | |
public void deleteNumber(int n) { | |
n--; | |
if (n < 0 || n > list.length) { | |
System.out.println("Неверные данные!"); | |
} else { | |
Student[] t = new Student[list.length - 1]; | |
int k = 0; | |
for (int i = 0; i < n; i++) { | |
t[k++] = list[i]; | |
} | |
for (int i = n + 1; i < list.length; i++) { | |
t[k++] = list[i]; | |
} | |
list = t; | |
} | |
} | |
public void printList() { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < list.length; i++) { | |
if (list[i] != null) { | |
sb.append(list[i].getName()); | |
sb.append(" "); | |
sb.append(list[i].getSurname()); | |
sb.append(" "); | |
sb.append(list[i].getBirth()); | |
sb.append("\n"); | |
} | |
} | |
System.out.println(sb.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment