Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public List<Student> listaStudentow = new ArrayList<Student>();
- Scanner czytnik = new Scanner(System.in);
- Student student;
- OperationsOnFiles operacje;
- public static void main(String[] args) {
- Main p2 = new Main();
- p2.TestFile();
- // p2.TestowaMetoda();
- p2.OdczytajZPliku();
- p2.WybierzDzialanie();
- }
- public void TestFile() {
- operacje = new OperationsOnFiles();
- operacje.ProbujPlik(listaStudentow);
- }
- public void OdczytajZPliku() {
- operacje = new OperationsOnFiles();
- listaStudentow = (List<Student>) operacje.OdczytajObiekt();
- }
- public void ZapiszDoPliku() {
- operacje = new OperationsOnFiles();
- operacje.ZapiszObiekt(listaStudentow);
- }
- public void DodajStudenta() {
- ListaWszystkichStudentow();
- listaStudentow.add(WpisywanieStudenta(new Student()));
- }
- public void UsunStudenta() {
- if (listaStudentow.size() == 0) {
- listaStudentow.add(new Student("Brak Imienia", "Brak Nazwiska"));
- }
- listaStudentow.remove(WybierzStudenta() - 1);
- }
- public void DodajOceneStudenta() {
- Student student = listaStudentow.get((WybierzStudenta()) - 1);
- System.out.println(student.getImie());
- WybierzOcene2(student);
- }
- public void ListaWszystkichStudentow() {
- for (Student item : listaStudentow) {
- System.out.println("Imie: " + item.getImie() + " Nazwisko: " + item.getNazwisko());
- }
- }
- public int WybierzStudenta() {
- System.out.print("Ktorego studenta chcesz wybrac: ");
- ListaWszystkichStudentow();
- return getValueMaxMin(listaStudentow.size(), 1);
- }
- public void WybierzDzialanie() {
- int value = 333;
- while (value != 0) {
- ListaWszystkichStudentow();
- System.out.println(
- "1: utwórz studenta. 2: usuń studenta. 3: dodaj Oceny studenta. 4: Zapisz zmiany do pliku BazaStudentow.txt. 5: Pokaz Srednia studenta 0: KONCZY PROGRAM");
- value = getValueMaxMin(5, 0);
- switch (value) {
- case 1:
- DodajStudenta();
- break;
- case 2:
- UsunStudenta();
- break;
- case 3:
- DodajOceneStudenta();
- break;
- case 4:
- ZapiszDoPliku();
- break;
- case 5:
- LiczSrednia();
- default:
- break;
- }
- }
- }
- public Student WpisywanieStudenta(Student value) {
- System.out.println("Podaj imie studenta");
- PodawanieImieniaStudenta(value);
- System.out.println("Podaj nazwisko studenta");
- PodawanieNazwiskaStudenta(value);
- return value;
- }
- public void PodawanieImieniaStudenta(Student value) {
- String temp = "0";
- while (!(checkWord(temp))) {
- System.out.println("Mozesz podawać tylko litery");
- temp = czytnik.next();
- }
- value.setImie(temp);
- }
- public void PodawanieNazwiskaStudenta(Student value) {
- String temp = "0";
- while (!(checkWord(temp))) {
- System.out.println("Mozesz podawać tylko litery");
- temp = czytnik.next();
- }
- value.setNazwisko(temp);
- }
- public boolean checkWord(String name) {
- char[] chars = name.toCharArray();
- for (char c : chars) {
- if (!Character.isLetter(c)) {
- return false;
- }
- }
- return true;
- }
- public boolean checkNumbers(String name) {
- char[] chars = name.toCharArray();
- for (char c : chars) {
- if (!Character.isDigit(c)) {
- return false;
- }
- }
- return true;
- }
- public int getValueStudentMaxMin(Student value, int max, int min) {
- String temp = "a";
- while (!(checkNumbers(temp) && ((Integer.parseInt(temp) <= max && Integer.parseInt(temp) >= min)))) {
- System.out.println("Mozesz podawac tylko cyfry od " + min + " do " + max);
- temp = czytnik.next();
- }
- return Integer.parseInt(temp);
- }
- public int getValueMaxMin(int max, int min) {
- String temp = "a";
- while (!(checkNumbers(temp) && ((Integer.parseInt(temp) <= max && Integer.parseInt(temp) >= min)))) {
- System.out.println("Mozesz podawac tylko cyfry od " + min + " do " + max);
- temp = czytnik.next();
- }
- return Integer.parseInt(temp);
- }
- public int getValueStudent6to2(Student value) {
- String temp = "a";
- while (!(checkNumbers(temp)
- && (((Integer.parseInt(temp) <= 6 && Integer.parseInt(temp) >= 2) || (Integer.parseInt(temp) == 0))))) {
- System.out.println("Mozesz podawac tylko cyfry od 2 do 6 albo 0 bedzie oznaczac brak oceny");
- temp = czytnik.next();
- }
- return Integer.parseInt(temp);
- }
- public void DrukujOcenyStudenta(Student student) {
- System.out.println("Oceny studenta");
- for (int i = 0; i < student.ocena.length; i++) {
- System.out.println(i + 1 + ": " + student.ocena[i].toString());
- }
- }
- public void LiczSrednia() {
- Student student = listaStudentow.get((WybierzStudenta()) - 1);
- System.out.println("Srednia studenta wynosi: " + student.getSrednia());
- }
- public void WybierzOcene2(Student student) {
- DrukujOcenyStudenta(student);
- System.out.println("ktorym miejscu chcesz wprowadzic zmiany od 1-10");
- int index = getValueStudentMaxMin(student, 10, 1);
- WybierzOceneOdJednegoDo6(student, index - 1);
- DrukujOcenyStudenta(student);
- }
- public void WybierzOceneOdJednegoDo6(Student student, int index) {
- System.out.println("Wprowadz ocene 2: 2\n3: 3\n4: 4\n5: 5\n6: 6\n");
- int value = getValueStudent6to2(student);
- switch (value) {
- case 2:
- student.ocena[index] = Ocena.DWA;
- break;
- case 3:
- student.ocena[index] = Ocena.TRZY;
- break;
- case 4:
- student.ocena[index] = Ocena.CZTERY;
- break;
- case 5:
- student.ocena[index] = Ocena.PIEC;
- break;
- case 6:
- student.ocena[index] = Ocena.SZESC;
- break;
- case 0:
- student.ocena[index] = Ocena.ZERO;
- default:
- break;
- }
- }
- }
- import java.io.Serializable;
- public class Student implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- String imie;
- String nazwisko;
- public Ocena[] ocena = new Ocena[10];
- public Student() {
- for (int i = 0; i < ocena.length; i++) {
- ocena[i] = Ocena.ZERO;
- }
- }
- public Student(String imie, String nazwisko) {
- super();
- setImie(imie);
- setNazwisko(nazwisko);
- }
- public double getSrednia() {
- double srednia = 0;
- double counter = 0;
- String temp;
- for (int i = 0; i < ocena.length; i++) {
- temp = ocena[i].value();
- char[] chars = temp.toCharArray();
- for (char c : chars) {
- if (Character.isDigit(c)) {
- counter++;
- srednia = srednia + Character.getNumericValue(c);
- }
- }
- }
- return srednia / counter;
- }
- public String getImie() {
- return imie;
- }
- public void setImie(String imie) {
- this.imie = toUpperCase(imie);
- }
- public String getNazwisko() {
- return nazwisko;
- }
- public void setNazwisko(String nazwisko) {
- this.nazwisko = toUpperCase(nazwisko);
- ;
- }
- public Ocena[] getOcena() {
- return ocena;
- }
- public void setOcena(Ocena ocena, int index) {
- this.ocena[index] = ocena;
- }
- public String toUpperCase(String lancuch) {
- char firstCharOfWord = Character.toUpperCase(lancuch.charAt(0));
- lancuch = firstCharOfWord + lancuch.substring(1);
- return lancuch;
- }
- }
- public enum Ocena {
- DWA("2"), TRZY("3"), CZTERY("4"), PIEC("5"), SZESC("6"), ZERO("BRAK OCENY");
- public String textRepresentation;
- Ocena(String value) {
- this.textRepresentation = value;
- }
- public String value() {
- return textRepresentation;
- }
- @Override
- public String toString() {
- return textRepresentation;
- }
- }
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.List;
- public class OperationsOnFiles {
- final private String nameFile = "BazaStudentow.txt";
- public OperationsOnFiles() {
- }
- /*sprawdza czy baza istnieje jesli nie to program tworzy automatycznie*/
- public void ProbujPlik(List<Student> listaStudentow) {
- try {
- File myFile = new File(nameFile);
- if (myFile.createNewFile()) {
- ZapiszObiekt(listaStudentow);
- System.out.println("Plik BazaStudentow.txt został utworzony jest gotowy do pracy");
- } else {
- System.out.println("Plik BazaStudentow.txt aktualnie istnieje jest gotowy do pracy.");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void ZapiszObiekt(Object obj) {
- File file;
- FileOutputStream fileOut = null;
- ObjectOutputStream out = null;
- try {
- fileOut = new FileOutputStream(file = new File(nameFile));
- out = new ObjectOutputStream(fileOut);
- out.writeObject(obj);
- out.close();
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- public Object OdczytajObiekt() {
- Object obj = new Object();
- File file = new File(nameFile);
- try {
- ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
- obj = in.readObject(); // przerzucanie do objectu
- in.close();
- } catch (IOException | ClassNotFoundException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- return obj;
- }
- /*
- * public void CreateFile(nameFile) { File file = new File(nameFile) }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement