From 7ac3b8accf8ef1df16fbec59a2c16418b25355a7 Mon Sep 17 00:00:00 2001 From: megadoxs Date: Tue, 27 Feb 2024 18:21:00 -0500 Subject: [PATCH] V0.1 --- .gitignore | 5 +- .idea/artifacts/Clicker_jar.xml | 8 + .idea/misc.xml | 1 - src/Building.java | 424 +++++++++++++++++++ src/GUI.java | 700 ++++++++++++++++++++++++++++++++ src/META-INF/MANIFEST.MF | 3 + src/Main.java | 104 ++++- src/Upgrade.java | 207 ++++++++++ 8 files changed, 1447 insertions(+), 5 deletions(-) create mode 100644 .idea/artifacts/Clicker_jar.xml create mode 100644 src/Building.java create mode 100644 src/GUI.java create mode 100644 src/META-INF/MANIFEST.MF create mode 100644 src/Upgrade.java diff --git a/.gitignore b/.gitignore index f68d109..1db8689 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,7 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +### this program ### +data/ \ No newline at end of file diff --git a/.idea/artifacts/Clicker_jar.xml b/.idea/artifacts/Clicker_jar.xml new file mode 100644 index 0000000..bc42e67 --- /dev/null +++ b/.idea/artifacts/Clicker_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/Clicker_jar + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 15cec4b..be55cdd 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/Building.java b/src/Building.java new file mode 100644 index 0000000..1418c94 --- /dev/null +++ b/src/Building.java @@ -0,0 +1,424 @@ +import java.text.DecimalFormat; +import java.util.ArrayList; + +public class Building { + private String name; + private String[] groups; + private String group; + private int level; + private double defaultProduction; + private double unitProduction; + private double defaultPrice; + private Building previousBuilding; + private boolean jobUpgrade; //unknown; + private boolean doubleUpgrade; //100% + private boolean tripleUpgrade; //200% + private boolean sameBuildingUpgrade; //1% per level + private boolean previousBuildingUpgrade; //1% per previous level + private boolean groupUpgrade; // 10% group + private String groupUpgrade10; + private ArrayList groupUpgrade50; // 50% groups upgrades + + public Building(String name, String group, String[] groups, double defaultProduction, double defaultPrice, Building previousBuilding) { // constructor with all upgrades + this.name = name; + this.group = group; + this.groups = groups; + this.defaultProduction = defaultProduction; + this.defaultPrice = defaultPrice; + this.previousBuilding = previousBuilding; + this.unitProduction = defaultProduction; + this.level = 0; + } + public Building(String name, String group, double defaultProduction, double defaultPrice, Building previousBuilding) { // constructor with all upgrades + this.name = name; + this.group = group; + this.defaultProduction = defaultProduction; + this.defaultPrice = defaultPrice; + this.previousBuilding = previousBuilding; + this.unitProduction = defaultProduction; + this.level = 0; + } + + public Building(String name, String group, String groups[], double defaultProduction, double defaultPrice) { // constructor without previous upgrade + this.name = name; + this.group = group; + this.groups = groups; + this.defaultProduction = defaultProduction; + this.defaultPrice = defaultPrice; + this.unitProduction = defaultProduction; + this.level = 0; + } + + public Building(String name, String group, double defaultProduction, double defaultPrice) { // constructor without previous upgrade + this.name = name; + this.group = group; + this.defaultProduction = defaultProduction; + this.defaultPrice = defaultPrice; + this.unitProduction = defaultProduction; + this.level = 0; + } + + public Building(Building building){ + this.name = building.getName(); + this.groups = building.getGroups(); + this.level = building.getLevel(); + this.defaultProduction = building.getDefaultProduction(); + this.defaultPrice = building.getDefaultPrice(); + this.previousBuilding = building.getPreviousBuilding(); + this.jobUpgrade = building.isJobUpgrade(); + this.doubleUpgrade = building.isDoubleUpgrade(); + this.tripleUpgrade = building.isTripleUpgrade(); + this.sameBuildingUpgrade = building.isSameBuildingUpgrade(); + this.previousBuildingUpgrade = building.isPreviousBuildingUpgrade(); + this.groupUpgrade = building.isGroupUpgrade(); + this.groupUpgrade50 = building.groupUpgrade50; + this.groupUpgrade10 = building.groupUpgrade10; + calculateUnitProduction(); + } + + public String getName() { + return name; + } + + public String[] getGroups() { + return groups; + } + + public String getGroup() { + return group; + } + + public void addGroupUpgrade50(String group){ + groupUpgrade50.add(group); + } + + public ArrayList getGroupUpgrade50() { + return groupUpgrade50; + } + + public void setGroupUpgrade50(ArrayList groupUpgrade50) { + this.groupUpgrade50 = groupUpgrade50; + } + + public boolean isJobUpgrade() { + return jobUpgrade; + } + + public void setJobUpgrade(boolean jobUpgrade) { + this.jobUpgrade = jobUpgrade; + } + + public int getLevel() { + return level; + } + + public void setLevel(int level) { + this.level = level; + } + + public double getDefaultProduction() { + return defaultProduction; + } + + public void setDefaultProduction(double defaultProduction) { + this.defaultProduction = defaultProduction; + } + + public String getFormattedUnitProduction() { + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,###,#00"); + if (this.unitProduction < 10) + return smallProd.format(this.unitProduction); + else + return bigProd.format(this.unitProduction); + } + + public double getUnitProduction() { + return this.unitProduction; + } + + public double getDefaultPrice() { + return defaultPrice; + } + + public void setDefaultPrice(double defaultPrice) { + this.defaultPrice = defaultPrice; + } + + public Building getPreviousBuilding() { + return previousBuilding; + } + + public void setPreviousBuilding(Building previousBuilding) { + this.previousBuilding = previousBuilding; + } + + public boolean isDoubleUpgrade() { + return doubleUpgrade; + } + + public void setDoubleUpgrade(boolean doubleUpgrade) { + this.doubleUpgrade = doubleUpgrade; + } + + public boolean isTripleUpgrade() { + return tripleUpgrade; + } + + public void setTripleUpgrade(boolean tripleUpgrade) { + this.tripleUpgrade = tripleUpgrade; + } + + public boolean isSameBuildingUpgrade() { + return sameBuildingUpgrade; + } + + public void setSameBuildingUpgrade(boolean sameBuildingUpgrade) { + this.sameBuildingUpgrade = sameBuildingUpgrade; + } + + public boolean isPreviousBuildingUpgrade() { + return previousBuildingUpgrade; + } + + public void setPreviousBuildingUpgrade(boolean previousBuildingUpgrade) { + this.previousBuildingUpgrade = previousBuildingUpgrade; + } + + public boolean isGroupUpgrade() { + return groupUpgrade; + } + + public void setGroupUpgrade(boolean groupUpgrade) { + this.groupUpgrade = groupUpgrade; + } + + public void addLevel(){ + this.level += 1; + } + + public void delLevel(){ + if(this.level > 0) + this.level -= 1; + } + + public double getUpgradePrice(){ + return this.defaultPrice*Math.pow(1.1, (this.getLevel())); + } + + public String getFormattedUpgradePrice(){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,#00"); + if (this.defaultPrice*Math.pow(1.1, (this.getLevel() + 1)) < 10) + return smallProd.format(this.defaultPrice*Math.pow(1.1, (this.getLevel()))); + else + return bigProd.format(this.defaultPrice*Math.pow(1.1, (this.getLevel()))); + } + + public String getFormattedProduction(){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,#00"); + if (this.unitProduction*this.level < 10) + return smallProd.format(this.unitProduction*this.level); + else + return bigProd.format(this.unitProduction*this.level); + } + + public double getProduction(){ + return this.unitProduction*this.level; + } + + public void calculateUnitProduction(){ + double production = this.defaultProduction; + if (this.doubleUpgrade) { + production += this.defaultProduction; + } + if (this.tripleUpgrade) { + production += this.defaultProduction; + } + if (this.previousBuildingUpgrade && this.previousBuilding != null) { + production += this.defaultProduction * 0.01 * this.previousBuilding.getLevel(); + } + if (this.sameBuildingUpgrade) { + production += this.defaultProduction * 0.01 * this.level; + } + if (this.groupUpgrade) { + production += this.defaultProduction * 0.1; + } + if (this.groupUpgrade50 != null){ + for(int i = 0; i < this.groupUpgrade50.size(); i++) { + production += this.defaultProduction * 0.5; + } + } + this.unitProduction = production; + } + + public static String getFormattedTotalProd(Building[] buildings){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,###,###,###,#00"); + double total = 0; + for (int i = 0; i < buildings.length; i++){ + total += buildings[i].getProduction(); + } + if (total < 10) + return smallProd.format(total); + else + return bigProd.format(total); + } + + public static double getTotalProd(Building[] buildings){ + double total = 0; + for (int i = 0; i < buildings.length; i++){ + total += buildings[i].getProduction(); + } + return total; + } + + public double getUpgradeValue(){ + return this.unitProduction/getUpgradePrice(); + } + + public static bestValue getBestPurchase(Building[] buildings){ + int index = 0; + double value = 0; + for (int i = 0; i < buildings.length; i++){ + if (buildings[i].getUpgradeValue() > value && (buildings[i].getPreviousBuilding() == null || buildings[i].getPreviousBuilding().getLevel() > 0)){ + value = buildings[i].getUpgradeValue(); + index = i; + } + } + return new bestValue(value, index); + } + + public static String getFormattedUpgradeBoost(Building building){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,#00"); + Building newBuilding = new Building(building); + newBuilding.addLevel(); + newBuilding.calculateUnitProduction(); + if (newBuilding.getUnitProduction() < 10) + return smallProd.format(newBuilding.getUnitProduction()); + else + return bigProd.format(newBuilding.getUnitProduction()); + } + + public static double getUpgradeBoost(Building building){ + Building newBuilding = new Building(building); + newBuilding.addLevel(); + newBuilding.calculateUnitProduction(); + return newBuilding.getUnitProduction(); + } + + public static String getFormattedNewProd(Building[] buildings, Building building){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,#00"); + double New = getTotalProd(buildings) + getUpgradeBoost(building); + if (New < 10) + return smallProd.format(New); + else + return bigProd.format(New); + } + + public static betterBuilding getBuildingByName(Building[] buildings, String name){ + Building building = null; + int index = 0; + for (int i = 0; i < buildings.length; i++){ + if(buildings[i].getName().equals(name)) { + building = buildings[i]; + index = i; + } + } + return new betterBuilding(building, index); + } + + public static Building[] getGroup(Building[] buildings, String group, boolean group50){ + ArrayList groupBuildings = new ArrayList<>(); + if(group50) + for (int i = 0; i < buildings.length; i++){ + if(buildings[i].getGroups() != null) + for (int j = 0; j < buildings[i].getGroups().length; j++){ + if(buildings[i].getGroups()[j].equals(group)){ + groupBuildings.add(buildings[i]); + } + } + } + else + for (int i = 0; i < buildings.length; i++){ + if(buildings[i].getGroup() != null) + if(buildings[i].getGroup().equals(group)) + groupBuildings.add(buildings[i]); + } + return groupBuildings.toArray(new Building[0]); + } + + public boolean hasUpgrade(String upgrade){ + switch(upgrade){ + case "Job": + return this.jobUpgrade; + case "100%": + return this.doubleUpgrade; + case "200%": + return this.tripleUpgrade; + case "1% per same building": + return this.sameBuildingUpgrade; + case "1% per previous building": + if (this.previousBuilding != null) + return this.previousBuildingUpgrade; + else + return true; + } + return true; + } + + public boolean hasUpgrade(String upgrade, String group){ + switch(upgrade){ + case "10% group": + return this.groupUpgrade; + case "50% group": + if(this.groupUpgrade50 != null){ + for (int i = 0; i < this.groupUpgrade50.size(); i++){ + if (this.groupUpgrade50.get(i).equals(group)) + return true; + } + } + return false; + } + return true; + } + + public static class bestValue { + private double value; + private int index; + + public bestValue(double value, int index){ + this.value = value; + this.index = index; + } + + public double getValue() { + return value; + } + + public int getIndex() { + return index; + } + } + + public static class betterBuilding { + private Building building; + private int index; + + public betterBuilding(Building building, int index){ + this.building = building; + this.index = index; + } + + public Building getBuilding() { + return building; + } + + public int getIndex() { + return index; + } + } +} diff --git a/src/GUI.java b/src/GUI.java new file mode 100644 index 0000000..8f52c1c --- /dev/null +++ b/src/GUI.java @@ -0,0 +1,700 @@ +import javax.swing.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import java.awt.*; +import java.awt.event.*; +import java.io.*; +import java.text.DecimalFormat; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Scanner; + +public class GUI extends JFrame{ + private final Building[] buildings; + private final String[] groups10 = new String[]{"mineur", "fermier", "alchimiste"}; + private final String[] groups50 = new String[]{"humanoide", "gelé", "eau"}; + private ArrayList upgrades; + private JTextField[] levels; + private JTextField[] prod; + private JTextField[] unitprod; + private JLabel[] statsValue; + private static AddUpgradeGUI addUpgradeGUI; + + public GUI(Building[] buildings, ArrayList upgrades){ + this.buildings = buildings; + this.upgrades = upgrades; + setGUI(); + } + + public GUI(Building[] buildings) throws HeadlessException { + this.buildings = buildings; + setGUI(); + } + + public void setGUI(){ + setTitle("Clicker"); + setVisible(true); + setLayout(new GridBagLayout()); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);; + addClosingListener(); + + //layouts + GridBagConstraints gbc = new GridBagConstraints(); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.insets = new Insets(2, 5, 2, 5); + + //header + JLabel[] header = new JLabel[4]; + header[0] = new JLabel("Buildings"); + header[1] = new JLabel("Levels"); + header[2] = new JLabel("Unit Prod/s"); + header[3] = new JLabel("Building Prod/s"); + header[1].setHorizontalAlignment(JTextField.CENTER); + for (int j = 0; j < header.length; j++){ + header[j].setFont(new Font(header[j].getFont().getName(), Font.PLAIN, 20)); + gbc.gridy = 0; + if (j > 2) + gbc.gridx = (j+2); + if (j == 1) + gbc.gridwidth = 3; + else gbc.gridwidth = 1; + add(header[j], gbc); + } + gbc.gridwidth = 1; + + //body + buildingAdd addListener = new buildingAdd(); + buildingDel delListener = new buildingDel(); + BetterButton[] addButtons = new BetterButton[35]; + BetterButton[] delButtons = new BetterButton[35]; + levels = new JTextField[35]; + prod = new JTextField[35]; + unitprod = new JTextField[35]; + JLabel[] buildingNames = new JLabel[35]; + for (int i = 0; i < addButtons.length; i++){ + //prod + prod[i] = new JTextField(this.buildings[i].getFormattedProduction()); + prod[i].setEditable(false); + prod[i].setPreferredSize(new Dimension(40,20)); + prod[i].setHorizontalAlignment(JTextField.CENTER); + + //unit prod + unitprod[i] = new JTextField(this.buildings[i].getFormattedUnitProduction()); + unitprod[i].setEditable(false); + unitprod[i].setPreferredSize(new Dimension(40,20)); + unitprod[i].setHorizontalAlignment(JTextField.CENTER); + + //levels + levels[i] = new JTextField(this.buildings[i].getLevel() + ""); + levels[i].setEditable(false); + levels[i].setPreferredSize(new Dimension(40,20)); + levels[i].setHorizontalAlignment(JTextField.CENTER); + + //button + addButtons[i] = new BetterButton("+1", i); + delButtons[i] = new BetterButton("-1", i); + addButtons[i].setMargin(new Insets(0, 0, 0, 0)); + delButtons[i].setMargin(new Insets(0, 0, 0, 0)); + addButtons[i].setPreferredSize(new Dimension(20, 20)); + delButtons[i].setPreferredSize(new Dimension(20, 20)); + + //buildings + buildingNames[i] = new JLabel(this.buildings[i].getName()); + gbc.gridy = (i+1); + Object[] body = new Object[]{buildingNames, levels, addButtons, delButtons, unitprod, prod}; + + //add + for (int j = 0; j < body.length; j++){ + if (j == 1) + gbc.insets = new Insets(2, 5, 2, 0); + else if (j == 2) + gbc.insets = new Insets(2, 0, 2, 0); + else if (j == 3) + gbc.insets = new Insets(2, 0, 2, 5); + else + gbc.insets = new Insets(2, 5, 2, 5); + gbc.gridx = j; + add(((Component[]) body[j])[i], gbc); + } + addButtons[i].addActionListener(addListener); + delButtons[i].addActionListener(delListener); + } + + //stats + JLabel stats = new JLabel("Stats"); + stats.setFont(new Font(stats.getFont().getName(), Font.PLAIN, 20)); + gbc.insets = new Insets(2, 30, 2, 5); + gbc.gridx = 6; + gbc.gridy = 0; + add(stats, gbc); + JLabel[] statsName = new JLabel[5]; + statsName[0] = new JLabel("Current Total Production:"); + statsName[1] = new JLabel("Best Purchase:"); + statsName[2] = new JLabel("Purchase Price:"); + statsName[3] = new JLabel("Production Boost:"); + statsName[4] = new JLabel("New Total Production:"); + for (int i = 0; i < statsName.length; i++){ + gbc.gridy = (i+1); + add(statsName[i], gbc); + } + gbc.insets = new Insets(2, 5, 2, 5); + gbc.gridx = 7; + statsValue = new JLabel[5]; + statsValue[0] = new JLabel(Building.getFormattedTotalProd(buildings)); + statsValue[1] = new JLabel(getBestPurchase()[0]); + statsValue[2] = new JLabel(getBestPurchase()[4]); + statsValue[3] = new JLabel(getBestPurchase()[2]); + statsValue[4] = new JLabel(getBestPurchase()[3]); + for (int i = 0; i < statsValue.length; i++){ + gbc.gridy = (i+1); + add(statsValue[i], gbc); + } + + //options + gbc.insets = new Insets(2, 30, 2, 5); + gbc.gridx = 6; + gbc.gridy = 6; + JLabel options = new JLabel("Options"); + options.setFont(new Font(options.getFont().getName(), Font.PLAIN, 20)); + add(options, gbc); + + gbc.gridy++; + purchaseRecommended purchaseListener = new purchaseRecommended(); + BetterButton purchaseRecommended = new BetterButton("Purchase Recommended"); + purchaseRecommended.setMargin(new Insets(0, 0, 0, 0)); + purchaseRecommended.addActionListener(purchaseListener); + add(purchaseRecommended, gbc); + + gbc.gridy++; + addUpgrade addUpgradeListener = new addUpgrade(); + JButton addUpgrade = new JButton("Add Upgrade"); + addUpgrade.setMargin(new Insets(0, 0, 0, 0)); + addUpgrade.addActionListener(addUpgradeListener); + add(addUpgrade, gbc); + + gbc.gridy++; + JButton upgradeList = new JButton("Upgrade List"); + upgradeList.setMargin(new Insets(0, 0, 0, 0)); + add(upgradeList, gbc); + + gbc.gridy++; + JButton showbuildingUpgrades = new JButton("See Buildings's Upgrades"); + showbuildingUpgrades.setMargin(new Insets(0, 0, 0, 0)); + add(showbuildingUpgrades, gbc); + + gbc.gridy++; + JButton reset = new JButton("Reset Data"); + reset.setMargin(new Insets(0, 0, 0, 0)); + add(reset, gbc); + + //pack + pack(); + } + + private class BetterButton extends JButton{ + private int id; + + public BetterButton(String label, int id) throws HeadlessException { + super(label); + this.id = id; + } + public BetterButton(String label) throws HeadlessException { + super(label); + } + + public int getId() { + return id; + } + + public void setId(int id){ + this.id = id; + } + } + + private void updateBuildings(ActionEvent evt){ + + //updates target building + GUI.this.levels[((BetterButton) evt.getSource()).getId()].setText(GUI.this.buildings[((BetterButton) evt.getSource()).getId()].getLevel() + ""); + GUI.this.buildings[((BetterButton) evt.getSource()).getId()].calculateUnitProduction(); + GUI.this.unitprod[((BetterButton) evt.getSource()).getId()].setText(GUI.this.buildings[((BetterButton) evt.getSource()).getId()].getFormattedUnitProduction()); + GUI.this.prod[((BetterButton) evt.getSource()).getId()].setText(GUI.this.buildings[((BetterButton) evt.getSource()).getId()].getFormattedProduction()); + + //updates next building + GUI.this.buildings[((BetterButton) evt.getSource()).getId()+1].calculateUnitProduction(); + GUI.this.unitprod[((BetterButton) evt.getSource()).getId()+1].setText(GUI.this.buildings[((BetterButton) evt.getSource()).getId()+1].getFormattedUnitProduction()); + GUI.this.prod[((BetterButton) evt.getSource()).getId()+1].setText(GUI.this.buildings[((BetterButton) evt.getSource()).getId()+1].getFormattedProduction()); + + updateStats(); + GUI.this.pack(); + } + + private void updateStats(){ + GUI.this.statsValue[0].setText(Building.getFormattedTotalProd(buildings)); + GUI.this.statsValue[1].setText(getBestPurchase()[0]); + GUI.this.statsValue[2].setText(getBestPurchase()[4]); + GUI.this.statsValue[3].setText(getBestPurchase()[2]); + GUI.this.statsValue[4].setText(getBestPurchase()[3]); + GUI.this.pack(); + } + + private String[] getBestPurchase(){ + String[] bestPurchase = new String[5]; + if (Building.getBestPurchase(buildings).getValue() > Upgrade.getBestPurchase(upgrades).getValue()){ + bestPurchase[0] = GUI.this.buildings[Building.getBestPurchase(buildings).getIndex()].getName(); + bestPurchase[1] = "Building"; + bestPurchase[2] = Building.getFormattedUpgradeBoost(buildings[Building.getBestPurchase(buildings).getIndex()]); + bestPurchase[3] = Building.getFormattedNewProd(buildings, GUI.this.buildings[Building.getBestPurchase(buildings).getIndex()]); + bestPurchase[4] = GUI.this.buildings[Building.getBestPurchase(buildings).getIndex()].getFormattedUpgradePrice(); + }else { + bestPurchase[0] = GUI.this.upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getName(); + bestPurchase[1] = "Upgrade"; + bestPurchase[2] = GUI.this.upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getFormattedUpgradeBoost(); + bestPurchase[3] = GUI.this.upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getFormattedNewProd(buildings); + bestPurchase[4] = GUI.this.upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getFormattedPrice(); + } + return bestPurchase; + } + + private class buildingDel implements ActionListener { + @Override + public void actionPerformed(ActionEvent evt) { + GUI.this.buildings[((BetterButton) evt.getSource()).getId()].delLevel(); + updateBuildings(evt); + updateStats(); + } + } + private class buildingAdd implements ActionListener { + @Override + public void actionPerformed(ActionEvent evt) { + GUI.this.buildings[((BetterButton) evt.getSource()).getId()].addLevel(); + updateBuildings(evt); + updateStats(); + } + } + + private class purchaseRecommended implements ActionListener { + @Override + public void actionPerformed(ActionEvent evt) { + if (getBestPurchase()[1].equals("Building")){ + ((BetterButton) evt.getSource()).setId(Building.getBestPurchase(GUI.this.buildings).getIndex()); + GUI.this.buildings[Building.getBestPurchase(GUI.this.buildings).getIndex()].addLevel(); + updateBuildings(evt); + } + else{ + try { + File iFile = new File("./data/upgrades.txt"); + Scanner inputFile = new Scanner(iFile); + File OFile = new File("temp.txt"); + PrintWriter outputFile = new PrintWriter(OFile); + int UpgradeIndex = Upgrade.getBestPurchase(upgrades).getIndex(); + + while (inputFile.hasNextLine()) { + String upgrade = inputFile.nextLine(); + if(!upgrade.equals(upgrades.get(UpgradeIndex).getName()+ "|" + upgrades.get(UpgradeIndex).getType() + "|" + upgrades.get(UpgradeIndex).getTargetName() + "|" + upgrades.get(UpgradeIndex).getPrice())){ + outputFile.println(upgrade); + } + } + + inputFile.close(); + outputFile.close(); + iFile.delete(); + OFile.renameTo(new File("./data/upgrades.txt")); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).applyUpgrade(); + if(upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getGroup() != null){ + for (int i = 0; i < upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getGroup().length; i++){ + ((BetterButton) evt.getSource()).setId(Building.getBuildingByName(buildings, upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getGroup()[i].getName()).getIndex()); + updateBuildings(evt); + } + } + else{ + ((BetterButton) evt.getSource()).setId(Building.getBuildingByName(buildings, upgrades.get(Upgrade.getBestPurchase(upgrades).getIndex()).getBuilding().getName()).getIndex()); + updateBuildings(evt); + } + } + updateStats(); + } + } + + private class addUpgrade implements ActionListener { + public void actionPerformed(ActionEvent evt) { + if (addUpgradeGUI == null) { + addUpgradeGUI = new AddUpgradeGUI(); + } else { + addUpgradeGUI.toFront(); + } + } + } + private void addClosingListener(){ + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + try { + PrintWriter outputFile = new PrintWriter("./data/buildings.txt"); + for (int i = 0; i < GUI.this.buildings.length; i++){ + outputFile.print(GUI.this.buildings[i].getLevel() + "|"); + outputFile.print(GUI.this.buildings[i].isJobUpgrade() + "|"); + outputFile.print(GUI.this.buildings[i].isDoubleUpgrade() + "|"); + outputFile.print(GUI.this.buildings[i].isTripleUpgrade() + "|"); + outputFile.print(GUI.this.buildings[i].isSameBuildingUpgrade() + "|"); + outputFile.print(GUI.this.buildings[i].isPreviousBuildingUpgrade() + "|"); + outputFile.print(GUI.this.buildings[i].isGroupUpgrade() + "|"); + if(GUI.this.buildings[i].getGroupUpgrade50() != null){ + for (int j = 0; j < GUI.this.buildings[i].getGroupUpgrade50().size(); j++) + outputFile.print(GUI.this.buildings[i].getGroupUpgrade50().get(j) + " "); + } + outputFile.println(); + } + outputFile.close(); + } catch (FileNotFoundException ex) { + throw new RuntimeException(ex); + } + + } + }); + } + + private class AddUpgradeGUI extends JFrame{ + + JTextField enterName; + JComboBox selectType; + JComboBox selectTarget; + NumericTextField enterPrice; + JLabel prodBoostValue = new JLabel(); + JLabel newTotalProd = new JLabel(); + JLabel isBestPurchase = new JLabel(); + public AddUpgradeGUI() throws HeadlessException { + //layout + setTitle("Add Upgrade"); + setVisible(true); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + addClosingListener(); + setLayout(new GridBagLayout()); + GridBagConstraints gbc = new GridBagConstraints(); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.insets = new Insets(5, 5, 5, 5); + + //page body + gbc.gridx = 0; + gbc.gridy = 0; + gbc.gridwidth = 2; + JLabel addUpgrade = new JLabel("New Upgrade"); + addUpgrade.setFont(new Font(addUpgrade.getFont().getName(), Font.PLAIN, 20)); + add(addUpgrade, gbc); + gbc.gridwidth = 1; + + gbc.gridy++; + JLabel name = new JLabel("Name:"); + add(name, gbc); + + gbc.gridy++; + JLabel type = new JLabel("Type:"); + add(type, gbc); + + gbc.gridy++; + JLabel target = new JLabel("Target:"); + add(target, gbc); + + gbc.gridy++; + JLabel price = new JLabel("Price:"); + add(price, gbc); + + gbc.gridx = 1; + gbc.gridy = 1; + enterName = new JTextField(); + add(enterName, gbc); + + gbc.gridy++; + selectType = new JComboBox<>(); + selectType.addItem("Job"); + selectType.addItem("100%"); + selectType.addItem("200%"); + selectType.addItem("1% per same building"); + selectType.addItem("1% per previous building"); + selectType.addItem("10% group"); + selectType.addItem("50% group"); + selectType.setSelectedItem(null); + add(selectType, gbc); + + + gbc.gridy++; + selectTarget = new JComboBox<>(); + add(selectTarget, gbc); + + gbc.gridy++; + enterPrice = new NumericTextField(); + add(enterPrice, gbc); + + gbc.gridy++; + gbc.gridx = 0; + gbc.gridwidth = 2; + JButton createUpgrade = new JButton("add Upgrade"); + createUpgrade.setHorizontalAlignment(JTextField.CENTER); + add(createUpgrade, gbc); + + //stats + gbc.insets = new Insets(5, 30, 5, 5); + gbc.gridy = 0; + gbc.gridx = 2; + JLabel stats = new JLabel("Stats"); + stats.setFont(new Font(addUpgrade.getFont().getName(), Font.PLAIN, 20)); + add(stats, gbc); + gbc.gridwidth = 1; + + gbc.gridy++; + JLabel totalProd = new JLabel("Current Total Production:"); + add(totalProd, gbc); + + gbc.gridy++; + JLabel prodBoost = new JLabel("Production Boost:"); + add(prodBoost, gbc); + + gbc.gridy++; + JLabel prodTotal = new JLabel("New Total Production:"); + add(prodTotal, gbc); + + gbc.gridy++; + JLabel isBestPurchase = new JLabel("Best Purchase?"); + add(isBestPurchase, gbc); + + + gbc.insets = new Insets(5, 5, 5, 5); + gbc.gridy = 1; + gbc.gridx = 3; + JLabel totalProdValue = new JLabel(Building.getFormattedTotalProd(GUI.this.buildings)); + add(totalProdValue, gbc); + + gbc.insets = new Insets(2, 30, 2, 5); + gbc.gridx = 4; + gbc.gridy = 0; + JLabel options = new JLabel("Options"); + options.setFont(new Font(options.getFont().getName(), Font.PLAIN, 20)); + add(options, gbc); + + gbc.gridy++; + JButton upgradeList = new JButton("Upgrade List"); + upgradeList.setMargin(new Insets(0, 0, 0, 0)); + add(upgradeList, gbc); + + gbc.gridy++; + JButton showbuildingUpgrades = new JButton("See Buildings's Upgrades"); + showbuildingUpgrades.setMargin(new Insets(0, 0, 0, 0)); + add(showbuildingUpgrades, gbc); + + //listeners + + class updateUpgrade implements ActionListener { + @Override + public void actionPerformed(ActionEvent evt) { + updateStats(); + } + } + + DocumentListener textFieldListener = new DocumentListener() { + @Override + public void insertUpdate(DocumentEvent e) { + updateStats(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + updateStats(); + } + + @Override + public void changedUpdate(DocumentEvent e) {} + }; + + updateUpgrade updateUpgradeListener = new updateUpgrade(); + enterName.getDocument().addDocumentListener(textFieldListener); + selectType.addActionListener(updateUpgradeListener); + selectTarget.addActionListener(updateUpgradeListener); + + + selectType.addActionListener(new ActionListener() { // need to remove the possibility to select an a target that already has that upgrade + public void actionPerformed(ActionEvent e) { + if (selectType.getSelectedItem() != null && selectType.getSelectedItem().toString().equals("10% group")){ + selectTarget.removeAllItems(); + for (int i = 0; i < GUI.this.groups10.length; i++){ + if(!Building.getGroup(buildings, GUI.this.groups10[i], false)[0].hasUpgrade("10% group", GUI.this.groups10[i]) && !Upgrade.upgradeExist(GUI.this.upgrades, GUI.this.groups10[i], selectType.getSelectedItem().toString())) + selectTarget.addItem(GUI.this.groups10[i]); + } + selectTarget.setSelectedItem(null); + } + else if (selectType.getSelectedItem() != null && selectType.getSelectedItem().toString().equals("50% group")){ + selectTarget.removeAllItems(); + for (int i = 0; i < GUI.this.groups50.length; i++){ + if(!Building.getGroup(buildings, GUI.this.groups50[i], true)[0].hasUpgrade("50% group", GUI.this.groups50[i]) && !Upgrade.upgradeExist(GUI.this.upgrades, GUI.this.groups50[i], selectType.getSelectedItem().toString())) + selectTarget.addItem(GUI.this.groups50[i]); + } + selectTarget.setSelectedItem(null); + } + if (selectType.getSelectedItem() != null && selectType.getSelectedItem().toString().equals("200%")){ + selectTarget.removeAllItems(); + for (int i = 0; i < GUI.this.buildings.length; i++){ + if(GUI.this.buildings[i].getLevel() > 0 && !GUI.this.buildings[i].hasUpgrade("200%") && GUI.this.buildings[i].hasUpgrade("100%") && !Upgrade.upgradeExist(GUI.this.upgrades, buildings[i].getName(), selectType.getSelectedItem().toString())) + selectTarget.addItem(GUI.this.buildings[i].getName()); + } + selectTarget.setSelectedItem(null); + } + else if (selectType.getSelectedItem() != null) { + selectTarget.removeAllItems(); + for (int i = 0; i < GUI.this.buildings.length; i++){ + if(GUI.this.buildings[i].getLevel() > 0 && !GUI.this.buildings[i].hasUpgrade(selectType.getSelectedItem().toString()) && !Upgrade.upgradeExist(GUI.this.upgrades, buildings[i].getName(), selectType.getSelectedItem().toString())) + selectTarget.addItem(GUI.this.buildings[i].getName()); + } + selectTarget.setSelectedItem(null); + } + else { + selectTarget.removeAllItems(); + } + } + }); + + createUpgrade.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if(!enterName.getText().trim().isEmpty() && selectType.getSelectedItem() != null && selectTarget.getSelectedItem() != null && !enterPrice.getText().trim().isEmpty()){ + try { + PrintWriter outputFile = new PrintWriter(new FileWriter("./data/upgrades.txt", true)); + Scanner inputFile = new Scanner(new File("./data/upgrades.txt")); + if(inputFile.hasNext()) + outputFile.println(); + outputFile.print(enterName.getText() + "|"); + outputFile.print(selectType.getSelectedItem().toString() + "|"); + if(selectType.getSelectedItem().toString().equals("50% group")){ + GUI.this.upgrades.add(new Upgrade(enterName.getText(), selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), true), Integer.parseInt(enterPrice.getText().replaceAll("[^0-9]", "")))); + outputFile.print(selectTarget.getSelectedItem().toString() + "|"); + } + else if(selectType.getSelectedItem().toString().equals("10% group")){ + GUI.this.upgrades.add(new Upgrade(enterName.getText(), selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), false), Integer.parseInt(enterPrice.getText().replaceAll("[^0-9]", "")))); + outputFile.print(selectTarget.getSelectedItem().toString() + "|"); + } + else { + GUI.this.upgrades.add(new Upgrade(enterName.getText(), selectType.getSelectedItem().toString(), Building.getBuildingByName(GUI.this.buildings, selectTarget.getSelectedItem().toString()).getBuilding(), Integer.parseInt(enterPrice.getText().replaceAll("[^0-9]", "")))); + outputFile.print(selectTarget.getSelectedItem().toString() + "|"); + } + outputFile.print(Integer.parseInt(enterPrice.getText().replaceAll("[^0-9]", ""))); + inputFile.close(); + outputFile.close(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + enterName.setText(""); + enterPrice.setText(""); + selectType.setSelectedItem(null); + GUI.this.updateStats(); + } + } + }); + + //pack + pack(); + } + + public void updateStats(){ + GridBagConstraints gbc = new GridBagConstraints(); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.insets = new Insets(5, 5, 5, 5); + gbc.gridy = 2; + gbc.gridx = 3; + // upgrade boost & New prod + if(selectType.getSelectedItem() != null && selectTarget.getSelectedItem() != null){ + if(selectType.getSelectedItem().toString().equals("50% group")){ + AddUpgradeGUI.this.prodBoostValue.setText((new Upgrade("", selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), true), 0)).getFormattedUpgradeBoost()); + AddUpgradeGUI.this.newTotalProd.setText((new Upgrade("", selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), true), 0)).getFormattedNewProd(GUI.this.buildings)); + } + else if(selectType.getSelectedItem().toString().equals("10% group")){ + AddUpgradeGUI.this.prodBoostValue.setText((new Upgrade("", selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), false), 0)).getFormattedUpgradeBoost()); + AddUpgradeGUI.this.newTotalProd.setText((new Upgrade("", selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), false), 0)).getFormattedNewProd(GUI.this.buildings)); + } + else { + AddUpgradeGUI.this.prodBoostValue.setText(new Upgrade("", selectType.getSelectedItem().toString(), Building.getBuildingByName(GUI.this.buildings, selectTarget.getSelectedItem().toString()).getBuilding(), 0).getFormattedUpgradeBoost()); + AddUpgradeGUI.this.newTotalProd.setText(new Upgrade("", selectType.getSelectedItem().toString(), Building.getBuildingByName(GUI.this.buildings, selectTarget.getSelectedItem().toString()).getBuilding(), 0).getFormattedNewProd(GUI.this.buildings)); + } + AddUpgradeGUI.this.add(AddUpgradeGUI.this.prodBoostValue, gbc); + gbc.gridy++; + AddUpgradeGUI.this.add(AddUpgradeGUI.this.newTotalProd, gbc); + } + else { + if(AddUpgradeGUI.this.prodBoostValue != null && AddUpgradeGUI.this.newTotalProd != null) { + AddUpgradeGUI.this.prodBoostValue.setText(""); + AddUpgradeGUI.this.newTotalProd.setText(""); + } + } + // is best purchase + if(selectType.getSelectedItem() != null && selectTarget.getSelectedItem() != null && !enterPrice.getText().trim().isEmpty()){ + // upgrade boost + if(selectType.getSelectedItem().toString().equals("50% group")){ + AddUpgradeGUI.this.isBestPurchase.setText(Boolean.toString((new Upgrade("", selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), true), Integer.parseInt(enterPrice.getText().replaceAll("[^0-9]", "")))).isBestPurchase(GUI.this.upgrades, GUI.this.buildings))); + } + else if(selectType.getSelectedItem().toString().equals("10% group")){ + AddUpgradeGUI.this.isBestPurchase.setText(Boolean.toString((new Upgrade("", selectType.getSelectedItem().toString(), selectTarget.getSelectedItem().toString(), Building.getGroup(GUI.this.buildings, selectTarget.getSelectedItem().toString(), false), Integer.parseInt(enterPrice.getText().replaceAll("[^0-9]", "")))).isBestPurchase(GUI.this.upgrades, GUI.this.buildings))); + } + else { + AddUpgradeGUI.this.isBestPurchase.setText(Boolean.toString(new Upgrade("", selectType.getSelectedItem().toString(), Building.getBuildingByName(GUI.this.buildings, selectTarget.getSelectedItem().toString()).getBuilding(), Integer.parseInt(enterPrice.getText().replaceAll("[^0-9]", ""))).isBestPurchase(GUI.this.upgrades, GUI.this.buildings))); + } + gbc.gridy++; + AddUpgradeGUI.this.add(AddUpgradeGUI.this.isBestPurchase, gbc); + } + else { + if(AddUpgradeGUI.this.isBestPurchase != null) { + AddUpgradeGUI.this.isBestPurchase.setText(""); + } + } + AddUpgradeGUI.this.pack(); + } + + public class NumericTextField extends JTextField { + private DecimalFormat formatter; + + public NumericTextField() { + super(); + + formatter = new DecimalFormat("#,###"); + formatter.setGroupingSize(3); + formatter.setParseIntegerOnly(true); + + addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent e) { + char c = e.getKeyChar(); + if (!(Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)) { + e.consume(); + } + } + + @Override + public void keyPressed(KeyEvent e) { + } + + @Override + public void keyReleased(KeyEvent e) { + formatText(); + AddUpgradeGUI.this.updateStats(); + } + }); + } + + private void formatText() { + try { + String text = getText().replaceAll("\\s", ""); + long value = formatter.parse(text).longValue(); + String formattedText = formatter.format(value); + setText(formattedText); + AddUpgradeGUI.this.updateStats(); + } catch (ParseException e) {} + } + } + + private void addClosingListener() { + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + addUpgradeGUI = null; + } + }); + } + } +} diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/Main.java b/src/Main.java index 3e59c38..f5ab5f9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,103 @@ +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); + public static void main(String[] args) throws IOException { + + // add all buildings + Building[] Buildings = new Building[35]; + Buildings[0] = new Building("Mine abandonnée", "mineur", 0.1, 1250); + Buildings[1] = new Building("Caverne aux gros cailloux", "mineur", 0.18, 3500, Buildings[0]); + Buildings[2] = new Building("Mine des nains", "mineur", new String[]{"humanoide"}, 0.324, 6000, Buildings[1]); + Buildings[3] = new Building("Jardin de mamie", "fermier", 0.584, 12500); + Buildings[4] = new Building("Cabane de sorcière", "alchimiste", new String[]{}, 1.06, 27500, Buildings[3]); + Buildings[5] = new Building("Ruche Bourdonnate", "alchimiste", new String[]{}, 1.89, 67500, Buildings[4]); + Buildings[6] = new Building("Foret enneigé", "alchimiste", new String[]{"gelé"}, 3.4, 125000, Buildings[5]); + Buildings[7] = new Building("Banquise gelée", "", new String[]{"gelé", "eau"}, 6.12, 315000, Buildings[6]); + Buildings[8] = new Building("Port de pirate", "", new String[]{"humanoide", "eau"}, 11, 650000, Buildings[7]); + Buildings[9] = new Building("Ile volcanique", "fermier", new String[]{}, 20, 1574000, Buildings[8]); + Buildings[10] = new Building("Dune caniculaire", "", new String[]{}, 36, 3150000, Buildings[9]); + Buildings[11] = new Building("Forêt mystique", "", new String[]{}, 64, 6000000, Buildings[10]); + Buildings[12] = new Building("Jungle luxuriante", "", new String[]{}, 115, 11500000, Buildings[11]); + Buildings[13] = new Building("", "", new String[]{}, 0, 0); + Buildings[14] = new Building("", "", new String[]{}, 0, 0); + Buildings[15] = new Building("", "", new String[]{}, 0, 0); + Buildings[16] = new Building("", "", new String[]{}, 0, 0); + Buildings[17] = new Building("", "", new String[]{}, 0, 0); + Buildings[18] = new Building("", "", new String[]{}, 0, 0); + Buildings[19] = new Building("", "", new String[]{}, 0, 0); + Buildings[20] = new Building("", "", new String[]{}, 0, 0); + Buildings[21] = new Building("", "", new String[]{}, 0, 0); + Buildings[22] = new Building("", "", new String[]{}, 0, 0); + Buildings[23] = new Building("", "", new String[]{}, 0, 0); + Buildings[24] = new Building("", "", new String[]{}, 0, 0); + Buildings[25] = new Building("", "", new String[]{}, 0, 0); + Buildings[26] = new Building("", "", new String[]{}, 0, 0); + Buildings[27] = new Building("", "", new String[]{}, 0, 0); + Buildings[28] = new Building("", "", new String[]{}, 0, 0); + Buildings[29] = new Building("", "", new String[]{}, 0, 0); + Buildings[30] = new Building("", "", new String[]{}, 0, 0); + Buildings[31] = new Building("", "", new String[]{}, 0, 0); + Buildings[32] = new Building("", "", new String[]{}, 0, 0); + Buildings[33] = new Building("", "", new String[]{}, 0, 0); + Buildings[34] = new Building("", "", new String[]{}, 0, 0); + + + //makes the dir to store the data txt files + Path path = Paths.get("./data"); + if (Files.exists(path) && Files.isDirectory(path)) { + System.out.println("Directory already exists at: " + path); + } else { + try { + Files.createDirectories(path); + } catch (IOException ignored) {} + } + + // check in the file for the values + File file = new File("./data/buildings.txt"); + if (file.exists()){ + Scanner inputFile = new Scanner(file); + for (int i = 0; i < 35; i++){ + String[] line = inputFile.nextLine().split("\\|"); + Buildings[i].setLevel(Integer.parseInt(line[0])); + Buildings[i].setJobUpgrade(Boolean.parseBoolean(line[1])); + Buildings[i].setDoubleUpgrade(Boolean.parseBoolean(line[2])); + Buildings[i].setTripleUpgrade(Boolean.parseBoolean(line[3])); + Buildings[i].setSameBuildingUpgrade(Boolean.parseBoolean(line[4])); + Buildings[i].setPreviousBuildingUpgrade(Boolean.parseBoolean(line[5])); + Buildings[i].setGroupUpgrade(Boolean.parseBoolean(line[6])); + + if (line.length == 8){ + String[] group = line[7].split(" "); + ArrayList groups = new ArrayList<>(Arrays.asList(group)); + Buildings[i].setGroupUpgrade50(groups); + } + Buildings[i].calculateUnitProduction(); + } + } + + File file2 = new File("./data/upgrades.txt"); + ArrayList upgrades = new ArrayList<>(); + if (file2.exists()){ + Scanner inputFile = new Scanner(file2); + while (inputFile.hasNextLine()){ + String[] line; + line = inputFile.nextLine().split("\\|"); + if (line[1].equals("50% group")){ + upgrades.add(new Upgrade(line[0], line[1], line[2], Building.getGroup(Buildings, line[2], true), Integer.parseInt(line[3]))); + }else + upgrades.add(new Upgrade(line[0], line[1], Building.getBuildingByName(Buildings, line[2]).getBuilding(), Integer.parseInt(line[3]))); + } + inputFile.close(); + } + System.out.println(); + GUI clicker = new GUI(Buildings, upgrades); } -} \ No newline at end of file +} + diff --git a/src/Upgrade.java b/src/Upgrade.java new file mode 100644 index 0000000..738fe24 --- /dev/null +++ b/src/Upgrade.java @@ -0,0 +1,207 @@ +import java.text.DecimalFormat; +import java.util.ArrayList; + +public class Upgrade { + private final String name; + private final String type; + private Building building; + private Building[] group; + private String groupName; + + private boolean multiple; + private final int price; + + public Upgrade(String name, String type, Building building, int price) { + this.name = name; + this.type = type; + this.building = building; + this.price = price; + this.multiple = false; + } + + public Upgrade(String name, String type, String groupName, Building[] group, int price) { + this.name = name; + this.type = type; + this.groupName = groupName; + this.group = group; + this.price = price; + this.multiple = true; + } + + public String getName() { + return name; + } + + public String getType() { + return type; + } + + public int getPrice() { + return price; + } + + public String getFormattedPrice(){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,#00"); + if (this.price < 10) + return smallProd.format(this.price); + else + return bigProd.format(this.price); + } + + public Building getBuilding() { + return building; + } + + public String getTargetName() { + if(this.multiple){ + return this.groupName; + }else + return this.building.getName(); + } + + + public Building[] getGroup() { + return group; + } + + public void applyUpgrade(){ + switch (type){ + case "Job": + this.building.setJobUpgrade(true); + break; + case "100%": + this.building.setDoubleUpgrade(true); + break; + case "200%": + this.building.setTripleUpgrade(true); + break; + case "1% per same building": + this.building.setSameBuildingUpgrade(true); + break; + case "1% per previous building": + this.building.setPreviousBuildingUpgrade(true); + break; + case "10% group": + for (int i = 0; i < this.group.length; i++){ + this.group[i].setGroupUpgrade(true); + } + break; + case "50% group": + for (int i = 0; i < this.group.length; i++){ + this.group[i].addGroupUpgrade50(this.groupName); + } + break; + } + } + + public String getFormattedNewProd(Building[] buildings){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,#00"); + double newProd = Building.getTotalProd(buildings) + getUpgradeBoost(); + if (newProd < 10) + return smallProd.format(newProd); + else + return bigProd.format(newProd); + } + + public String getFormattedUpgradeBoost(){ + DecimalFormat smallProd = new DecimalFormat("#0.00"); + DecimalFormat bigProd = new DecimalFormat("#,#00"); + double boost = getUpgradeBoost(); + if (boost < 10) + return smallProd.format(boost); + else + return bigProd.format(boost); + } + + public double getUpgradeBoost(){ + double total = 0; + if(this.multiple){ + Building[] group = new Building[this.group.length]; + for (int i = 0; i < this.group.length; i++){ + group[i] = new Building(this.group[i]); + } + switch (type){ + case "10% group": + for (int i = 0; i < this.group.length; i++){ + group[i].setGroupUpgrade(true); + } + break; + case "50% group 1": + for (int i = 0; i < this.group.length; i++){ + group[i].addGroupUpgrade50(this.groupName); + } + break; + } + for (int i = 0; i < group.length; i++){ + group[i].calculateUnitProduction(); + total += group[i].getProduction() - this.group[i].getProduction(); + } + }else { + Building building = new Building(this.building); + switch (type){ + case "Job": + building.setJobUpgrade(true); + break; + case "100%": + building.setDoubleUpgrade(true); + break; + case "200%": + building.setTripleUpgrade(true); + break; + case "1% per same building": + building.setSameBuildingUpgrade(true); + break; + case "1% per previous building": + building.setPreviousBuildingUpgrade(true); + break; + } + building.calculateUnitProduction(); + total += building.getProduction() - this.building.getProduction(); + } + return total; + } + + public double getUpgradeValue(){ + return getUpgradeBoost()/this.price; + } + + public static Building.bestValue getBestPurchase(ArrayList upgrades){ + int index = 0; + double value = 0; + for (int i = 0; i < upgrades.size(); i++){ + if (upgrades.get(i).getUpgradeValue() > value){ + value = upgrades.get(i).getUpgradeValue(); + index = i; + } + } + return new Building.bestValue(value, index); + } + + public static Boolean upgradeExist(ArrayList upgrades, String target, String type){ + for (int i = 0; i < upgrades.size(); i++){ + if(target.equals(upgrades.get(i).getTargetName()) && type.equals(upgrades.get(i).getType())) + return true; + } + return false; + } + + public boolean isBestPurchase(ArrayList upgrades, Building[] buildings){ + double value = 0; + for (int i = 0; i < upgrades.size(); i++){ + if (upgrades.get(i).getUpgradeValue() > value){ + value = upgrades.get(i).getUpgradeValue(); + } + } + for (int i = 0; i < buildings.length; i++){ + if (buildings[i].getUpgradeValue() > value){ + value = buildings[i].getUpgradeValue(); + } + } + if (this.getUpgradeValue() > value) + return true; + else + return false; + } +}