Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.jar.Attributes.Name;
- import javax.security.auth.callback.ChoiceCallback;
- import javax.swing.plaf.metal.OceanTheme;
- import java.io.LineNumberInputStream;
- import java.lang.invoke.SwitchPoint;
- 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.TestowaMetoda();
- p2.OdczytajZPliku();
- p2.WybierzDzialanie();
- Student student = new Student();
- p2.PodawanieImieniaStudenta(student);
- System.out.println(student.getImie());
- p2.WybierzOcene2(student);
- }
- public void TestowaMetoda() {
- Student osoba = new Student("Andrzej", "Duda");
- osoba.setOcena(Ocena.CZTERY, 2);
- Student osoba2 = new Student("Jan", "Kowalski");
- osoba.setOcena(Ocena.SZESC, 2);
- Student osoba3 = new Student("Janina", "Kowalska");
- osoba.setOcena(Ocena.PIEC, 4);
- listaStudentow.add(osoba);
- listaStudentow.add(osoba2);
- listaStudentow.add(osoba3);
- OperationsOnFiles operacje = new OperationsOnFiles();
- operacje.ZapiszObiekt(listaStudentow);
- listaStudentow = (List<Student>) operacje.OdczytajObiekt();
- for (Student item : listaStudentow) {
- System.out.println(item.imie + " " + item.nazwisko + " ");
- System.out.println();
- System.out.println();
- }
- for (int x = 0; x < listaStudentow.size(); x++) {
- System.out.println(listaStudentow.get(x).imie + " " + listaStudentow.get(x).nazwisko + " ");
- }
- }
- 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()));
- ListaWszystkichStudentow();
- }
- 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());
- DrukujOcenyStudenta(student);
- 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");
- value = getValueMaxMin(4, 1);
- switch (value) {
- case 1:
- DodajStudenta();
- break;
- case 2:
- UsunStudenta();
- break;
- case 3:
- DodajOceneStudenta();
- break;
- case 4:
- ZapiszDoPliku();
- break;
- 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 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;
- }
- }
- }
- /*
- * jesli podales liczbe z klawiatury czyli element w ktory ma byc zmieniona
- * wartosc
- *
- */
- /*
- *
- *
- *
- *
- * */
- /*
- * Włacz program wczytaj dane do kolekcji wyswietl menu wyboru 1 dodaj studenta
- * = imie i nazwisko 2 przegladaj liste studentow wybierasz studenta -----> 1
- * zmiana nazwiska 2 zmiana imienia 3 Edycja ocen 1 wyswietl liste ocen 1 dodaj
- * ocene 2 usun ocene 3 zapisz plik 0 wyjdz z programu
- *
- *
- *
- *
- */
- 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;
- public class OperationsOnFiles {
- final private String nameFile = "BazaStudentow.txt";
- public OperationsOnFiles() {
- if (!(new File(nameFile).exists())){
- new File(nameFile);
- }
- }
- 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