Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! so basically I have a Java code for a basic application for university and its basically a money manager where the user enters their

Hello! so basically I have a Java code for a basic application for university and its basically a money manager where the user enters their expense income etc my problem is that I cant have it save it to a file.. the addToFile and readFromFile are not working for some reason. I need help fixing them. They are in the Test class but I will give all classes for more context!

Expense class:

package moneymanager;

public class Expense { private String category; private double amount;

public Expense(String category, double amount) { this.category = category; this.amount = amount; }

public String getCategory() { return category; }

public void setCategory(String category) { this.category = category; }

public double getAmount() { return amount; }

public void setAmount(double amount) { this.amount = amount; } }

Income class:

package moneymanager;

public class Income { private String source; private double amount;

public Income(String source, double amount) { this.source = source; this.amount = amount; }

public String getSource() { return source; }

public void setSource(String source) { this.source = source; }

public double getAmount() { return amount; }

public void setAmount(double amount) { this.amount = amount; } }

User class:

package moneymanager;

import java.util.ArrayList;

public class User { private String name; private ArrayList income; private ArrayList expenses;

public User(String name, ArrayList income, ArrayList expenses) { this.name = name; this.income = income; this.expenses = expenses; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public ArrayList getIncome() { return income; }

public void setIncome(ArrayList income) { this.income = income; }

public ArrayList getExpenses() { return expenses; }

public void setExpenses(ArrayList expenses) { this.expenses = expenses; } }

Test class:

package moneymanager;

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Scanner;

public class Test { private ArrayList users;

public Test() { users = new ArrayList(); }

public void createUser() { Scanner input = new Scanner(System.in); System.out.print("Enter user name: "); String name = input.nextLine(); User user = new User(name, new ArrayList(), new ArrayList()); users.add(user); }

public void createIncome() { Scanner input = new Scanner(System.in); System.out.print("Enter user name: "); String name = input.nextLine(); User user = null; for (User u : users) { if (u.getName().equals(name)) { user = u; break; } } if (user == null) { System.out.println("User not found."); return; } System.out.print("Enter source of income: "); String source = input.nextLine(); System.out.print("Enter amount: "); double amount = input.nextDouble(); Income income = new Income(source, amount); user.getIncome().add(income); }

public void createExpense() { Scanner input = new Scanner(System.in); System.out.print("Enter user name: "); String name = input.nextLine(); User user = null; for (User u : users) { if (u.getName().equals(name)) { user = u; break; } } if (user == null) { System.out.println("User not found."); return; } System.out.print("Enter category of expense: "); String category = input.nextLine(); System.out.print("Enter amount: "); double amount = input.nextDouble(); Expense expense = new Expense(category, amount); user.getExpenses().add(expense); } public void addToFile() { try { try (FileOutputStream fos = new FileOutputStream("money-manager.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos)) { oos.writeObject(users); } System.out.println("Data saved successfully to file"); } catch (IOException ioe) { } }

public void readFromFile() { try { try (FileInputStream fis = new FileInputStream("money-manager.txt"); ObjectInputStream ois = new ObjectInputStream(fis)) { users = (ArrayList) ois.readObject(); } System.out.println("Data loaded successfully from file"); } catch (IOException ioe) { System.out.println("Error reading from file"); } catch (ClassNotFoundException cnfe) { } }

public void displayDetails() { for (User user : users) { System.out.println("Name: " + user.getName()); System.out.println("Income:"); ArrayList userIncome = user.getIncome(); for (Income income : userIncome) { System.out.println("Source: " + income.getSource() + ", Amount: $" + income.getAmount()); } System.out.println("Expenses:"); ArrayList userExpenses = user.getExpenses(); for (Expense expense : userExpenses) { System.out.println("Category: " + expense.getCategory() + ", Amount: $" + expense.getAmount()); } System.out.println(" "); } } }

MoneyManager class:

package moneymanager;

import java.util.Scanner;

/** * * @author TRIX */ public class MoneyManager {

/** * @param args the command line arguments */ public static void main(String[] args) { Test moneyManager = new Test(); moneyManager.readFromFile();

Scanner sc = new Scanner(System.in); int choice = 0; while (choice != 5) { System.out.println("Welcome to Money Manager"); System.out.println("1. Create a user"); System.out.println("2. Create an income"); System.out.println("3. Create an expense"); System.out.println("4. Display user information"); System.out.println("5. Exit"); System.out.println("Enter your choice: ");

choice = sc.nextInt(); sc.nextLine();

switch (choice) { case 1: moneyManager.createUser(); break; case 2: moneyManager.createIncome(); break; case 3: moneyManager.createExpense(); break; case 4: moneyManager.displayDetails(); break; case 5: moneyManager.addToFile(); System.out.println("Exiting the program"); break; default: System.out.println("Invalid choice, please try again"); } } sc.close(); } }

--------------

I need help fixing the readFromFile and addToFile please thank youu! Send all classes please, if you can't paste all of them here then I suggest you post the continuation of the code on Pastebin. Thanks!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions

Question

3. Describe at least two features of wise reasoning.

Answered: 1 week ago

Question

Understanding Group Leadership Culture and Group Leadership

Answered: 1 week ago