The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 2 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
1. Write a class named RetailItem that holds data about an item in a retail store, include the following fields:
-description: references a String object that holds a brief description of the item.
-unitsOnHand: an int variable that holds the number of units currently in inventory.
-price: a double variable that holds the item's retail price.
-DISCOUNT: a constant static attribute double and set to 0.20 or 20%
2. Write the following for each field (description, unitsOnHand, price):
Constructor. Accepts arguments for each field
Mutator methods. Stores values in each field.
Accessor methods. Returns values in each field.
applyDiscount()Â - Computes the new price of the item using the constant (DISCOUNT) as above.
toString()Â - Displays the description, unitsOnHand, price, and the new price after the discount is applied.
3. Write a separate class named RetailItemDriver (which includes main() )
- Reads in 3 RetailItem objects (this is read in from the "circleData.txt" ) and stores their data..
- Create a getRetailItem() method to read data for the 3 RetailItem objects and the create the objects one at a time while returning the object reference.
- Write a display()Â method that displays one object at a time, that is display the current state of the object. In the display method, display a new price of the item after the discount is applied using the
applyDiscount()Â method, one at a time.
---
I'm having trouble reading the data in from a file. Â I got a little lost trying to write this Java program. Â I have attached my RetailItem.java & RetailItemDriver.java files, as well as the retailItem.txt data file.RetailItem.java
public class RetailItem {
private String description;
private int units;
private double price;
private static final double DISCOUNT = 0.20;
public RetailItem() {
description = "";
units = 0;
price = 0.0;
}
public RetailItem(String description, int unit, double price)
{
this.description = description;
this.units = units;
this.price = price;
}
public void setDescription(String description)
{
this.description = description;
}
public void setPrice(double price)
{
this.price = price;
}
public void setUnits(int units) //Accessor for units on hand
{
this.units = units;
}
public int getUnits() {
return units;
}
public String getDescription() {
return description;
}
public double getPrice() {
return price;
}
public double applyDiscount(double getPrice) {
double amountDiscount = getPrice * DISCOUNT;
return (getPrice - amountDiscount);
}
public String toString() {
String str = "";
str += "Description: " + description;
str += "\nUnits: " + units;
str += "\nPrice: $" + price;
str += "\nPrice after Discount: $" + applyDiscount(price) + "\n";
return str;
}
}//end class
import java.io.*;
import java.util.Scanner;
class RetailItemDriver{
public static void main(String args) throws IOException {
File infile = new File("retailItems.txt");
if(!infile.exists()) {
System.out.println("File not found. Exiting...");
System.exit(0);
}//end find file
String readIn = "";
Scanner scan = new Scanner(infile);
//RetailItem w/ 3 objects
r1 = new RetailItem();
r2 = new RetailItem();
r3 = new RetailItem();
while(scan.hasNext()) {
readIn = scan.next();
//??? I'm so lost
}
//function call to set values
r3.setDescription();
r3.setUnits();
r3.setPrice();
//output data
System.out.println(r1 + "\n" + r2
}//end main + "\n" + r3); public static void displayAll(RetailItem items) {
for(RetailItem item: items)
System.out.println(item);
}
public static RetailItem getRetailObject() {
String des = scan.next();
int unit = scan.nextInt();
double price = scan.nextDouble();
} }//end class
-----------