Advertisement
Morogn93

DZIALA

Apr 4th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.73 KB | None | 0 0
  1. import java.util.concurrent.ArrayBlockingQueue;
  2. import java.util.jar.Attributes.Name;
  3.  
  4. import javax.security.auth.callback.ChoiceCallback;
  5. import javax.swing.plaf.metal.OceanTheme;
  6.  
  7. import java.io.LineNumberInputStream;
  8. import java.lang.invoke.SwitchPoint;
  9. import java.util.*;
  10.  
  11. public class Main {
  12.  
  13.     public List<Student> listaStudentow = new ArrayList<Student>();
  14.     Scanner czytnik = new Scanner(System.in);
  15.     Student student;
  16.     OperationsOnFiles operacje;
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.        
  21.  
  22.         Main p2 = new Main();
  23.         //p2.TestowaMetoda();
  24.         p2.OdczytajZPliku();
  25.        
  26.         p2.WybierzDzialanie();
  27.  
  28.         Student student = new Student();
  29.  
  30.         p2.PodawanieImieniaStudenta(student);
  31.         System.out.println(student.getImie());
  32.         p2.WybierzOcene2(student);
  33.  
  34.     }
  35.  
  36.     public void TestowaMetoda() {
  37.        
  38.         Student osoba = new Student("Andrzej", "Duda");
  39.         osoba.setOcena(Ocena.CZTERY, 2);
  40.         Student osoba2 = new Student("Jan", "Kowalski");
  41.         osoba.setOcena(Ocena.SZESC, 2);
  42.         Student osoba3 = new Student("Janina", "Kowalska");
  43.         osoba.setOcena(Ocena.PIEC, 4);
  44.  
  45.         listaStudentow.add(osoba);
  46.         listaStudentow.add(osoba2);
  47.         listaStudentow.add(osoba3);
  48.  
  49.         OperationsOnFiles operacje = new OperationsOnFiles();
  50.  
  51.         operacje.ZapiszObiekt(listaStudentow);
  52.  
  53.         listaStudentow = (List<Student>) operacje.OdczytajObiekt();
  54.  
  55.         for (Student item : listaStudentow) {
  56.  
  57.             System.out.println(item.imie + " " + item.nazwisko + " ");
  58.             System.out.println();
  59.             System.out.println();
  60.         }
  61.  
  62.         for (int x = 0; x < listaStudentow.size(); x++) {
  63.             System.out.println(listaStudentow.get(x).imie + " " + listaStudentow.get(x).nazwisko + " ");
  64.         }
  65.     }
  66.    
  67.    
  68.    
  69.     public void OdczytajZPliku() {
  70.         operacje = new OperationsOnFiles();
  71.         listaStudentow = (List<Student>) operacje.OdczytajObiekt();
  72.     }
  73.  
  74.     public void ZapiszDoPliku() {
  75.         operacje = new OperationsOnFiles();
  76.         operacje.ZapiszObiekt(listaStudentow);
  77.     }
  78.  
  79.     public void DodajStudenta() {
  80.         ListaWszystkichStudentow();
  81.         listaStudentow.add(WpisywanieStudenta(new Student()));
  82.         ListaWszystkichStudentow();
  83.     }
  84.  
  85.     public void UsunStudenta() {
  86.         if (listaStudentow.size() == 0) {
  87.             listaStudentow.add(new Student("Brak Imienia", "Brak Nazwiska"));
  88.         }
  89.         listaStudentow.remove(WybierzStudenta() - 1);
  90.     }
  91.  
  92.     public void DodajOceneStudenta() {
  93.  
  94.         Student student = listaStudentow.get((WybierzStudenta()) - 1);
  95.         System.out.println(student.getImie());
  96.         DrukujOcenyStudenta(student);
  97.         WybierzOcene2(student);
  98.  
  99.     }
  100.  
  101.     public void ListaWszystkichStudentow() {
  102.         for (Student item : listaStudentow) {
  103.             System.out.println("Imie: " + item.getImie() + " Nazwisko: " + item.getNazwisko());
  104.         }
  105.     }
  106.  
  107.     public int WybierzStudenta() {
  108.         System.out.print("Ktorego studenta chcesz wybrac: ");
  109.         ListaWszystkichStudentow();
  110.  
  111.         return getValueMaxMin(listaStudentow.size(), 1);
  112.     }
  113.  
  114.     public void WybierzDzialanie() {
  115.         int value = 333;
  116.  
  117.         while (value != 0) {
  118.             ListaWszystkichStudentow();
  119.             System.out.println(
  120.                     "1: utwórz studenta. 2: usuń studenta. 3: dodaj Oceny studenta. 4: Zapisz zmiany do pliku BazaStudentow.txt");
  121.             value = getValueMaxMin(4, 1);
  122.  
  123.             switch (value) {
  124.             case 1:
  125.                 DodajStudenta();
  126.                 break;
  127.             case 2:
  128.                 UsunStudenta();
  129.                 break;
  130.             case 3:
  131.                 DodajOceneStudenta();
  132.                 break;
  133.             case 4:
  134.                 ZapiszDoPliku();
  135.                 break;
  136.             default:
  137.                 break;
  138.             }
  139.  
  140.         }
  141.  
  142.     }
  143.  
  144.     public Student WpisywanieStudenta(Student value) {
  145.         System.out.println("Podaj imie studenta");
  146.         PodawanieImieniaStudenta(value);
  147.         System.out.println("Podaj nazwisko studenta");
  148.         PodawanieNazwiskaStudenta(value);
  149.  
  150.         return value;
  151.     }
  152.  
  153.     public void PodawanieImieniaStudenta(Student value) {
  154.         String temp = "0";
  155.         while (!(checkWord(temp))) {
  156.             System.out.println("Mozesz podawać tylko litery");
  157.             temp = czytnik.next();
  158.         }
  159.         value.setImie(temp);
  160.     }
  161.  
  162.     public void PodawanieNazwiskaStudenta(Student value) {
  163.         String temp = "0";
  164.         while (!(checkWord(temp))) {
  165.             System.out.println("Mozesz podawać tylko litery");
  166.             temp = czytnik.next();
  167.         }
  168.         value.setNazwisko(temp);
  169.     }
  170.  
  171.     public boolean checkWord(String name) {
  172.         char[] chars = name.toCharArray();
  173.         for (char c : chars) {
  174.             if (!Character.isLetter(c)) {
  175.                 return false;
  176.             }
  177.         }
  178.         return true;
  179.     }
  180.  
  181.     public boolean checkNumbers(String name) {
  182.         char[] chars = name.toCharArray();
  183.         for (char c : chars) {
  184.             if (!Character.isDigit(c)) {
  185.                 return false;
  186.             }
  187.         }
  188.         return true;
  189.     }
  190.  
  191.     public int getValueStudentMaxMin(Student value, int max, int min) {
  192.         String temp = "a";
  193.         while (!(checkNumbers(temp) && ((Integer.parseInt(temp) <= max && Integer.parseInt(temp) >= min)))) {
  194.             System.out.println("Mozesz podawac tylko cyfry od " + min + " do " + max);
  195.             temp = czytnik.next();
  196.         }
  197.  
  198.         return Integer.parseInt(temp);
  199.     }
  200.  
  201.     public int getValueMaxMin(int max, int min) {
  202.         String temp = "a";
  203.         while (!(checkNumbers(temp) && ((Integer.parseInt(temp) <= max && Integer.parseInt(temp) >= min)))) {
  204.             System.out.println("Mozesz podawac tylko cyfry od " + min + " do " + max);
  205.             temp = czytnik.next();
  206.         }
  207.  
  208.         return Integer.parseInt(temp);
  209.     }
  210.  
  211.     public int getValueStudent6to2(Student value) {
  212.         String temp = "a";
  213.         while (!(checkNumbers(temp)
  214.                 && (((Integer.parseInt(temp) <= 6 && Integer.parseInt(temp) >= 2) || (Integer.parseInt(temp) == 0))))) {
  215.             System.out.println("Mozesz podawac tylko cyfry od 2 do 6 albo 0 bedzie oznaczac brak oceny");
  216.             temp = czytnik.next();
  217.         }
  218.         return Integer.parseInt(temp);
  219.     }
  220.  
  221.     public void DrukujOcenyStudenta(Student student) {
  222.         System.out.println("Oceny studenta");
  223.         for (int i = 0; i < student.ocena.length; i++) {
  224.             System.out.println(i + 1 + ": " + student.ocena[i].toString());
  225.         }
  226.     }
  227.  
  228.     public void WybierzOcene2(Student student) {
  229.         DrukujOcenyStudenta(student);
  230.         System.out.println("ktorym miejscu chcesz wprowadzic zmiany od 1-10");
  231.         int index = getValueStudentMaxMin(student, 10, 1);
  232.  
  233.         WybierzOceneOdJednegoDo6(student, index - 1);
  234.         DrukujOcenyStudenta(student);
  235.  
  236.     }
  237.  
  238.     public void WybierzOceneOdJednegoDo6(Student student, int index) {
  239.         System.out.println("Wprowadz ocene 2: 2\n3: 3\n4: 4\n5: 5\n6: 6\n");
  240.         int value = getValueStudent6to2(student);
  241.  
  242.         switch (value) {
  243.         case 2:
  244.             student.ocena[index] = Ocena.DWA;
  245.             break;
  246.         case 3:
  247.             student.ocena[index] = Ocena.TRZY;
  248.             break;
  249.         case 4:
  250.             student.ocena[index] = Ocena.CZTERY;
  251.             break;
  252.         case 5:
  253.             student.ocena[index] = Ocena.PIEC;
  254.             break;
  255.         case 6:
  256.             student.ocena[index] = Ocena.SZESC;
  257.             break;
  258.         case 0:
  259.             student.ocena[index] = Ocena.ZERO;
  260.         default:
  261.             break;
  262.         }
  263.     }
  264. }
  265.  
  266. /*
  267.  * jesli podales liczbe z klawiatury czyli element w ktory ma byc zmieniona
  268.  * wartosc
  269.  *
  270.  */
  271.  
  272. /*
  273.  *
  274.  *
  275.  *
  276.  *
  277.  * */
  278.  
  279. /*
  280.  * Włacz program wczytaj dane do kolekcji wyswietl menu wyboru 1 dodaj studenta
  281.  * = imie i nazwisko 2 przegladaj liste studentow wybierasz studenta -----> 1
  282.  * zmiana nazwiska 2 zmiana imienia 3 Edycja ocen 1 wyswietl liste ocen 1 dodaj
  283.  * ocene 2 usun ocene 3 zapisz plik 0 wyjdz z programu
  284.  *
  285.  *
  286.  *
  287.  *
  288.  */
  289.  
  290. public enum Ocena{
  291.     DWA("2"), TRZY(
  292.             "3"), CZTERY("4"), PIEC("5"), SZESC("6"), ZERO("BRAK OCENY");
  293.     public String textRepresentation;
  294.    
  295.         Ocena(String value) {
  296.             this.textRepresentation= value;          
  297.         }
  298.  
  299.         public String value() {
  300.             return textRepresentation;
  301.         }
  302.         @Override public String toString() {
  303.             return textRepresentation;
  304.         }
  305.        
  306.        
  307.     }
  308.  
  309. import java.io.File;
  310. import java.io.FileInputStream;
  311. import java.io.FileOutputStream;
  312. import java.io.IOException;
  313. import java.io.ObjectInputStream;
  314. import java.io.ObjectOutputStream;
  315.  
  316. public class OperationsOnFiles {
  317.    
  318.     final private String nameFile = "BazaStudentow.txt";
  319.    
  320.    
  321.     public OperationsOnFiles() {
  322.         if (!(new File(nameFile).exists())){
  323.             new File(nameFile);
  324.         }
  325.     }
  326.    
  327.    
  328.     public void ZapiszObiekt(Object obj) {
  329.         File file;
  330.         FileOutputStream fileOut = null;
  331.        
  332.         ObjectOutputStream out = null;
  333.        
  334.         try {
  335.             fileOut = new FileOutputStream(file = new File(nameFile));
  336.             out = new ObjectOutputStream(fileOut);
  337.             out.writeObject(obj);
  338.             out.close();   
  339.            
  340.         } catch (Exception e) {
  341.             // TODO: handle exception
  342.             e.printStackTrace();
  343.         }
  344.     }
  345.    
  346.     public Object OdczytajObiekt() {
  347.         Object obj = new Object();
  348.         File file = new File(nameFile);
  349.        
  350.        
  351.         try {
  352.             ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
  353.             obj = in.readObject(); // przerzucanie do objectu
  354.             in.close();
  355.         } catch (IOException | ClassNotFoundException e) {
  356.             // TODO: handle exception
  357.             e.printStackTrace();
  358.         }
  359.         return obj;
  360.     }
  361.    
  362.    
  363.     /*public void CreateFile(nameFile) {
  364.         File file = new File(nameFile)
  365.     }*/
  366.    
  367.    
  368.    
  369.    
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement