# Programming - Java All-In-One Exercise Extended (Solution)

I managed to fix some things so here the Solution to this Exercise. Here you can find the previous Exercise and here it's Solution. So, without further do, here comes the Solution.

# Solution:

I already explained in the Exercise Post what I did to create all this.

Just to make things clear:

• There could be Bugs or Things everywhere, cause it is very difficult to test so many different things!
• You don't have to do everything exactly as I did it!
• This is just an example Solution and many things were edited very fast!
• This Exercise is not the exact same I had to do in my University, so I had to rewrite many things and it took a lot of time, so if anything doesn't work it is because I did some things fast, so that I can start out with different things for the upcoming days!

So, after this big shout out. Here the Coding :)

Address, Person, Owner and Administrator are the exact same as last time!

import java.io.Serializable;
private String city;
private String street;
private int number;
private int zip;
// CONSTRUCTORS
city = null;
street = null;
number = -1;
zip = -1;
}
public Address(String city, String street, int number, int zip) {
this.city = city;
this.street = street;
this.number = number;
this.zip = zip;
}
// GETTERS-SETTERS
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public int getNumber() {
return number;
}
public void setStreetNumber(int number) {
this.number = number;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
// METHODS
public String toString() {
return city + "\n" + street + "\n" + number + " " + zip;
}
}

Person:

import java.io.Serializable;
public class Person implements Serializable { // Person.java
private int id;
private String name;
private String surname;
// CONSTRUCTORS
public Person() {
id = -1;
name = null;
surname = null;
a = null;
}

public Person(int id, String name, String surname, Address a) {

this.id = id;
this.name = name;
this.surname = surname;
this.a = a;
}
// GETTERS-SETTERS
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
return a;
}
this.a = a;
}
// METHODS
public String toString() {
return id + "\n" + name + "\n" + surname + "\n" + a.toString();
}
}

Owner:

import java.io.Serializable;
public class Owner extends Person implements Serializable { // Owner.java
private Apartment apartment;
// CONSTRUCTORS
public Owner() {
super();
}
public Owner(int id, String name, String surname, Address a) {
super(id, name, surname, a);
}
// GETTERS-SETTERS
public Apartment getApartment() {
return apartment;
}
public void setApartment(Apartment apartment) {
this.apartment = apartment;
}
// METHODS
public String toString() {
return super.toString();
}
}

import java.io.Serializable;
private Condominium condo;
// CONSTRUCTORS
super();
}
super(id, name, surname, a);
}
// GETTERS-SETTERS
public Condominium getCondo() {
return condo;
}
public void setCondo(Condominium condo) {
this.condo = condo;
}
// METHODS
public String toString() {
return super.toString();
}
}

