Need internal documentation for this Java file For internal documentation (i.e. in the source code) we require:
Question:
Need internal documentation for this Java file
For internal documentation (i.e. in the source code) we require: A beginning comment clearly stating title, author, date, file name, purpose and any assumptions or conditions on the form of input and expected output; Well-formatted readable code with meaningful identifier names and blank lines between components (like methods and classes).
Change.java
import java.util.ArrayList;
public class Change { private String name; private int change, nFifty, nTwenty, nTen, nFive; public Change() { this.name = ""; this.change = this.nFifty = this.nFive = this.nTen = this.nTwenty = 0; }
public Change(String name, int change) { this.name = name; this.change = change; this.nFifty = this.nFive = this.nTen = this.nTwenty = 0; }
public String getName() { return name; }
public int getChange() { return change; }
public int getnFifty() { return nFifty; }
public int getnTwenty() { return nTwenty; }
public int getnTen() { return nTen; }
public int getnFive() { return nFive; } private void processChange() { // change = 65 int rem = change; if(change >= 50) { nFifty = (change / 50); rem = (change % 50); } if(rem >= 20) { nTwenty = (rem / 20); rem = (rem % 20); } if(rem >= 10) { nTen = (rem / 10); rem = (rem % 10); } if(rem >= 5) { nFive = (rem / 5); rem = (rem % 5); } } public int getTotalCoins() { processChange(); return(nFifty + nTwenty + nTen + nFive); } @Override public String toString() { processChange(); String fiftyStr = "", twentyStr = "", tenStr = "", fiveStr = ""; if(nFifty != 0) fiftyStr = "50 cents: " + nFifty; if(nTwenty != 0) twentyStr = "20 cents: " + nTwenty; if(nTen != 0) tenStr = "10 cents: " + nTen; if(nFive != 0) fiveStr = "5 cents: " + nFive; ArrayList strs = new ArrayList<>(); strs.add(fiftyStr); strs.add(twentyStr); strs.add(tenStr); strs.add(fiveStr); String res = ""; for(String s : strs) { if(!s.equals("")) res += s + " "; } return(name + " " + change + " cents " + "Change: " + res); } }
ManageChange.java
import java.util.ArrayList; import java.util.Scanner;
public class ManageChange { private ArrayList changes; public ManageChange() { this.changes = new ArrayList<>(); } private int getIndex(String name) { int index = -1; for(int i = 0; i < changes.size(); i++) { if(changes.get(i).getName().equalsIgnoreCase(name)) { index = i; break; } } return index; } public void addCustomer(Change c) { int index = getIndex(c.getName()); if(index == -1) changes.add(c); else System.out.println("Sorry, a customer with this name already exists! "); } public void getChangeByName() { Scanner sc = new Scanner(System.in); System.out.print("Customer: "); String name = sc.nextLine().trim(); int index = getIndex(name); if(index == -1) System.out.println("Not found! "); else System.out.println(changes.get(index)); } public void displayCustomerWithSmallestChange() { int min = changes.get(0).getChange(); int minIndex = 0; for(int i = 0; i < changes.size(); i++) { if(changes.get(i).getChange() < min) { min = changes.get(i).getChange(); minIndex = i; } } System.out.println("Customer with smallest change: " + changes.get(minIndex)); } public void displayCustomerWithLargestChange() { int max = changes.get(0).getChange(); int maxIndex = 0; for(int i = 0; i < changes.size(); i++) { if(changes.get(i).getChange() > max) { max = changes.get(i).getChange(); maxIndex = i; } } System.out.println("Customer with smallest change: " + changes.get(maxIndex)); } public void displayTotalCoinsForEachCustomer() { if(changes.isEmpty()) { System.out.println("List is empty! "); return; } int sum = 0; for(Change c : changes) { sum += c.getTotalCoins(); System.out.println(c.getName() + " got a total of " + c.getTotalCoins() + " coin(s)"); } System.out.println(" Sum of coins for all the customers = " + sum + ". "); } }
Client.java (Main class)
import java.util.Scanner;
public class Client { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ManageChange manage = new ManageChange(); int choice; do { displayMenu(); choice = Integer.parseInt(sc.nextLine().trim()); switch(choice) { case 1: { System.out.println(" ADD A CUSTOMER: " + "---------------"); System.out.print("Enter the number of customers to be added: "); int n = Integer.parseInt(sc.nextLine().trim()); for(int i = 0; i < n; i++) { System.out.println("Customer #" + (i + 1) + ": ------------"); System.out.print("Please enter the name of the person: "); String name = sc.nextLine().trim(); System.out.print("Please enter the coin value for the person (range 5 to 95, multiple of 5): "); int coinValue = Integer.parseInt(sc.nextLine().trim()); while(!validateCoinValue(coinValue)) { System.out.println("Incorrect coin value. Must be in the range 5 to 95, and multiple of 5. "); System.out.print("Please enter the coin value for the person (range 5 to 95, multiple of 5): "); coinValue = Integer.parseInt(sc.nextLine().trim()); } int change = 95 - coinValue; manage.addCustomer(new Change(name, change)); System.out.println(); } System.out.println(); break; } case 2: { System.out.println(" SEARCH FOR A CUSTOMER AND DISPLAY CHANGE TO BE GIVEN FOR EACH DENOMINATION: " + "---------------------------------------------------------------------------"); manage.getChangeByName(); break; } case 3: { System.out.println(" DISPLAY CUSTOMER WITH SMALLEST CHANGE: " + "--------------------------------------"); manage.displayCustomerWithSmallestChange(); break; } case 4: { System.out.println(" DISPLAY CUSTOMER WITH LARGEST CHANGE: " + "-------------------------------------"); manage.displayCustomerWithLargestChange(); break; } case 5: { System.out.println(" DISPLAY TOTAL NUMBER OF COINS FOR EACH DENOMINATION AND SUM OF THESE TOTALS: " + "----------------------------------------------------------------------------"); manage.displayTotalCoinsForEachCustomer(); break; } case 0: { System.out.println(" Thanks..Goodbye! "); System.exit(0); } default: System.out.println(" Invalid selection! "); } }while(choice != 0); } private static void displayMenu() { System.out.print("Choose from the following options: " + "1. Add a customer " + "2. Enter a name and display change to be given for each denomination " + "3. Find the name with the smallest amount and display change to be given for each denomination " + "4. Find the name with the largest amount and display change to be given for each denomination " + "5. Calculate and display the total number of coins for each denomination, and the sum of these totals " + "0. Exit " + "Your selection >> "); } private static boolean validateCoinValue(int value) { boolean withinRange = true; if(value < 5 || value > 95) withinRange = false; boolean divBy5 = true; if(value % 5 != 0) divBy5 = false; return(withinRange && divBy5); } }
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill