summaryrefslogtreecommitdiff
path: root/src/com/mavlon/finances/Main.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/mavlon/finances/Main.java')
-rw-r--r--src/com/mavlon/finances/Main.java170
1 files changed, 170 insertions, 0 deletions
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);
+ }
+}