java HW 5.3-5.4
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java HW 5.3-5.4相关的知识,希望对你有一定的参考价值。
package com.gmail.vhrushyn;
public interface WoenCom {
Student[] getRecruits();
}
package com.gmail.vhrushyn;
public class TooMuchStudentsException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String getMessage() {
return "ATTENTION! The group is full of students: imposible to add any more student to the group."+System.lineSeparator();
}
}
package com.gmail.vhrushyn;
public class Student extends Person {
private static int i = 0;
private int id;
private boolean scholarship;
public Student(String firstName, String lastName, int age, boolean sex, boolean scholarship) {
super(firstName, lastName, age, sex);
this.scholarship = scholarship;
id = i + 1;
i = i + 1;
}
public Student() {
super();
id = i + 1;
i = i + 1;
}
public boolean isScholarship() {
return scholarship;
}
public void setScholarship(boolean scholarship) {
this.scholarship = scholarship;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
String sScholarship = (scholarship == true) ? "yes" : "no";
return "Student id# " + id + ", " + super.toString() + ", scholarship: " + sScholarship + System.lineSeparator()
+ " ";
}
}
package com.gmail.vhrushyn;
public interface SaverGroup {
public void save ();
public Groupp load ();
}
package com.gmail.vhrushyn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class SaveGroupToCSV implements SaverGroup {
private Groupp group;
private File file;
public SaveGroupToCSV(Groupp group, File file) {
super();
this.group = group;
this.file = file;
}
public SaveGroupToCSV(File file) {
super();
this.file = file;
}
public SaveGroupToCSV(Groupp group) {
super();
this.group = group;
}
public SaveGroupToCSV() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void save() {
File file = this.file;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println(e);
}
}
Student[] students = group.getGroupp();
try (PrintWriter pw = new PrintWriter(file)) {
pw.println(group.getGrouppName());
for (int i = 0; i < students.length; i++) {
if (students[i] != null) {
pw.println(students[i].getFirstName() + ";" + students[i].getLastName() + ";" + students[i].getAge()
+ ";" + students[i].isSex() + ";" + students[i].isSex() + ";" + students[i].getId());
}
}
pw.println("end");
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
@Override
public Groupp load() {
Groupp loadedGroup = new Groupp();
try (BufferedReader br = new BufferedReader(new FileReader(this.file))) {
String groupName = br.readLine();
loadedGroup.setGrouppName(groupName);
String[] studentText = new String[6];
while (true) {
studentText = br.readLine().split(";");
if (studentText[0].equals("end")) {
break;
} else {
Student student = new Student(studentText[0], studentText[1], Integer.parseInt(studentText[2]), Boolean.valueOf(studentText[3]),
Boolean.valueOf(studentText[4]));
student.setId(Integer.parseInt(studentText[5]));
loadedGroup.addStudent(student);
}
}
} catch (IOException e) {
System.out.println(e);
}
return loadedGroup;
}
}
package com.gmail.vhrushyn;
public class Person {
private String firstName;
private String lastName;
private int age;
private boolean sex;
public Person(String firstName, String lastName, int age, boolean sex) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.sex = sex;
}
public Person() {
super();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public String toString() {
String sSex = (sex == true) ? "male" : "female";
return "last name: " + lastName + ", first name: " + firstName + ", age: " + age + ", sex: " + sSex + "";
}
}
package com.gmail.vhrushyn;
import java.io.File;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Groupp progGroupp = new Groupp("JavaOOP");
progGroupp.addStudent(new Student("Joe", "Dreeman", 16, true, true));
progGroupp.addStudent(new Student("Jack", "Daniels", 17, true, false));
progGroupp.addStudent(new Student("Vicki", "Springs", 18, false, true));
progGroupp.addStudent(new Student("Angella", "Totty", 19, false, true));
progGroupp.addStudent(new Student("Joee", "Gods", 20, true, false));
progGroupp.addStudent(new Student("Rob", "Key", 21, true, false));
progGroupp.addStudent(new Student("Bob", "Alley", 22, true, true));
progGroupp.addStudent(new Student("Tina", "Yankee", 23, false, true));
progGroupp.addStudent(new Student("Trisha", "Zoon", 24, false, true));
System.out.println(System.lineSeparator());
/*
// progGroupp.addStudentManually();
System.out.println(System.lineSeparator());
System.out.println(progGroupp);
// WoenCom w = progGroupp;
// System.out.println("Recruits: "+Arrays.toString(w.getRecruits()));
System.out.println(progGroupp);
progGroupp.sortGroup("Id");
System.out.println(progGroupp);
progGroupp.sortGroup("FirstName");
System.out.println(progGroupp);
progGroupp.sortGroup("Age");
System.out.println(progGroupp);
*/
progGroupp.sortGroup("LastName");
// System.out.println(progGroupp);
System.out.println("...save and load group...");
File file = new File("groupOne.csv");
SaveGroupToCSV sgt = new SaveGroupToCSV(progGroupp, file);
sgt.save();
System.out.println(sgt.load());
}
}
package com.gmail.vhrushyn;
import java.io.File;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Groupp implements WoenCom {
private String grouppName;
private Student[] groupp = new Student[10];
public Groupp(String grouppName) {
super();
this.grouppName = grouppName;
}
public Groupp() {
super();
}
public String getGrouppName() {
return grouppName;
}
public void setGrouppName(String grouppName) {
this.grouppName = grouppName;
}
public Student[] getGroupp() {
return groupp;
}
public void addStudent(Student student) {
try {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
groupp[i] = student;
flag = true;
System.out.println("Student " + groupp[i].getLastName() + " has been added to the group.");
break;
}
}
if (!flag)
throw new TooMuchStudentsException();
} catch (TooMuchStudentsException e) {
System.out.println(e.getMessage());
}
}
public void addStudentManually() {
Scanner sc = new Scanner(System.in);
try {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
flag = true;
System.out.println("You can add student to the group.");
System.out.println("Enter student's first name");
String firstName = sc.nextLine();
System.out.println("Enter student's last name");
String lastName = sc.nextLine();
System.out.println("Enter student's age");
int age = 0;
try {
age = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("You didn't type integer, student's age saved as '0'");
}
boolean sex = false;
while (true) {
String str = sc.nextLine(); // read to nowhere
System.out.println("If student is male - type 'yes', else - 'no'");
String sexS = sc.nextLine();
if (sexS.equalsIgnoreCase("yes")) {
sex = true;
break;
} else if (sexS.equalsIgnoreCase("no")) {
break;
} else {
System.out.println("You typed wrong word, try again");
}
}
boolean scholarship = false;
while (true) {
// String str = sc.nextLine(); // read to nowhere
System.out.println("If student has scholarship - type 'yes', else - 'no'");
String schS = sc.nextLine();
if (schS.equalsIgnoreCase("yes")) {
scholarship = true;
break;
} else if (schS.equalsIgnoreCase("no")) {
break;
} else {
System.out.println("You typed wrong word, try again");
}
}
System.out.println("Student's id will be defined automatically");
groupp[i] = new Student(firstName, lastName, age, sex, scholarship);
System.out.println("New student " + lastName + " has been addad to the group");
break;
}
}
sc.close();
if (!flag)
throw new TooMuchStudentsException();
} catch (TooMuchStudentsException e) {
System.out.println(e.getMessage());
}
}
public void dellStudent(String lastName) {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
} else {
if (lastName.equals(groupp[i].getLastName())) {
groupp[i] = null;
System.out.println("Student last name '" + lastName + "' has been deleted");
flag = true;
}
}
}
if (!flag) {
System.out.println("There is no such student in the group to delete");
}
}
public void findStudent(String lastName) {
boolean flag = false;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] == null) {
i++;
} else {
if (lastName.equals(groupp[i].getLastName())) {
System.out.println("Student found according to last name '" + lastName + "': "
+ System.lineSeparator() + groupp[i]);
flag = true;
}
}
}
if (!flag) {
System.out.println("There is no such student in the group");
}
}
private void sortGroup(Student[] group) {
while (true) {
int c = 0;
for (int i = 0; i < group.length - 1; i++) {
int t = 0;
if (group[i] == null && group[i + 1] != null) {
t = 1;
}
if (group[i] != null && group[i + 1] != null) {
t = (group[i].getLastName().compareToIgnoreCase(group[i + 1].getLastName()));
}
if (t > 0) {
Student tmp = group[i];
group[i] = group[i + 1];
group[i + 1] = tmp;
c++;
}
}
if (c == 0) {
break;
}
}
}
private void printGroup(Student[] group) {
int c = 0;
for (int i = 0; i < group.length; i++) {
if (group[i] != null) {
c++;
System.out.println(c + ". " + group[i]);
} else {
}
}
}
@Override
public String toString() {
// sortGroup(groupp);
System.out.println("Group '" + grouppName + "' students list:");
printGroup(groupp);
return " ";
}
@Override
public Student[] getRecruits() {
Student[] recruits = new Student[0];
int j = 0;
for (int i = 0; i < groupp.length; i++) {
if (groupp[i] != null) {
int age = groupp[i].getAge();
boolean sex = groupp[i].isSex();
if (age >= 18 && sex) {
Student[] tmp = new Student[j + 1];
for (int k = 0; k < recruits.length; k++) {
tmp[k] = recruits[k];
}
recruits = tmp;
recruits[j] = groupp[i];
j++;
}
}
}
sortGroup(recruits);
return recruits;
}
public void sortGroup(String arg) {
if (arg.equalsIgnoreCase("LastName")) {
System.out.println("Group sorted by last name");
} else if (arg.equalsIgnoreCase("FirstName")) {
System.out.println("Group sorted by first name");
} else if (arg.equalsIgnoreCase("Age")) {
System.out.println("Group sorted by age");
} else if (arg.equalsIgnoreCase("Id")) {
System.out.println("Group sorted by id");
} else {
System.out.println("wrong argument");
return;
}
while (true) {
int c = 0;
for (int i = 0; i < groupp.length - 1; i++) {
int t = 0;
if (groupp[i] == null && groupp[i + 1] != null) {
t = 1;
}
if (groupp[i] != null && groupp[i + 1] != null) {
if (arg.equalsIgnoreCase("LastName")) {
t = (groupp[i].getLastName().compareToIgnoreCase(groupp[i + 1].getLastName()));
} else if (arg.equalsIgnoreCase("FirstName")) {
t = (groupp[i].getFirstName().compareToIgnoreCase(groupp[i + 1].getFirstName()));
} else if (arg.equalsIgnoreCase("Age")) {
t = (groupp[i].getAge()) - (groupp[i + 1].getAge());
} else if (arg.equalsIgnoreCase("Id")) {
t = (groupp[i].getId()) - (groupp[i + 1].getId());
}
}
if (t > 0) {
Student tmp = groupp[i];
groupp[i] = groupp[i + 1];
groupp[i + 1] = tmp;
c++;
}
}
if (c == 0) {
break;
}
}
}
static void saveToFile(String address) {
File file = new File(address);
try {
file.createNewFile();
} catch (IOException e) {
System.out.println(e);
}
}
}
以上是关于java HW 5.3-5.4的主要内容,如果未能解决你的问题,请参考以下文章
CentOS 7.x编译安装Nginx1.10.3+MySQL5.7.16+PHP5.2 5.3 5.4 5.5 5.6 7.0 7.1多版本全能环境