Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The answer must use the indexof , substring , and stringbuilder methods. The use of tokenizer utility is not allowed. NOTE THERE IS ALREADY A

The answer must use the indexof, substring, and stringbuilder methods. The use of tokenizer utility is not allowed.

NOTE THERE IS ALREADY A SIMILAR QUESTION ON CHEGG AND THE MAIN CLASS AND ANSWER WILL DIFFER. PLEASE DONT COPY AND PASE THE SOLUTION ALREADY PROVIDED ON CHEGG.

Exercise 9-3 Parse a string of product data In this exercise, youll parse a string that contains data for a product and store that data in a Product object. Review the application Open the project named ch08_ex3_ProductParser.

Open the Main and Product classes and review the code. Note that the Main class defines a Product object and a String object that stores the data for a product with each field delimited by a colon (:). However, this code doesnt parse this string and store the data in the Product object.

Run the application. At this point, it shouldnt print any product data to the console.

Add the code that parses the string

In the Main class, add code to the parseString method that parses the string and gets the three fields that are stored in the string. Then, store this data in the Product object that is returned by the method.

Run the application and make sure it works correctly. After a successful run, the console should look something like this:

CODE PROVIDED:

main.java

package murach.ui;

import murach.business.Product;

public class Main {

public static void main(String[] args) { String productString1 = "java:Murach's Java Programming:57.50"; String productString2 = "mysql:Murach's MySQL:54.50"; Product product1 = new Product(); Product product2 = new Product(); //TODO: process the productString variables and populate fields of //product objects in the parseString method product1 = parseString(productString1, product1); product2 = parseString(productString2, product2); System.out.println("Code: " + product1.getCode()); System.out.println("Description: " + product1.getDescription()); System.out.println("Price: " + product1.getPriceFormatted()); System.out.println(""); System.out.println("Code: " + product2.getCode()); System.out.println("Description: " + product2.getDescription()); System.out.println("Price: " + product2.getPriceFormatted()); System.out.println(""); } public static Product parseString (String inputString, Product inputProduct){ // use String class 'index' amd substring methods to parse the input string parameter // and then set the properties of the inputProduct object return inputProduct; } }

product.java

package murach.business;

import java.text.NumberFormat;

public class Product {

private String code; private String description; private double price;

public Product() { code = ""; description = ""; price = 0; }

public Product(String code, String description, double price) { this.code = code; this.description = description; this.price = price; }

public void setCode(String code) { this.code = code; }

public String getCode() { return code; }

public void setDescription(String description) { this.description = description; }

public String getDescription() { return description; }

public void setPrice(double price) { this.price = price; }

public double getPrice() { return price; }

public String getPriceFormatted() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return currency.format(price); } }

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

More Books

Students also viewed these Databases questions

Question

Financial management involves

Answered: 1 week ago