Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1) Implement the credit(int) and debit(int) methods in the Asset classes (Asset Account(Credit(): value is subtracted from the delta value; Debit(): value is added to
1)
Implement the credit(int) and debit(int) methods in the Asset classes (Asset Account(Credit(): value is subtracted from the delta value; Debit(): value is added to the delta value)) and Liability (liability account(Credit(): value is added to the delta value; Debit(): value is subtracted from the delta value.)). Be sure to include only the delta value and not change the value of the account itself. Only when commit() is called later (see class Account) the value of the account should be adjusted. Throw appropriate exceptions (see Package exceptions) when an account is not opened or the amount on the account is insufficient for the given value on the account to post (negative value on the account).
public class Asset extends Account { public Asset(String name) { super(name); } }
public class Liability extends Account { public Liability(String name) { super(name); } }
public abstract class Account { private String name; private boolean open = false; protected int value = 5000; protected int delta = 0; public Account(String name) { this.name = name; } public abstract void credit(int value); public abstract void debit(int value); public void open() { this.open = true; } public void abort() { delta = 0; } public void commit() { value += delta; delta = 0; } public void close() { this.open = false; } public boolean isOpen() { return open; } public String getName() { return name; } @Override public String toString() { return name + " (value=" + value + ", open=" + open + ")"; } }
2)
Implement the getAccount, getAsset, and getLiability methods in the class Account Manager. While the getAsset(name) and getLiability(name) methods are only available in the list of active accounts, or passive accounts, should search for the given account, the getAccount(name) method Search all accounts. Throw an appropriate exception if no account with the given name is found and do not return null to the caller.
import java.util.ArrayList; import java.util.Random; public class AccountManager { private static ArrayListassets = new ArrayList<>(); private static ArrayList liabilities = new ArrayList<>(); private static Random r = new Random(); /** * Ratio from 0 to 100; 0 - (almost) no errors in journal entries, 100 - many errors in entries */ private static final int ERRORRATIO = 10; public static Account getAccount(String name){ return null; } public static Account getAsset(String name) { return null; } public static Account getLiability(String name) { return null; } /** * Prints all accounts. */ public static void printAccounts() { System.out.println("Assets:"); for (Account a : assets) { System.out.println(" - " + a.toString()); } System.out.println("Liabilities:"); for (Account a : liabilities) { System.out.println(" - " + a.toString()); } } /** * Inits Account Manager */ public static void init() { //Debits assets.add(new Asset("Cash")); assets.add(new Asset("Inventory")); assets.add(new Asset("Supplies")); assets.add(new Asset("Land")); assets.add(new Asset("Equipment")); assets.add(new Asset("Vehicles")); assets.add(new Asset("Buildings")); //Revenues/Gains assets.add(new Asset("Operating_Revenues")); assets.add(new Asset("Other_Revenues")); //Credits liabilities.add(new Liability("Accounts_Payable")); liabilities.add(new Liability("Wages_Payable")); liabilities.add(new Liability("Interest_Payable")); liabilities.add(new Liability("Unearned_Payable")); liabilities.add(new Liability("Bonds_Payable")); liabilities.add(new Liability("Accounts_Payable")); liabilities.add(new Liability("Equity")); //Expenses/Losses liabilities.add(new Liability("Cost_of_Goods_Sold")); liabilities.add(new Liability("Payroll_Expenses")); liabilities.add(new Liability("Marketing_Expenses")); liabilities.add(new Liability("Other_Expenses")); } public static String getRandomJournalEntry() { return getRandomJournalEntry(r.nextInt(9)+1); } public static String getRandomJournalEntry(int numberOfAffectedAcounts) { int d = r.nextInt(numberOfAffectedAcounts-1) + 1; int c = numberOfAffectedAcounts-d; int value = 0; String entry = ""; for(int i = 0; i < d; i++) { Account a = getRandomAccount(); int v = r.nextInt(1000)+1; if (r.nextInt(100) < ERRORRATIO) { value += v; if (r.nextInt(100) < 50) { entry += a.getName() + " " + v + "* "; } else { entry += a.getName() + "_NEW " + v + ", "; } } else { value += v; entry += a.getName() + " " + v + ", "; } } entry = entry.substring(0, entry.length()-2); if (r.nextInt(100) < ERRORRATIO) { if (r.nextInt(100) < 50) { entry += "# "; } else { entry += " "; } } else { entry += "; "; } for(int i = 0; i < c - 1; i++) { Account a = getRandomAccount(); int v = value / 3; value -= v; entry += a.getName() + " " + v + ", "; } Account a = getRandomAccount(); int v = value; if (r.nextInt(100) < ERRORRATIO) { v++; entry += a.getName() + " " + v; } else { entry += a.getName() + " " + v; } return entry; } private static Account getRandomAccount() { int i = r.nextInt(assets.size() + liabilities.size()); if (i < assets.size()) { return assets.get(i); } return liabilities.get(i-assets.size()); } }
3)
Implement the postEntry() method in the Accountant class, which has a posting record is passed as a parameter. A correct accounting record consists of a series of accounts addressed in the debit followed by a ';' character, followed by a series of accounts that are in credit to be agreed. Each account-value pair is separated by commas, Account name and value are separated by a space. An exemplary Booking rate is: "Equity 27, Bonds_Payable 385; Land 137, Other_Revenues 275 In your implementation, first check the syntax of the passed posting record and make sure that the amount on the debit side matches the amount on the Credit side matches. Throw appropriate exceptions in case of error. The following applies to the subsequent booking: 1. First open all accounts involved (open) 2. Prepare the booking on each account (credit or debit) 3. If there is no error in any account, then carry out the posting (commit) 4. If an error occurs (e.g. insufficient amount), all previous changes should be made be reversed on the accounts (abort) 5. Finally, all accounts should be closed in any case (close)
public class Accountant { public void postEntry(String journalEntry) { //check syntax and import //Ensure that debit value equals credit value //Open Accounts //Post entries //Ensure that all accounts are closed } }
package util; public class Pair { public final U first; public final V second; public Pair(U first, V second) { this.first = first; this.second = second; } public U getFirst() { return first; } public V getSecond() { return second; } @Override public String toString() { return "(" + first + ", " + second + ")"; } }
PLEASE HELP
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started