Apartment:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Apartment implements Serializable { // Apartment.java
private int condominiumID;
private String apartmentID;
ArrayList<Owner> owners = new ArrayList<>();
private int squareMeters;
private int millRate;
private double propertyCharges;
// CONSTRUCTORS
public Apartment() {
condominiumID = -1;
apartmentID = null;
squareMeters = -1;
millRate = 0;
propertyCharges = 0.0;
}
public Apartment(int condominiumID, String apartmentID, int squareMeters) {
this.condominiumID = condominiumID;
this.apartmentID = apartmentID;
this.squareMeters = squareMeters;
millRate = calculateMillRate();
propertyCharges = 0;
}
public Apartment(int condominiumID, String apartmentID, int squareMeters, int millRate, double propertyCharges) {
this.condominiumID = condominiumID;
this.apartmentID = apartmentID;
this.squareMeters = squareMeters;
this.millRate = millRate;
this.propertyCharges = propertyCharges;
}
// GETTERS-SETTERS
public int getCondominiumID() {
return condominiumID;
}
public void setCondominiumID(int condominiumID) {
this.condominiumID = condominiumID;
}
public String getApartmentID() {
return apartmentID;
}
public void setApartmentID(String apartmentID) {
this.apartmentID = apartmentID;
}
public int getSquareMeters() {
return squareMeters;
}
public void setSquareMeters(int squareMeters) {
this.squareMeters = squareMeters;
}
public int getMillRate() {
return millRate;
}
public void setMillRate(int millRate) {
this.millRate = millRate;
}
public double getPropertyCharges() {
return propertyCharges;
}
public void setPropertyCharges(double propertyCharges) {
this.propertyCharges = propertyCharges;
}
// METHODS
public int calculateMillRate() {
int apartmentMilm;
// find condo in which the apartment belongs
Condominium c = null;
for (int i = 0; i < Driver.driver.condos.size(); i++) {
if (Driver.driver.condos.get(i).getId() == condominiumID) {
c = Driver.driver.condos.get(i);
}
}
int N = c.apartments.size();
if (N == 0) {
return 1000;
}
double[] weight = new double[N + 1];
// loop all apartments to calculate weight
for (int i = 0; i < N; i++) {
weight[i] = c.apartments.get(i).getSquareMeters();
// we use an object that does the same as strtok() in C
// and splits the String where a specific character appears
StringTokenizer strTok = new StringTokenizer(c.apartments.get(i).getApartmentID(), "_");
// we get the first "number" that represents the floor
// and convert it to an Integer
int floor = Integer.parseInt(strTok.nextToken());
weight[i] = weight[i] * (1 + ((floor - 1) * 0.2));
}
// the new apartment
StringTokenizer strTok = new StringTokenizer(apartmentID, "_");
int floor = Integer.parseInt(strTok.nextToken());
weight[N] = squareMeters * (1 + ((floor - 1) * 0.2));
// calculate sum of weights
double sumWeight = 0;
for (int i = 0; i < N + 1; i++) {
sumWeight += weight[i];
}
// recalculate millRate of all apartments
for (int i = 0; i < N; i++) {
c.apartments.get(i).setMillRate((int) ((weight[i] / sumWeight)));
}
// calculate apartment's millRate
apartmentMilm = (int) ((weight[N] / sumWeight));
return apartmentMilm;
}
public String toString() {
return condominiumID + "\n" + apartmentID + "\n" + squareMeters + " " + millRate + " " + propertyCharges;
}
}

Condominium:

import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Condominium implements Serializable { // Condominium.java
private int id;
ArrayList<Owner> owners = new ArrayList<>();
ArrayList<Apartment> apartments = new ArrayList<>();
private double expenditure;
// CONSTRUCTORS
public Condominium() {
id = -1;
a = null;
expenditure = 0.0;
}
public Condominium(int id, Address a) {
this.id = id;
this.a = a;
expenditure = 0.0;
}
// GETTERS-SETTERS
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
return a;
}
this.a = a;
}
// expenditure will be inputed with another method
// METHODS
public boolean findApartment(String apartmentcode) {
for (int i = 0; i < apartments.size(); i++) {
// checking equality of strings
if (apartmentcode.equals(apartments.get(i).getApartmentID())) {
JOptionPane.showMessageDialog(null, "Apartment:\n" + apartments.get(i));
return true;
}
}
return false;
}
public void printApartments() {
String temp = "Condo " + id + " has:\n";
for (int i = 0; i < apartments.size(); i++) {
temp += apartments.get(i) + "\n";
}
JOptionPane.showMessageDialog(null, temp);
}
public void setCondominiumPropertyCharges(int condominiumCode, double expenditure) {
if (condominiumCode == this.id) {
this.expenditure = expenditure;
} else {
JOptionPane.showMessageDialog(null, "No Condominium has the id " + condominiumCode);
}
}
public void apartmentPropertyCharges() {
for (int i = 0; i < apartments.size(); i++) {
apartments.get(i).setPropertyCharges(expenditure * apartments.get(i).getMillRate() / 1000);
}
}
public String toString() {
return id + "\n" + a.toString() + "\n" + expenditure;
}
}

Now the Last 3 Are for the GUI

Driver:

