From 3821a5ca263474fb1105a0777ceacf1c357f0231 Mon Sep 17 00:00:00 2001 From: Anyx Date: Fri, 3 Nov 2023 20:37:05 +0200 Subject: [PATCH] Initial Commit --- Bookcase.java | 47 ++++++++++++++++++++++++ Chair.java | 42 ++++++++++++++++++++++ Furniture.java | 57 +++++++++++++++++++++++++++++ FurnitureCompany.java | 21 +++++++++++ FurnitureFrame.java | 83 +++++++++++++++++++++++++++++++++++++++++++ Main.java | 14 ++++++++ 6 files changed, 264 insertions(+) create mode 100644 Bookcase.java create mode 100644 Chair.java create mode 100644 Furniture.java create mode 100644 FurnitureCompany.java create mode 100644 FurnitureFrame.java create mode 100644 Main.java diff --git a/Bookcase.java b/Bookcase.java new file mode 100644 index 0000000..ff4550c --- /dev/null +++ b/Bookcase.java @@ -0,0 +1,47 @@ + +public class Bookcase extends Furniture{ + private int shelves; + + public Bookcase(String model, int weight, int category,int shelves) { + super(model, weight, category); + this.shelves=shelves; + + } + + public double calculatePrice() { + double currentPrice = 50*shelves ; + if(this.getCategory()==1) { + currentPrice=(50*shelves)+(0.2*currentPrice); + } + else if(this.getCategory()==2) { + currentPrice=(50*shelves)+(0.3*currentPrice); + }else if(this.getCategory()==3) { + currentPrice=(50*shelves)+(0.5*currentPrice); + } + return currentPrice; + } + + public String chooseCategory() { + String aCategory=""; + if(this.getCategory()==1) { + return aCategory ="Plastic"; + } + else if(this.getCategory()==2) { + return aCategory = "Metallic"; + }else if(this.getCategory()==3) { + aCategory = "Wooden"; + } + return aCategory; + } + + public int getShelves() { + return shelves; + } + + public String getInfo() { + super.getInfo(); + return "Shelves: "+this.getShelves(); + } + + +} diff --git a/Chair.java b/Chair.java new file mode 100644 index 0000000..e9d6d99 --- /dev/null +++ b/Chair.java @@ -0,0 +1,42 @@ + +public class Chair extends Furniture{ + private String color; + + public Chair(String model, int weight, int category,String color) { + super(model, weight, category); + this.color=color; + + } + + public String getColor() { + return color; + } + + public double calculatePrice() { + double currentPrice = 100 ; + + if(this.getCategory()==3) { + currentPrice=100+(0.3*currentPrice); + } + return currentPrice; + } + + + public String chooseCategory() { + String aCategory=""; + if(this.getCategory()==1) { + return aCategory ="Office chair"; + } + else if(this.getCategory()==2) { + return aCategory = "Cooperation Chair"; + }else if(this.getCategory()==3) { + aCategory = "Armchair"; + } + return aCategory; + } + + public String getInfo() { + super.getInfo(); + return "Shelves: "+this.getColor(); + } +} diff --git a/Furniture.java b/Furniture.java new file mode 100644 index 0000000..82998a5 --- /dev/null +++ b/Furniture.java @@ -0,0 +1,57 @@ +import java.util.ArrayList; + +public abstract class Furniture implements Comparable{ + + private String model; + private int weight; + private int category; + + private ArrayList allFurniture = new ArrayList(); + + + public Furniture(String model, int weight, int category) { + + this.model = model; + this.weight = weight; + this.category = category; + } + + public int getCategory() { + return category; + } + + public boolean equals(Furniture o) { + Furniture otherFurniture =(Furniture)o; + return this.equals(otherFurniture); + + } + + public abstract double calculatePrice(); + public abstract String chooseCategory(); + + + public int getWeight() { + return weight; + } + + public String getModel() { + return model; + } + public String getInfo() { + return "-"+this.getClass()+"info:\n"+ + this.getModel()+","+this.calculatePrice()+"Euro: \n"+ + "Category: "+this.chooseCategory()+"\n"+ + "Weight: "+this.getWeight()+"\n"; + + } + @Override + public int compareTo(Object o) { + Furniture otherFurniture = (Furniture)o; + return Double.compare(otherFurniture.calculatePrice(), this.calculatePrice()); + } + + + + + +} diff --git a/FurnitureCompany.java b/FurnitureCompany.java new file mode 100644 index 0000000..7689955 --- /dev/null +++ b/FurnitureCompany.java @@ -0,0 +1,21 @@ +import java.util.ArrayList; + +public class FurnitureCompany{ + + private ArrayList allFurniture = new ArrayList(); + + Furniture Bookcase; + Furniture Chair; + + public FurnitureCompany(Furniture Bookcase,Furniture Chair) { + this.Bookcase=Bookcase; + this.Chair=Chair; + + } + + public ArrayList getAllFurnitures() { + return this.allFurniture; + } + + +} diff --git a/FurnitureFrame.java b/FurnitureFrame.java new file mode 100644 index 0000000..499b97f --- /dev/null +++ b/FurnitureFrame.java @@ -0,0 +1,83 @@ +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; + +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JTextField; + +public class FurnitureFrame extends JFrame{ + + private JPanel panel = new JPanel(); + private JButton b1 = new JButton("Search Furniture"); + private JButton b2 = new JButton("Print Furniture to Text File"); + private JTextField t1 = new JTextField("Enter model"); + private JTextField t2 = new JTextField("Enter Category"); + + ArrayList allFurniture = new ArrayList(); + + public FurnitureFrame(ArrayList furnitures) { + + allFurniture=furnitures; + this.setVisible(true); + this.setTitle("Furniture Company"); + this.setSize(300,250); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + panel.add(t1); + panel.add(b1); + panel.add(t2); + panel.add(b2); + + this.setContentPane(panel); + + Collections.sort(furnitures); + + b1.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + String modelName =t1.getText(); + for(Furniture furniture: allFurniture) { + if(furniture.getModel().equals(modelName)) { + furniture.getInfo(); + } + else { + System.out.println("There is no furniture of model "+modelName); + } + } + } + }); + + b2.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + + String textCategory=t2.getText(); + File f = new File(textCategory+".txt"); + try { + FileWriter write = new FileWriter(f); + for(Furniture furniture:furnitures) { + if(furniture.chooseCategory().equals(textCategory)) { + + write.write(furniture.getInfo()); + write.write(System.lineSeparator()); + } + } + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + }); + + } + +} + diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..7d3b60f --- /dev/null +++ b/Main.java @@ -0,0 +1,14 @@ +import java.util.ArrayList; + +public class Main { + + public static void main(String[] args) { + + Furniture b1 = new Bookcase("Ikea",70,3,8); + Furniture c1 = new Chair("HermanMiller",40,2,"Grey"); + FurnitureCompany bestFurniture = new FurnitureCompany(b1,c1); + + new FurnitureFrame(bestFurniture.getAllFurnitures()); + } + +}