Advertisement
Morogn93

Dziala wsio

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