import java.awt.GridLayout;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
public class Driver extends JFrame { // Driver.java
ArrayList<Condominium> condos = new ArrayList<>();
static Driver driver;
public static void main(String[] args) {
driver = new Driver("Condominium Programm");
}
// CONSTRUCTOR
public Driver(String title) {
// set up frame
this.setTitle(title);
this.setSize(500, 700);
this.setResizable(false);
this.setLocationByPlatform(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(14, 1));
// set up Custom "Choice" Buttons
Choice[] choices = new Choice[14];
for (int i = 0; i < choices.length; i++) {
choices[i] = new Choice();
choices[i].setName(Integer.toString(i));
}
choices[1].setText("2. Insert Owner/Apartment");
choices[3].setText("4. Add an Owner to an Apartment");
choices[4].setText("5. Print All Apartments");
choices[5].setText("6. Find Apartment using Code");
choices[6].setText("7. Find Property charges of specific Owner");
choices[7].setText("8. Calculate Expenditure of all Condos");
choices[8].setText("9. Calculate/Print Property charges of all Apartments");
choices[9].setText("10. Save to Text File");
choices[11].setText("12. Save to Serializable File");
choices[13].setText("0. Exit");
for (int i = 0; i < choices.length; i++) {
}
this.setVisible(true);
}
}

Choice:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class Choice extends JButton implements ActionListener {
// CONSTRUCTOR
Choice() {
}
@Override
public void actionPerformed(ActionEvent e) {
// get name of Button and run specified action
switch (this.getName()) {
// on all those Cases we create an "Input" JDialog Window
case "1": // Insert Owner/Apartment
case "3": // Add an Owner to an Apartment"
new Input(this.getName());
break;
case "4": // Print All Apartments
printApartments();
break;
case "5": // Find Apartment using Code
String apartmentcode = JOptionPane.showInputDialog("Give Apartment Code");
findApartmentCharges(apartmentcode);
break;
case "6": // Find Property charges of specific Owner
int id = Integer.parseInt(JOptionPane.showInputDialog("Give an Owner ID"));
findPropertyCharges(id);
break;
// in this 2 Cases we create an "Input" JDialog Window
case "7": // Calculate Expenditure of all Condos
case "8": // Calculate/Print Property charges of all Apartments
new Input(this.getName());
break;
case "9": // Save to Text File
writeToTextFile();
break;
case "10": // Load from Text File
break;
case "11": // Save to Serializable File
writeToSerializableFile();
break;
case "12": // Load from Serializable File
break;
case "13": // Exit
System.exit(0);
break;
}
}
// METHODS
public void printApartments() {
if (Driver.driver.condos.size() == 0) {
JOptionPane.showMessageDialog(null, "No Condos/Apartments Inserted");
return;
}
for (int i = 0; i < Driver.driver.condos.size(); i++) {
Driver.driver.condos.get(i).printApartments();
}
}
public void findApartmentCharges(String id) {
boolean found = false;
for (int i = 0; i < Driver.driver.condos.size(); i++) {
found = Driver.driver.condos.get(i).findApartment(id);
if (found == true) {
return;
}
}
if (found == false) {
JOptionPane.showMessageDialog(null, "No Apartment has the id " + id);
}
}
public void findPropertyCharges(int id) {
for (int i = 0; i < Driver.driver.condos.size(); i++) {
for (int j = 0; j < Driver.driver.condos.get(i).owners.size(); j++) {
// check equality of ids
if (Driver.driver.condos.get(i).owners.get(j).getId() == id) {
JOptionPane.showMessageDialog(null, "Owner with id " + id + " has to pay "
+ Driver.driver.condos.get(i).owners.get(j).getApartment().getPropertyCharges());
return;
}
}
}
JOptionPane.showMessageDialog(null, "No Owner has the id " + id);
}
public void writeToTextFile() {
try {
PrintWriter writer = new PrintWriter(new FileOutputStream("condominium.txt"));
// write num of Condos
writer.println(Driver.driver.condos.size());
// write each Condo separatly
for (int i = 0; i < Driver.driver.condos.size(); i++) {
Condominium c = Driver.driver.condos.get(i);
writer.println(c.toString());
for (int j = 0; j < c.admins.size(); j++) {
}
// apartment data
writer.println(c.apartments.size());
for (int j = 0; j < c.apartments.size(); j++) {
Apartment a = c.apartments.get(j);
writer.println(a.toString());
// owner of apartments
writer.println(a.owners.size());
for (int k = 0; k < a.owners.size(); k++) {
writer.println(a.owners.get(k));
}
}
// we don't write the owner list
// we will do it in another way
}
writer.close();
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
}
try {
Scanner reader = new Scanner(new FileInputStream("condominium.txt"));
for (int i = 0; i < num; i++) {
Condominium condo = new Condominium(tempCondoID, tempA);
// expenditure
for (int j = 0; j < num2; j++) {
}
// apartment data
for (int j = 0; j < num3; j++) {
Apartment a = new Apartment(tempCondoID, tempApartmentID,
tempSquareMeters, tempMillRate, tempPropertyCharges);
for (int k = 0; k < num4; k++) {
// add owner to apartment and condo
Owner tempOwner = new Owner(tempOwnerID, tempName, tempSurname, tempA3);
}
}
// add condo to condo list
}
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
}
public void writeToSerializableFile() {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("condominium.dat"));
// write condos
oos.writeInt(Driver.driver.condos.size());
for (int i = 0; i < Driver.driver.condos.size(); i++) {
oos.writeObject(Driver.driver.condos.get(i));
}
oos.close();
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
}
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("condominium.dat"));
for (int i = 0; i < num; i++) {
}
ois.close();
} catch (IOException | ClassNotFoundException ioe) {
JOptionPane.showMessageDialog(null, ioe.getMessage());
}
}
}

Input:

import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Input extends JDialog {
// CONSTRUCTOR
public Input(String name) {
// get name of button that called it
// to create the right type of Input
switch (name) {
this.setSize(600, 400);
this.setLayout(new GridLayout(8, 2));
JTextField[] texts = new JTextField[12];
for (int i = 0; i < texts.length; i++) {
texts[i] = new JTextField();
texts[i].setText("");
}
for (int i = 0; i < 10; i++) {
}
JButton button = new JButton("OK");
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < texts.length; i++) {
if (texts[i].getText().equals("")) {
return;
}
}
// get Text from TextFields and convert it if needed
Integer.parseInt(texts[10].getText()), Integer.parseInt(texts[11].getText()));
Integer.parseInt(texts[7].getText()), Integer.parseInt(texts[9].getText()));
texts[4].getText(), a1);
Condominium condo = new Condominium(Integer.parseInt(texts[1].getText()), a2);
// set composition partners
Input.this.setVisible(false);
Input.this.dispose();
}
});
this.setVisible(true);
break;
case "1": // Insert Owner/Apartment
this.setTitle("Insert Owner/Apartment");
this.setSize(600, 400);
this.setLayout(new GridLayout(8, 2));
JTextField[] texts2 = new JTextField[10];
for (int i = 0; i < texts2.length; i++) {
texts2[i] = new JTextField();
texts2[i].setText("");
}
for (int i = 0; i < 7; i++) {
}
JButton button2 = new JButton("OK");
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < texts2.length; i++) {
if (texts2[i].getText().equals("")) {
return;
}
}
// get Text from TextFields and convert it if needed
Integer.parseInt(texts2[8].getText()), Integer.parseInt(texts2[9].getText()));
Owner owner = new Owner(Integer.parseInt(texts2[0].getText()), texts2[2].getText(),
texts2[4].getText(), a3);
Apartment a = new Apartment(Integer.parseInt(texts2[1].getText()), texts2[3].getText(),
Integer.parseInt(texts2[5].getText()));
// set composition partners
owner.setApartment(a);
// add Owner and Apartment to Condo
for (int i = 0; i < Driver.driver.condos.size(); i++) {
if (Driver.driver.condos.get(i).getId() == a.getCondominiumID()) {
Input.this.setVisible(false);
Input.this.dispose();
return;
}
}
JOptionPane.showMessageDialog(null, "No Condominium has this ID!");
}
});
this.setVisible(true);
break;
this.setSize(600, 500);
this.setLayout(new GridLayout(8, 1));
JTextField[] texts3 = new JTextField[7];
for (int i = 0; i < texts3.length; i++) {
texts3[i] = new JTextField();
texts3[i].setText("");
}
JButton button3 = new JButton("OK");
@Override
public void actionPerformed(ActionEvent e) {
Integer.parseInt(texts3[5].getText()), Integer.parseInt(texts3[6].getText()));
texts3[2].getText(), a4);
int condo_id = Integer.parseInt(JOptionPane.showInputDialog("Give Condo ID"));
for (int i = 0; i < Driver.driver.condos.size(); i++) {
Condominium c = Driver.driver.condos.get(i);
if (c.getId() == condo_id) {
Input.this.setVisible(false);
Input.this.dispose();
return;
}
}
JOptionPane.showMessageDialog(null, "No Condo has this ID");
}
});
this.setVisible(true);
break;
case "3": // Add an Owner to an Apartment"
this.setTitle("Add an Owner to an Apartment");
this.setSize(600, 500);
this.setLayout(new GridLayout(8, 1));
JTextField[] texts4 = new JTextField[7];
for (int i = 0; i < texts4.length; i++) {
texts4[i] = new JTextField();
texts4[i].setText("");
}
JButton button4 = new JButton("OK");
@Override
public void actionPerformed(ActionEvent e) {
Integer.parseInt(texts4[5].getText()), Integer.parseInt(texts4[6].getText()));
Owner owner = new Owner(Integer.parseInt(texts4[0].getText()), texts4[1].getText(),
texts4[2].getText(), a5);
String apartment_id = JOptionPane.showInputDialog("Give Apartment ID");
for (int i = 0; i < Driver.driver.condos.size(); i++) {
Condominium c = Driver.driver.condos.get(i);
for (int j = 0; j < c.apartments.size(); j++) {
if (c.apartments.get(j).getApartmentID().equals(apartment_id)) {
Input.this.setVisible(false);
Input.this.dispose();
return;
}
}
}
JOptionPane.showMessageDialog(null, "No Apartment has this ID");
}
});
break;
case "7": // Calculate Expenditure of all Condos
this.setTitle("Calculate Expenditure of all Condos");
this.setSize(400, 300);
this.setLayout(new GridLayout(Driver.driver.condos.size() + 1, 2));
JTextField[] texts5 = new JTextField[Driver.driver.condos.size()];
for (int i = 0; i < texts5.length; i++) {
texts5[i] = new JTextField();
texts5[i].setText("");
}
JButton button5 = new JButton("OK");
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < texts5.length; i++) {
if (texts5[i].getText().equals("")) {
return;
}
}
// get Text from TextFields and convert it if needed
for (int i = 0; i < texts5.length; i++) {
Condominium c = Driver.driver.condos.get(i);
c.setCondominiumPropertyCharges(c.getId(), Double.parseDouble(texts5[i].getText()));
}
Input.this.setVisible(false);
Input.this.dispose();
}
});
this.setVisible(true);
break;
case "8": // Calculate/Print Property charges of all Apartments
this.setTitle("Calculate/Print Property charges of all Apartments");
this.setSize(300, 600);
int num = 0;
for (int i = 0; i < Driver.driver.condos.size(); i++) {
num += Driver.driver.condos.get(i).apartments.size();
// calculate charges
Driver.driver.condos.get(i).apartmentPropertyCharges();
}
this.setLayout(new GridLayout(num + 1, 2));
// print charges into Layout
for (int i = 0; i < Driver.driver.condos.size(); i++) {
Condominium c = Driver.driver.condos.get(i);
for (int j = 0; j < c.apartments.size(); j++) {
}
}
JButton button6 = new JButton("OK");
@Override
public void actionPerformed(ActionEvent e) {
Input.this.setVisible(false);
Input.this.dispose();
}
});
this.setVisible(true);
break;
}
}
}

You can see that I don't put more Labels that indicate what Kind of Input needs to be Inserted for Admin, Owner and so on, cause it would get even more complicated. You can try it out using numbers if you want tho :) I also don't check if some Choice is already made so that it is not allowed to run another one too. So, the GUI can look like this

This was the End of Today's Post. The GUI Code is too large and that's why we get too many Scrollbars. That's why I will stop with Java (for now), cause Coding in other languages is more compact. I will upload 2 Exercises in C for Queues and Stacks and then we will start taking a look into Assembly :D
Hope you enjoyed this Solution/Post :)

H2
H3
H4
3 columns
2 columns
1 column
1 Comment