Created
June 1, 2015 22:03
-
-
Save ad-m/d37233496c1822940f37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class Address { | |
| private String street; | |
| private int apartmentNumber; | |
| private int houseNumber; | |
| public Address(String street, int apartmentNumber, int houseNumber) { | |
| this.street = street; | |
| this.apartmentNumber = apartmentNumber; | |
| this.houseNumber = houseNumber; | |
| } | |
| public String toString() { | |
| return "Address [street=" + street + ", apartmentNumber=" | |
| + apartmentNumber + ", houseNumber=" + houseNumber + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Mamy sklep. W sklepie sprzedawane są różne towary. Te towary mają nazwę, | |
| * cenę. Towar sprzedają sprzedawcy. Mają imię i nazwisko, a także numer ID. | |
| * W sklepie towary kupują klienci. Klient to imię, nazwisko, adres. Klient może | |
| * kupować wiele towarów. Klient może kupować wiele towarów. Zakup kończy się | |
| * wystawieniem faktury (adres klienta, sklepu, wykaz pozycji z kosztem, koszt całkowity) | |
| * z dodawaniem i usuwaniem pozycji z faktury. Aplikacja | |
| * powinna umożliwiać gromadzenie informacji o fakturach (dodawanie, usuwanie | |
| * pozycji), sprzedawcach, towarach. */ | |
| package lab12; | |
| import java.math.BigDecimal; | |
| import java.util.Iterator; | |
| import java.util.Scanner; | |
| public class App{ | |
| private StaffSet ss; | |
| private ClientSet cs; | |
| private InvoiceSet is; | |
| private ProductSet ps; | |
| private ShopSet ssh; | |
| public App(StaffSet ss, ClientSet cs, InvoiceSet is, ProductSet ps, ShopSet ssh) { | |
| super(); | |
| this.ss = ss; | |
| this.cs = cs; | |
| this.is = is; | |
| this.ps = ps; | |
| this.ssh = ssh; | |
| } | |
| private Scanner sc = new Scanner(System.in); | |
| private void run() { | |
| menu(); | |
| } | |
| private void menu() { | |
| String entry; | |
| do{ | |
| this.displayMenu(); | |
| entry = sc.nextLine().trim(); | |
| System.out.print("BBB: "+entry+"CCCC"); | |
| if(entry.equals("1")){ | |
| this.addProduct(); | |
| }else if(entry.equals("2")){ | |
| this.addStaff(); | |
| }else if(entry.equals("3")){ | |
| this.addShop(); | |
| }else if(entry.equals("4")){ | |
| this.addInvoice(); | |
| }else if(entry.equals("5")){ | |
| this.addClient(); | |
| }else if(entry.equals("9")){ | |
| }else if(entry.equals("0")){ | |
| System.out.println("Bye!"); | |
| }else{ | |
| System.out.print("Unknown action"); | |
| } | |
| }while(!entry.equals("0")); | |
| } | |
| private Address getAddress(){ | |
| System.out.print("Get street name:"); | |
| return new Address(sc.nextLine(),0,0); | |
| } | |
| private Product getProduct(){ | |
| int i=0; | |
| for(Iterator<Product> iterator = ps.iterator(); iterator.hasNext(); i++){ | |
| System.out.print(i + ". " + iterator.next()); | |
| } | |
| System.out.print("Enter no:"); | |
| return ps.get(sc.nextInt()); | |
| } | |
| private Client getClient(){ | |
| int i=0; | |
| for(Iterator<Client> iterator = cs.iterator(); iterator.hasNext(); i++){ | |
| System.out.print(i + ". " + iterator.next()); | |
| } | |
| System.out.print("Enter no:"); | |
| return cs.get(sc.nextInt()); | |
| } | |
| private Staff getStaff(){ | |
| int i=0; | |
| for(Iterator<Staff> iterator = ss.iterator(); iterator.hasNext(); i++){ | |
| System.out.print(i + ". " + iterator.next()); | |
| } | |
| System.out.print("Enter no:"); | |
| return ss.get(sc.nextInt()); | |
| } | |
| private Shop getShop(){ | |
| int i=0; | |
| for(Iterator<Shop> iterator = ssh.iterator(); iterator.hasNext(); i++){ | |
| System.out.print(i + ". " + iterator.next()); | |
| } | |
| System.out.print("Enter no:"); | |
| return ssh.get(sc.nextInt()); | |
| } | |
| private void addClient() { | |
| System.out.print("Get first name:"); | |
| String firstName = sc.nextLine(); | |
| System.out.print("Get second name:"); | |
| String secondName = sc.nextLine(); | |
| Client obj = new Client(firstName, secondName, this.getAddress()); | |
| cs.add(obj); | |
| System.out.print("Added " + obj); | |
| } | |
| private void addInvoice() { | |
| Shop shop = this.getShop(); | |
| Client client = this.getClient(); | |
| Staff staff = this.getStaff(); | |
| Invoice obj = new Invoice(staff, client, shop); | |
| is.add(obj); | |
| System.out.print("Added " + obj); | |
| } | |
| private void addShop() { | |
| System.out.print("Get name:"); | |
| String name = sc.nextLine(); | |
| Shop obj = new Shop(name, this.getAddress()); | |
| ssh.add(obj); | |
| System.out.print("Added " + obj); | |
| } | |
| private void addStaff() { | |
| System.out.print("Get first name:"); | |
| String firstName = sc.nextLine(); | |
| System.out.print("Get second name:"); | |
| String secondName = sc.nextLine(); | |
| System.out.print("Get ID:"); | |
| int id= sc.nextInt(); | |
| Staff obj = new Staff(firstName, secondName, id); | |
| ss.add(obj); | |
| System.out.print("Added " + obj); | |
| } | |
| private void addProduct() { | |
| System.out.print("Get name:"); | |
| String name = sc.nextLine(); | |
| System.out.print("Get price:"); | |
| BigDecimal price = new BigDecimal(sc.nextLine()); | |
| Product obj = new Product(name, price); | |
| System.out.print("Added " + obj); | |
| ps.add(obj); | |
| } | |
| private void displayMenu() { | |
| System.out.println("1 - dodaj towar"); | |
| System.out.println("2 - dodaj sprzedawce"); | |
| System.out.println("3 - dodaj sklep"); | |
| System.out.println("4 - dodaj zamowienie"); | |
| System.out.println("5 - dodaj klient"); | |
| System.out.println("9 - zapisz wszystko"); | |
| System.out.println("0 - koniec"); | |
| } | |
| public static void main(String[] args) { | |
| StaffSet ss = new StaffSet(); | |
| ss.fromFile("ss.bin"); | |
| ClientSet cs = new ClientSet(); | |
| cs.fromFile("cs.bin"); | |
| InvoiceSet is = new InvoiceSet(); | |
| is.fromFile("is.bin"); | |
| ProductSet ps = new ProductSet(); | |
| ps.fromFile("ps.bin"); | |
| ShopSet ssh = new ShopSet(); | |
| ssh.fromFile("ssh.bin"); | |
| new App(ss, cs, is, ps, ssh).run(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class Client extends Person { | |
| Address address; | |
| public Client(String firstName, String secondName, Address address) { | |
| super(firstName, secondName); | |
| this.address = address; | |
| } | |
| public Address getAddress() { | |
| return address; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Client [address=" + address + ", toString()=" | |
| + super.toString() + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class ClientSet extends Set<Client> { | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = -1842241984827636571L; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| import java.math.BigDecimal; | |
| public class Entry { | |
| private int amount; | |
| private Product product; | |
| public Entry(int amount, Product product) { | |
| super(); | |
| this.amount = amount; | |
| this.product = product; | |
| } | |
| public int getAmount() { | |
| return amount; | |
| } | |
| public void setAmount(int amount) { | |
| this.amount = amount; | |
| } | |
| public Product getProduct() { | |
| return product; | |
| } | |
| public void setProduct(Product product) { | |
| this.product = product; | |
| } | |
| public BigDecimal getValue(){ | |
| BigDecimal bd = new BigDecimal(String.valueOf(this.amount)); | |
| return this.product.getPrice().multiply(bd); | |
| } | |
| @Override | |
| public String toString() { | |
| return "Entry [amount=" + amount + ", product=" + product | |
| + ", toString()=" + super.toString() + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| import java.math.BigDecimal; | |
| import java.util.Iterator; | |
| import java.util.LinkedList; | |
| public class Invoice { | |
| private Staff staff; | |
| private Client client; | |
| private Shop shop; | |
| private LinkedList<Entry> entries = new LinkedList<Entry>(); | |
| public Invoice(Staff staff, Client client, Shop shop) { | |
| super(); | |
| this.staff = staff; | |
| this.client = client; | |
| this.shop = shop; | |
| } | |
| public Iterator<Entry> iterator() { | |
| return entries.iterator(); | |
| } | |
| public boolean add(Entry e) { | |
| return entries.add(e); | |
| } | |
| public Client getClient() { | |
| return client; | |
| } | |
| public void setClient(Client client) { | |
| this.client = client; | |
| } | |
| private BigDecimal total(){ | |
| BigDecimal bd = new BigDecimal("0"); | |
| for(Entry e: entries){ | |
| bd.add(e.getValue()); | |
| } | |
| return bd; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Invoice [staff=" + staff + ", client=" + client + ", shop=" | |
| + shop + ", entries=" + entries + ", total()=" + total() + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class InvoiceSet extends Set<Invoice> { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class Person { | |
| private String firstName; | |
| private String secondName; | |
| public Person(String firstName, String secondName) { | |
| super(); | |
| this.firstName = firstName; | |
| this.secondName = secondName; | |
| } | |
| public String getFirstName() { | |
| return firstName; | |
| } | |
| public void setFirstName(String firstName) { | |
| this.firstName = firstName; | |
| } | |
| public String getSecondName() { | |
| return secondName; | |
| } | |
| public void setSecondName(String secondName) { | |
| this.secondName = secondName; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| import java.math.BigDecimal; | |
| public class Product { | |
| private String name; | |
| private BigDecimal price; | |
| public Product(String name, BigDecimal price) { | |
| super(); | |
| this.name = name; | |
| this.price = price; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public BigDecimal getPrice() { | |
| return price; | |
| } | |
| public void setPrice(BigDecimal price) { | |
| this.price = price; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Product [name=" + name + ", price=" + price + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class ProductSet extends Set<Product>{ | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = -3493835544036631471L; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| import java.io.*; | |
| import java.util.Iterator; | |
| import java.util.LinkedList; | |
| public abstract class Set<T> implements Serializable { | |
| /** | |
| * | |
| */ | |
| private static final long serialVersionUID = 1248377715356025802L; | |
| private LinkedList<T> data = new LinkedList<T>(); | |
| public Iterator<T> iterator() { | |
| return data.iterator(); | |
| } | |
| public int size() { | |
| return data.size(); | |
| } | |
| public T get(int index) { | |
| return data.get(index); | |
| } | |
| public boolean add(T e) { | |
| return data.add(e); | |
| } | |
| public void clear() { | |
| data.clear(); | |
| } | |
| public void fromFile(String filename) { | |
| try (InputStream file = new FileInputStream(filename); | |
| InputStream buffer = new BufferedInputStream(file); | |
| ObjectInputStream input = new ObjectInputStream(file)) { | |
| T el; | |
| try { | |
| while ((el = (T) input.readObject()) != null) { | |
| this.add(el); | |
| } | |
| } catch (EOFException | ClassNotFoundException e) { | |
| } | |
| } catch (IOException e1) { | |
| e1.printStackTrace(); | |
| }; | |
| }; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class Shop { | |
| private String name; | |
| private Address address; | |
| public Shop(String name, Address address) { | |
| super(); | |
| this.name = name; | |
| this.address = address; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public Address getAddress() { | |
| return address; | |
| } | |
| public void setAddress(Address address) { | |
| this.address = address; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Shop [name=" + name + ", address=" + address + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class ShopSet extends Set<Shop> { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| public class Staff extends Person{ | |
| private int id; | |
| public Staff(String firstName, String secondName, int id) { | |
| super(firstName, secondName); | |
| this.id = id; | |
| } | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Staff [id=" + id + ", toString()=" + super.toString() + "]"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lab12; | |
| import java.util.LinkedList; | |
| public class StaffSet extends Set<Staff> { | |
| private static final long serialVersionUID = 1644363768729919330L; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment