Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import javax.swing.JOptionPane;
- class Sklep
- {
- public static void main(String args[]) throws FileNotFoundException {
- Scanner sc=new Scanner(System.in);
- String kod;
- String o;
- String line = "";
- FileInputStream fin = null;
- try
- {
- fin = new FileInputStream("id.txt");
- }
- catch(FileNotFoundException e)
- {
- System.out.println("Brak pliku id.txt");
- System.exit(-1);
- }
- System.out.println("****************************************");
- System.out.println("* E-HURTOWNIA *");
- System.out.println("****************************************");
- System.out.println("* Witam w e-hurtownii, podaj swoje id. *");
- System.out.println("****************************************");
- kod = JOptionPane.showInputDialog("Podaj id klienta");
- DataInputStream out = new DataInputStream(fin);
- BufferedReader inbr = new BufferedReader(new InputStreamReader(fin));
- boolean ok = false;
- try
- {
- while (((line = inbr.readLine()) != null) && !ok)
- {
- if(line.equals(kod))
- {
- ok = true;
- }
- }
- }
- catch(IOException e)
- {
- System.out.println(e);
- }
- if(!ok)
- {
- JOptionPane.showMessageDialog(null,"Id jest nieprawidlowy");
- System.exit(-1);
- }
- ArrayList<Produkt> produkty = createListOfProducts();
- System.out.println("****************************************");
- System.out.println("* Kod pin prawidłowy. *");
- System.out.println("****************************************");
- System.out.println("* Lista towarow *");
- for(int i=0;i<produkty.size();i++)
- {
- System.out.println((i+1)+". "+produkty.get(i).nazwa+" "+produkty.get(i).cena);
- }
- System.out.println("****************************************");
- System.out.println("* Wybierz jedna z opcji. *");
- System.out.println("****************************************");
- System.out.println("1 - pierwszy towar");
- System.out.println("...");
- System.out.println(produkty.size()+" - ostatni towar");
- System.out.println("p - podsumowanie");
- System.out.println("k - koniec programu");
- ArrayList<Produkt> paragon = new ArrayList<Produkt>();
- while(true)
- {
- o = JOptionPane.showInputDialog("Wybierz rodzaj operacji").toLowerCase();
- switch(o)
- {
- case "k":
- System.exit(0);
- case "p":
- //powinien być zapis paragonu do pliku
- double suma = 0.0;
- PrintWriter zapis = new PrintWriter("paragon.txt");
- for(int i=0;i<paragon.size();i++)
- {
- Produkt produkt = paragon.get(i);
- System.out.println(produkt.nazwa+" "+produkt.cena+"*"+produkt.ile+" = "+produkt.cena*produkt.ile);
- suma+=produkt.cena*produkt.ile;
- zapis.println(produkt.nazwa+" "+produkt.cena+"*"+produkt.ile+" = "+produkt.cena*produkt.ile);
- }
- System.out.println(suma);
- zapis.close();
- break;
- default:
- ok = false;
- int nr = -1;
- try
- {
- nr = Integer.parseInt(o)-1;
- ok = (nr >= 0) && (nr < produkty.size());
- }
- catch(NumberFormatException e)
- {
- }
- if(ok)
- {
- Produkt produkt = produkty.get(nr);
- String wybor = JOptionPane.showInputDialog("Towar "+produkt.nazwa+"\nPodaj ilość:");
- try
- {
- double ile = Double.parseDouble(wybor);
- if(ile!=0.0)
- {
- paragon.add(new Produkt(produkt.nazwa,produkt.cena,ile));
- }
- }
- catch(NumberFormatException e)
- {
- }
- }
- }
- }
- }
- static ArrayList<Produkt> createListOfProducts()
- {
- ArrayList<Produkt> result = new ArrayList<Produkt>();
- result.add(new Produkt("piwo",2.5));
- result.add(new Produkt("truskawki",8.0));
- result.add(new Produkt("chleb",2.5));
- result.add(new Produkt("ser",21.45));
- return result;
- }
- }
- class Produkt
- {
- String nazwa;
- double cena;
- double ile = 0.0;
- public Produkt(String nazwa,double cena)
- {
- this.nazwa = nazwa;
- this.cena = cena;
- }
- public Produkt(String nazwa,double cena,double ile)
- {
- this.nazwa = nazwa;
- this.cena = cena;
- this.ile = ile;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement