Answer to Question #75118 in Java | JSP | JSF for ana

Question #75118
Receive the names of corporations involved in this simulation as command-line arguments. Corporation names are guaranteed not to contain whitespace characters. For example, if your main method is in a class called Assignment07, then a command specifying two companies named Apple and WalMart would look like this:
java Assignment07 Apple WalMart
Maintain information about each corporation in an object that contains at least:
The corporation's cash on hand (initially $0)
The corporation's annual taxable income (initially $1 billion)
The corporation's annual tax-sheltered income (initially $0)
Process an arbitrary number of commands as input on standard input. Each command consists of one or three whitespace-delimited tokens, as follows:
taxable name value: Assigns a new amount of annual taxable income for the given corporation. name must be a valid name of a corporation, and value must be a decimal number in the range provided by Java's double type. For example:
taxable WalMart 10e9
1
Expert's answer
2018-03-29T08:05:26-0400
Corporation.java
======================================
class Corporation {

private String name;

private double cash;
private double taxable;
private double taxSheltered;

Corporation(String name) {
this.name = name;
cash = 0;
taxable = 10e9;
taxSheltered = 0;
}

void setCash(double cash) {
this.cash = cash;
}

void setTaxable(double taxable) {
this.taxable = taxable;
}

void setTaxSheltered(double taxSheltered) {
this.taxSheltered = taxSheltered;
}
}
=============================================
Main.java
============================================
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) {
Map<String, Corporation> corporations = new HashMap<>();
Scanner scanner = new Scanner(System.in, "UTF-8");

for (String arg : args) {
corporations.put(arg, new Corporation(arg));
}

StringTokenizer tokenizer = new StringTokenizer(scanner.nextLine());
scanner.close();

while (tokenizer.hasMoreTokens()) {
switch (tokenizer.nextToken()) {
case "taxable": {
String name = tokenizer.nextToken();
double value;
try {
value = Double.parseDouble(tokenizer.nextToken());
} catch (NumberFormatException nfe) {
break;
}
if (corporations.containsKey(name)) {
corporations.get(name).setTaxable(value);
}
break;
}
case "cash": {
String name = tokenizer.nextToken();
double value;
try {
value = Double.parseDouble(tokenizer.nextToken());
} catch (NumberFormatException nfe) {
break;
}
if (corporations.containsKey(name)) {
corporations.get(name).setCash(value);
}
break;
}
case "tax-sheltered": {
String name = tokenizer.nextToken();
double value;
try {
value = Double.parseDouble(tokenizer.nextToken());
} catch (NumberFormatException nfe) {
break;
}
if (corporations.containsKey(name)) {
corporations.get(name).setTaxSheltered(value);
}
break;
}
}
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS