Advertisement
Talar97

Lab07_Kolekcja

Dec 1st, 2017
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package com.Talar;
  2.  
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6.  
  7. public class Kolekcja {
  8.     public int rozmiar;
  9.     Czolgi[] zbior;
  10.     public int zapelnienie = 0;
  11.  
  12.     public Kolekcja(int maxValue){
  13.         this.rozmiar = maxValue;
  14.         zbior = new Czolgi[rozmiar];
  15.     }
  16.  
  17.     public void dodajCzolg(Czolgi c){
  18.         for(int i = 0; i < zbior.length; i++){
  19.             if(zbior[i] == null){
  20.                 zbior[i] = c;
  21.                 zapelnienie++;
  22.                 break;
  23.             }
  24.         }
  25.     }
  26.  
  27.     public void wyswietlIndeksy(){
  28.         for(int i = 0; i < zbior.length; i++){
  29.             if(zbior[i]!=null) System.out.print(i + " - " + zbior[i].nazwa + '\n');
  30.         }
  31.     }
  32.  
  33.     public void wyswietlWszystko(){
  34.         for(int i = 0; i < zbior.length; i++){
  35.             if(zbior[i] != null){
  36.                 zbior[i].wyswietlDane();
  37.             }
  38.         }
  39.     }
  40.  
  41.     public void usunCzolg(){
  42.         Scanner inp = new Scanner(System.in);
  43.         this.wyswietlIndeksy();
  44.  
  45.         System.out.print("Wprowadź indeks do usunięcia: "); int indeks = inp.nextInt();
  46.         if(indeks < zbior.length && indeks >= 0){
  47.             zbior[indeks] = null;
  48.             zapelnienie--;
  49.         }
  50.     }
  51.  
  52.     public void edytujCzolg(){
  53.         Scanner inp = new Scanner(System.in);
  54.         this.wyswietlIndeksy();
  55.  
  56.         System.out.print("Wprowadź indeks do edycji: "); int indeks = inp.nextInt();
  57.         if(indeks < zbior.length && indeks >= 0){
  58.             zbior[indeks].ustawWartosci();
  59.         }
  60.     }
  61.  
  62.     public void edytujCeny(double modyfikator){
  63.         for(int i = 0; i < zbior.length; i++){
  64.             if(zbior[i] != null){
  65.                 zbior[i].cena *= modyfikator;
  66.             }
  67.         }
  68.     }
  69.  
  70.     public void zapisDoPliku(String nazwaPliku) throws IOException{
  71.         String defaultPath = "/Users/talar/IdeaProjects/Lab07/src/com/Talar/";
  72.         FileWriter zapis = new FileWriter(defaultPath+nazwaPliku + ".txt", true);
  73.         for(int i = 0; i < zbior.length; i++){
  74.             if(zbior[i] != null){
  75.                 zapis.write(zbior[i].nazwa + "; " + zbior[i].typ + "; " + zbior[i].cena
  76.                 + "; " + zbior[i].pancerz_przod + "; " + zbior[i].kaliber + "; " + zbior[i].skutecznosc + "; \n");
  77.             }
  78.         }
  79.         zapis.close();
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement