summaryrefslogtreecommitdiff
path: root/src/com
diff options
context:
space:
mode:
Diffstat (limited to 'src/com')
-rw-r--r--src/com/mavlon/finances/Country.java40
-rw-r--r--src/com/mavlon/finances/Item.java48
-rw-r--r--src/com/mavlon/finances/Job.java66
-rw-r--r--src/com/mavlon/finances/Main.java170
-rw-r--r--src/com/mavlon/finances/Person.java14
-rw-r--r--src/com/mavlon/finances/Statistics.java56
-rw-r--r--src/com/mavlon/finances/Wallet.java40
7 files changed, 434 insertions, 0 deletions
diff --git a/src/com/mavlon/finances/Country.java b/src/com/mavlon/finances/Country.java
new file mode 100644
index 0000000..eef2a31
--- /dev/null
+++ b/src/com/mavlon/finances/Country.java
@@ -0,0 +1,40 @@
+package com.mavlon.finances;
+
+import java.io.Serializable;
+
+public class Country implements Serializable {
+ private double stateTaxRate;
+ private double federalTaxRate;
+
+ public Country() {
+ }
+
+ public Country(double stateTaxRate, double federalTaxRate) {
+ this.stateTaxRate = stateTaxRate;
+ this.federalTaxRate = federalTaxRate;
+ }
+
+ public double getStateTaxRate() {
+ return stateTaxRate;
+ }
+
+ public void setStateTaxRate(double stateTaxRate) {
+ this.stateTaxRate = stateTaxRate;
+ }
+
+ public double getFederalTaxRate() {
+ return federalTaxRate;
+ }
+
+ public void setFederalTaxRate(double federalTaxRate) {
+ this.federalTaxRate = federalTaxRate;
+ }
+
+ @Override
+ public String toString() {
+ return "Country" +
+ "\nstate tax rate = " + stateTaxRate +
+ "\nfederal tax rate = " + federalTaxRate +
+ '\n';
+ }
+}
diff --git a/src/com/mavlon/finances/Item.java b/src/com/mavlon/finances/Item.java
new file mode 100644
index 0000000..62794aa
--- /dev/null
+++ b/src/com/mavlon/finances/Item.java
@@ -0,0 +1,48 @@
+package com.mavlon.finances;
+
+import java.io.Serializable;
+
+public class Item implements Serializable {
+ private String name;
+ private double cost;
+
+ public Item() {
+ }
+
+ public Item(String name) {
+ this.name = name;
+ }
+
+ public Item(double cost) {
+ this.cost = cost;
+ }
+
+ public Item(String name, double cost) {
+ this.name = name;
+ this.cost = cost;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public double getCost() {
+ return cost;
+ }
+
+ public void setCost(double cost) {
+ this.cost = cost;
+ }
+
+ @Override
+ public String toString() {
+ return "Item" +
+ "\nname = '" + name + '\'' +
+ "\ncost = " + cost +
+ '\n';
+ }
+}
diff --git a/src/com/mavlon/finances/Job.java b/src/com/mavlon/finances/Job.java
new file mode 100644
index 0000000..11a7797
--- /dev/null
+++ b/src/com/mavlon/finances/Job.java
@@ -0,0 +1,66 @@
+package com.mavlon.finances;
+
+import java.io.Serializable;
+
+public class Job implements Serializable {
+ private String name;
+ private double hoursPerWeek;
+ private double hourlyPayRate;
+
+ public Job() {
+ }
+
+ public Job(String name) {
+ this.name = name;
+ }
+
+ public Job(String name, int hoursPerWeek) {
+ this.name = name;
+ this.hoursPerWeek = hoursPerWeek;
+ }
+
+ public Job(String name, int hoursPerWeek, int hourlyPayRate) {
+ this.name = name;
+ this.hoursPerWeek = hoursPerWeek;
+ this.hourlyPayRate = hourlyPayRate;
+ }
+
+ public Job(String name, int hoursPerWeek, int hourlyPayRate, int monthsToWork) {
+ this.name = name;
+ this.hoursPerWeek = hoursPerWeek;
+ this.hourlyPayRate = hourlyPayRate;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public double getHoursPerWeek() {
+ return hoursPerWeek;
+ }
+
+ public void setHoursPerWeek(double hoursPerWeek) {
+ this.hoursPerWeek = hoursPerWeek;
+ }
+
+ public double getHourlyPayRate() {
+ return hourlyPayRate;
+ }
+
+ public void setHourlyPayRate(double hourlyPayRate) {
+ this.hourlyPayRate = hourlyPayRate;
+ }
+
+ @Override
+ public String toString() {
+ return "Job" +
+ "\nname = '" + name + '\'' +
+ "\nhours per week = " + hoursPerWeek +
+ "\nhourly pay rate = " + hourlyPayRate +
+ '\n';
+ }
+}
diff --git a/src/com/mavlon/finances/Main.java b/src/com/mavlon/finances/Main.java
new file mode 100644
index 0000000..a4dc62e
--- /dev/null
+++ b/src/com/mavlon/finances/Main.java
@@ -0,0 +1,170 @@
+package com.mavlon.finances;
+
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class Main {
+ private final static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
+
+ public static void main(String[] args) {
+ Person person = new Person();
+ int choice;
+
+ if (Files.exists(Path.of("person"))) {
+ try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("person"))) {
+ person = (Person) objectInputStream.readObject();
+ } catch (IOException | ClassNotFoundException ioException) {
+ ioException.printStackTrace();
+ }
+ } else {
+ System.out.println("Take a short survey:");
+ System.out.println();
+ setPersonsInformationAboutWallet(person);
+ System.out.println();
+ setPersonsInformationAboutCountry(person);
+ System.out.println();
+ setPersonsInformationAboutJob(person);
+ System.out.println();
+ setPersonsInformationAboutDreamItem(person);
+ System.out.println();
+ }
+
+ do {
+ System.out.println("""
+ Menu:
+ 1) Wallet info
+ 11) Put money to wallet
+ 12) Take money from wallet
+ 2) Job info
+ 21) I have worked a month
+ 22) Change job
+ 3) Dream item info
+ 31) When I will be able to buy?
+ 32) Change dream item
+ 33) Change dream item cost
+ 4) Country info
+ 41) Change country
+ 5) Statistics
+ 6) Save and exit
+ """);
+ try {
+ System.out.print("Enter number: ");
+ choice = Integer.parseInt(bufferedReader.readLine());
+ System.out.print("\n");
+ } catch (IOException ioException) {
+ choice = -1;
+ }
+ switch (choice) {
+ case 1 -> System.out.println(person.wallet);
+ case 11 -> {
+ System.out.print("Enter how much money put: ");
+ try {
+ person.wallet.put(Double.parseDouble(bufferedReader.readLine()));
+ } catch (IOException ioException) {
+ System.out.println("Invalid number. Try again.");
+ }
+ }
+ case 12 -> {
+ double money;
+
+ System.out.print("Enter how much money take: ");
+ try {
+ money = Double.parseDouble(bufferedReader.readLine());
+ person.wallet.take(money);
+ person.statistics.addExpenses(money);
+ } catch (IOException ioException) {
+ System.out.println("Invalid number. Try again.");
+ }
+ }
+ case 2 -> System.out.println(person.job);
+ case 21 -> person.wallet.put(person.statistics.getMonthlyNetSalary());
+ case 22 -> setPersonsInformationAboutJob(person);
+ case 3 -> System.out.println(person.dreamItem);
+ case 31 -> System.out.println(person.wallet.getMoney() > person.dreamItem.getCost() ? "Congratulations! You already can buy your dream item!"
+ : "You have to work " + (int) Math.ceil((person.dreamItem.getCost() - person.wallet.getMoney()) / person.statistics.getMonthlyNetSalary()) + " months.");
+ case 32 -> setPersonsInformationAboutDreamItem(person);
+ case 33 -> {
+ System.out.print("How much your dream item costs? ");
+ try {
+ person.dreamItem.setCost(Double.parseDouble(bufferedReader.readLine()));
+ } catch (IOException ioException) {
+ System.out.println("Invalid number. Try again.");
+ }
+ }
+ case 4 -> System.out.println(person.country);
+ case 41 -> setPersonsInformationAboutCountry(person);
+ case 5 -> System.out.println(person.statistics);
+ case 6 -> {
+ try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("person"))) {
+ objectOutputStream.writeObject(person);
+ } catch (IOException ioException) {
+ ioException.printStackTrace();
+ }
+ System.out.println("Good bye!");
+ }
+ default -> System.out.println("Incorrect input. Try again.");
+ }
+ } while (choice != 6);
+ }
+
+ private static void setPersonsInformationAboutWallet(Person person) {
+ try {
+ System.out.print("How much money do you have? ");
+ person.wallet.setMoney(Double.parseDouble(bufferedReader.readLine()));
+ } catch (IOException ioException) {
+ System.out.println("Incorrect input. Try again.");
+ setPersonsInformationAboutWallet(person);
+ }
+ }
+
+ private static void setPersonsInformationAboutCountry(Person person) {
+ try {
+ System.out.print("How much is state tax rate in your country? ");
+ person.country.setStateTaxRate(Double.parseDouble(bufferedReader.readLine()));
+ System.out.print("How much is federal tax rate in your country? ");
+ person.country.setFederalTaxRate(Double.parseDouble(bufferedReader.readLine()));
+ setPersonsInformationAboutStatistics(person);
+ } catch (IOException ioException) {
+ System.out.println("Incorrect input. Try again.");
+ setPersonsInformationAboutCountry(person);
+ }
+ }
+
+ private static void setPersonsInformationAboutJob(Person person) {
+ try {
+ System.out.print("What is your job? ");
+ person.job.setName(bufferedReader.readLine());
+ System.out.print("How many hours do you work per week? ");
+ person.job.setHoursPerWeek(Double.parseDouble(bufferedReader.readLine()));
+ System.out.print("How much is your hourly pay? ");
+ person.job.setHourlyPayRate(Double.parseDouble(bufferedReader.readLine()));
+ setPersonsInformationAboutStatistics(person);
+ } catch (IOException ioException) {
+ System.out.println("Incorrect input. Try again.");
+ setPersonsInformationAboutJob(person);
+ }
+ }
+
+ private static void setPersonsInformationAboutDreamItem(Person person) {
+ try {
+ System.out.print("What is your dream item? ");
+ person.dreamItem.setName(bufferedReader.readLine());
+ System.out.print("How much your dream item costs? ");
+ person.dreamItem.setCost(Double.parseDouble(bufferedReader.readLine()));
+ } catch (IOException ioException) {
+ System.out.println("Incorrect input. Try again.");
+ setPersonsInformationAboutDreamItem(person);
+ }
+ }
+
+ private static void setPersonsInformationAboutStatistics(Person person) {
+ double monthlyGrossSalary = person.job.getHourlyPayRate() * person.job.getHoursPerWeek() * 4;
+ double monthlyTaxes = monthlyGrossSalary * (person.country.getStateTaxRate() + person.country.getFederalTaxRate()) / 100;
+ double monthlyNetSalary = monthlyGrossSalary - monthlyTaxes;
+
+ person.statistics.setMonthlyGrossSalary(monthlyGrossSalary);
+ person.statistics.setMonthlyTaxes(monthlyTaxes);
+ person.statistics.setMonthlyNetSalary(monthlyNetSalary);
+ }
+}
diff --git a/src/com/mavlon/finances/Person.java b/src/com/mavlon/finances/Person.java
new file mode 100644
index 0000000..8f21609
--- /dev/null
+++ b/src/com/mavlon/finances/Person.java
@@ -0,0 +1,14 @@
+package com.mavlon.finances;
+
+import java.io.Serializable;
+
+public class Person implements Serializable {
+ public final Wallet wallet = new Wallet();
+ public final Job job = new Job();
+ public final Item dreamItem = new Item();
+ public final Country country = new Country();
+ public final Statistics statistics = new Statistics();
+
+ public Person() {
+ }
+}
diff --git a/src/com/mavlon/finances/Statistics.java b/src/com/mavlon/finances/Statistics.java
new file mode 100644
index 0000000..edd2515
--- /dev/null
+++ b/src/com/mavlon/finances/Statistics.java
@@ -0,0 +1,56 @@
+package com.mavlon.finances;
+
+import java.io.Serializable;
+
+public class Statistics implements Serializable {
+ private double monthlyGrossSalary;
+ private double monthlyTaxes;
+ private double monthlyNetSalary;
+ private double totalExpenses;
+
+ public void addExpenses(double expenses) {
+ totalExpenses += expenses;
+ }
+
+ public double getMonthlyGrossSalary() {
+ return monthlyGrossSalary;
+ }
+
+ public void setMonthlyGrossSalary(double monthlyGrossSalary) {
+ this.monthlyGrossSalary = monthlyGrossSalary;
+ }
+
+ public double getMonthlyTaxes() {
+ return monthlyTaxes;
+ }
+
+ public void setMonthlyTaxes(double monthlyTaxes) {
+ this.monthlyTaxes = monthlyTaxes;
+ }
+
+ public double getMonthlyNetSalary() {
+ return monthlyNetSalary;
+ }
+
+ public void setMonthlyNetSalary(double monthlyNetSalary) {
+ this.monthlyNetSalary = monthlyNetSalary;
+ }
+
+ public double getTotalExpenses() {
+ return totalExpenses;
+ }
+
+ public void setTotalExpenses(double totalExpenses) {
+ this.totalExpenses = totalExpenses;
+ }
+
+ @Override
+ public String toString() {
+ return "Statistics" +
+ "\nmonthly gross salary = " + monthlyGrossSalary +
+ "\nmonthly taxes = " + monthlyTaxes +
+ "\nmonthly net salary = " + monthlyNetSalary +
+ "\ntotal expenses = " + totalExpenses +
+ '\n';
+ }
+}
diff --git a/src/com/mavlon/finances/Wallet.java b/src/com/mavlon/finances/Wallet.java
new file mode 100644
index 0000000..f3a04e0
--- /dev/null
+++ b/src/com/mavlon/finances/Wallet.java
@@ -0,0 +1,40 @@
+package com.mavlon.finances;
+
+import java.io.Serializable;
+
+public class Wallet implements Serializable {
+ private double money;
+
+ public Wallet() {
+ }
+
+ public Wallet(double money) {
+ this.money = money;
+ }
+
+ public void put(double money) {
+ this.money += money;
+ }
+
+ public void take(double money) {
+ if (this.money < money) {
+ throw new IllegalArgumentException("You do not have such money.");
+ }
+ this.money -= money;
+ }
+
+ public double getMoney() {
+ return money;
+ }
+
+ public void setMoney(double money) {
+ this.money = money;
+ }
+
+ @Override
+ public String toString() {
+ return "Wallet" +
+ "\nmoney = " + money +
+ '\n';
+ }
+}