In: Computer Science
To create a home budget, you want to find out your net income,
which is your income minus your expenses.
Write a Java method that takes an input string and computes the
income minus the expenses.
The income components are indicated by numbers; while the expenses
from your spending are numbers starting with a minus sign
'-'.
The input string may contain lowercase and uppercase letters, as
well as other characters.
Note that Character.isDigit(char) tests if a char is one of the
chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string)
converts a string to an int.
Test cases :
calcNetIncome("salary 15000yuan bonus2000 rent -1000Y") →
16000
calcNetIncome("25000 gross income, -200 water, electricity:-300") →
24500
Java Method to create a home budget, which calculate net income from a given string by substracting total expenses from total incomes :
Main.java :
public class Main {
/*
Method to calculate netIncome from a given string.
*/
public static double calculateNetIncome(String input) {
//split input string into a single words and store words in String array.
String[] words = input.split(" ");
double totalIncomes = 0; //variable to hold totalIncomes.
double totalExpenses = 0; //variable to hold totalExpenses.
/*
below for-each loop will execute for each word in array named words,
*/
for(String word:words) {
/*
check if word contain (-) symbol or not if yes then,
this word should not be processed further to calculate total income
because if word contain hyphen means this will considered as expense amount not as income.
*/
if(!word.contains("-")) { // if word not contains - symbol then below block execute.
String income = "0"; // local variable to hold digit in current word if any.
/*
loop through each character of select word
*/
for(int i=0;i<word.length();i++) {
//if character is a digit concatenate that digit with income varible.
if(Character.isDigit(word.charAt(i))) {
income +=word.charAt(i); // concatenate income varible.
}
}
/*
after above foreach terminate, add income and totalIncomes
since income variable if string we have to use Integer.parseInt() to convert it to Integer.
remember income variable will be either 0 (if current word doen't contain any digit) or any other numeric value.
*/
totalIncomes = totalIncomes + Integer.parseInt(income);
}
/*
check if word contain (-) symbol or if yes then,
this word should be processed further to calculate total expenses
because if word contain hyphen means this will considered as expense amount not as income.
*/
else if(word.contains("-")) {
/*
loop through each character of select word
*/
String expense = "0"; // local variable to hold digit in current word if any.
for(int i=0;i<word.length();i++) {
//if character is a digit concatenate that digit with expense varible.
if(Character.isDigit(word.charAt(i))) {
expense +=word.charAt(i);
}
}
/*
after above foreach terminate, add expense and totalExpenses
since expense variable if string we have to use Integer.parseInt() to convert it to Integer.
remember expense variable will be either 0 (if current word doen't contain any digit) or any other numeric value.
*/
totalExpenses = totalExpenses + Integer.parseInt(expense);
}
/*
from here program control will again go to for-each loop and
next available word in words array will be processed in same way as above
*/
}
/*
after for-each loop terminates we have totalIncomes and totalExpenses calculated
now substract totalExpenses from totalIncomes to get netIncome
*/
double netIncome = totalIncomes - totalExpenses;
/*
instead of returning value from this function you may also print calculated value inside this function
by making return type of this function as void instead of double according to need.
*/
//return netIncome
return netIncome;
}
/*
main method we will call calculateNetIncome() method in main method to see it's output
*/
public static void main(String[] args) {
/*
Creating tow test cases to test calculateNetIncome method.
since we have created calculateNetIncome() return type as double,
we need to store it's return value in double type variable.
*/
double test1 = calculateNetIncome("salary 15000yuan bonus2000 rent -1000Y");
double test2 = calculateNetIncome("25000 gross income, -200 water, electricity:-300");
/*
printing these two test result on console.
*/
System.out.println("test1 result= "+test1);
System.out.println("test2 result= "+test2);
}
}
Sample output :
Please refer to the Screenshot of the code given below to understand indentation of the code :