Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1.  
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5. import javax.swing.JOptionPane;
  6.  
  7. class Sklep
  8. {
  9.     public static void main(String args[]) throws FileNotFoundException {
  10.         Scanner sc=new Scanner(System.in);
  11.         String kod;
  12.         String o;
  13.         String line = "";
  14.         FileInputStream fin = null;
  15.         try
  16.         {
  17.             fin = new FileInputStream("id.txt");
  18.         }
  19.         catch(FileNotFoundException e)
  20.         {
  21.             System.out.println("Brak pliku id.txt");
  22.             System.exit(-1);
  23.  
  24.         }
  25.  
  26.         System.out.println("****************************************");
  27.         System.out.println("*             E-HURTOWNIA              *");
  28.         System.out.println("****************************************");
  29.         System.out.println("* Witam w e-hurtownii, podaj swoje id. *");
  30.         System.out.println("****************************************");
  31.         kod = JOptionPane.showInputDialog("Podaj id klienta");
  32.  
  33.         DataInputStream out = new DataInputStream(fin);
  34.         BufferedReader inbr = new BufferedReader(new InputStreamReader(fin));
  35.         boolean ok = false;
  36.         try
  37.         {
  38.             while (((line = inbr.readLine()) != null) && !ok)
  39.             {
  40.                 if(line.equals(kod))
  41.                 {
  42.                     ok = true;
  43.                 }
  44.             }
  45.         }
  46.         catch(IOException e)
  47.         {
  48.             System.out.println(e);
  49.         }
  50.         if(!ok)
  51.         {
  52.             JOptionPane.showMessageDialog(null,"Id jest nieprawidlowy");
  53.             System.exit(-1);
  54.         }
  55.         ArrayList<Produkt> produkty = createListOfProducts();
  56.         System.out.println("****************************************");
  57.         System.out.println("*       Kod pin prawidłowy.            *");
  58.         System.out.println("****************************************");
  59.         System.out.println("*         Lista towarow                *");
  60.         for(int i=0;i<produkty.size();i++)
  61.         {
  62.             System.out.println((i+1)+". "+produkty.get(i).nazwa+" "+produkty.get(i).cena);
  63.         }
  64.         System.out.println("****************************************");
  65.         System.out.println("*      Wybierz jedna z opcji.          *");
  66.         System.out.println("****************************************");
  67.         System.out.println("1 - pierwszy towar");
  68.         System.out.println("...");
  69.         System.out.println(produkty.size()+" - ostatni towar");
  70.         System.out.println("p - podsumowanie");
  71.         System.out.println("k - koniec programu");
  72.         ArrayList<Produkt> paragon = new ArrayList<Produkt>();
  73.         while(true)
  74.         {
  75.             o = JOptionPane.showInputDialog("Wybierz rodzaj operacji").toLowerCase();
  76.             switch(o)
  77.             {
  78.                 case "k":
  79.                     System.exit(0);
  80.                 case "p":
  81.                     //powinien być zapis paragonu do pliku
  82.                     double suma = 0.0;
  83.                     PrintWriter zapis = new PrintWriter("paragon.txt");
  84.                     for(int i=0;i<paragon.size();i++)
  85.                     {
  86.                         Produkt produkt = paragon.get(i);
  87.                         System.out.println(produkt.nazwa+" "+produkt.cena+"*"+produkt.ile+" = "+produkt.cena*produkt.ile);
  88.                         suma+=produkt.cena*produkt.ile;
  89.                         zapis.println(produkt.nazwa+" "+produkt.cena+"*"+produkt.ile+" = "+produkt.cena*produkt.ile);
  90.                     }
  91.                     System.out.println(suma);
  92.                     zapis.close();
  93.                     break;
  94.                 default:
  95.                     ok = false;
  96.                     int nr = -1;
  97.                     try
  98.                     {
  99.                         nr = Integer.parseInt(o)-1;
  100.                         ok = (nr >= 0) && (nr < produkty.size());
  101.                     }
  102.                     catch(NumberFormatException e)
  103.                     {
  104.                     }
  105.                     if(ok)
  106.                     {
  107.                         Produkt produkt = produkty.get(nr);
  108.                         String wybor = JOptionPane.showInputDialog("Towar "+produkt.nazwa+"\nPodaj ilość:");
  109.                         try
  110.                         {
  111.                             double ile = Double.parseDouble(wybor);
  112.                             if(ile!=0.0)
  113.                             {
  114.                                 paragon.add(new Produkt(produkt.nazwa,produkt.cena,ile));
  115.                             }
  116.                         }
  117.                         catch(NumberFormatException e)
  118.                         {
  119.                         }
  120.                     }
  121.             }
  122.         }
  123.     }
  124.     static ArrayList<Produkt> createListOfProducts()
  125.     {
  126.         ArrayList<Produkt> result = new ArrayList<Produkt>();
  127.         result.add(new Produkt("piwo",2.5));
  128.         result.add(new Produkt("truskawki",8.0));
  129.         result.add(new Produkt("chleb",2.5));
  130.         result.add(new Produkt("ser",21.45));
  131.         return result;
  132.     }
  133. }
  134. class Produkt
  135. {
  136.     String nazwa;
  137.     double cena;
  138.     double ile = 0.0;
  139.     public Produkt(String nazwa,double cena)
  140.     {
  141.         this.nazwa = nazwa;
  142.         this.cena = cena;
  143.     }
  144.     public Produkt(String nazwa,double cena,double ile)
  145.     {
  146.         this.nazwa = nazwa;
  147.         this.cena = cena;
  148.         this.ile = ile;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement