Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

1. Open the project named ch08_ex3_ProductParser.

2. 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 fields delimited by both a colon (:) and semicolon (;). However, this code doesnt parse this string and store the data in the Product object.

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

Add the code that parses the string

4. In the Main class, examine the parseString method. The parseString method has a String parameter name inputString and a Product parameter name inputProduct that are used as local variables in the method.

Add code to the parseString method that uses the String class index methods to parse the string parameter into the code, description, and price values. Then, store this data in the inputProduct Product object that is returned by the method.

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

Code: java

Description: Murach's Java Programming

Price: $57.50

Code: mysql

Description: Murach's MySQL

Price: $ 54.50

-----------------------------------------------------------------------------------------------------------------------------------------------------------

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); } }

-----------------------------------------------------------------------------------------------------------------------------------------------------------

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; } }

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

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions