Initial Commit

This commit is contained in:
Anyx 2023-11-03 20:37:05 +02:00
commit 3821a5ca26
Signed by: a
GPG Key ID: 43D8165FD2251985
6 changed files with 264 additions and 0 deletions

47
Bookcase.java Normal file
View File

@ -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();
}
}

42
Chair.java Normal file
View File

@ -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();
}
}

57
Furniture.java Normal file
View File

@ -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<Furniture> allFurniture = new ArrayList<Furniture>();
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());
}
}

21
FurnitureCompany.java Normal file
View File

@ -0,0 +1,21 @@
import java.util.ArrayList;
public class FurnitureCompany{
private ArrayList<Furniture> allFurniture = new ArrayList<Furniture>();
Furniture Bookcase;
Furniture Chair;
public FurnitureCompany(Furniture Bookcase,Furniture Chair) {
this.Bookcase=Bookcase;
this.Chair=Chair;
}
public ArrayList<Furniture> getAllFurnitures() {
return this.allFurniture;
}
}

83
FurnitureFrame.java Normal file
View File

@ -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<Furniture> allFurniture = new ArrayList<Furniture>();
public FurnitureFrame(ArrayList<Furniture> 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();
}
}
});
}
}

14
Main.java Normal file
View File

@ -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());
}
}