Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMMING Assignment #1 for OO Programming Your assignment is to write a

ID: 638580 • Letter: J

Question

JAVA PROGRAMMING

Assignment #1 for OO Programming

Your assignment is to write a program that computes the total cost of purchasing a variable number of three different items. Your program reads in three decimal numbers (item costs) and three integers (the number of each item purchased) from the console. You are to multiply the decimal numbers representing the costs times the integer specifying the number of that item purchased for each item purchased to get the total cost of each item. You must then compute the sum of these three costs and print it out on the console in US currency format with the label:

Explanation / Answer

Program that computes the total cost of purchasing a variable number of three different items:

Main File :

package inventory2;
import java.util.Scanner;

public class RunApp
{
public static void main(String[] args)
{

Scanner input = new Scanner( System.in );

Items theItem = new Items();

int number;
String Name = "";

System.out.print("How many items are to be put into inventory count?: ");
number = input.nextInt();
input.nextLine();

Items[]inv = new Items[number];


for(int count = 0; count < inv.length; ++count)
{
System.out.print(" What is item " +(count +1) + "'s name?: ");
Name = input.nextLine();
theItem.setName(Name);

System.out.print("Enter " + Name + "'s product number: ");
double pNumber = input.nextDouble();
theItem.setpNumber(pNumber);

System.out.print("How many " + Name + "s are there in inventory?: ");
double Units = input.nextDouble();
theItem.setUnits(Units);

System.out.print(Name + "'s cost: ");
double Price = input.nextDouble();
theItem.setPrice (Price);

inv[count] = new Items(Name, Price, Units, pNumber);
input.nextLine();

System.out.print(" Product Name: " + theItem.getName());
System.out.print(" Product Number: " + theItem.getpNumber());
System.out.print(" Amount of Units in Stock: " + theItem.getUnits());
System.out.print(" Price per Unit: " + theItem.getPrice() + " ");
System.out.printf(" Total cost for %s in stock: $%.2f", theItem.getName(), theItem.calculateTotalPrice());
System.out.printf("Total Cost for all items entered: $%.2f", theItem.calculateTotalPrice()); //prompt for output to show total price for all items in array
}
}
}

Second Class:

package inventory2;

public class Items
{
private String Name;
private double pNumber, Units, Price;

public Items()
{
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
}

//constructor
public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
{
Name = productName;
pNumber = productNumber;
Units = unitsInStock;
Price = unitPrice;
}
//setter methods
public void setName(String n)
{
Name = n;
}

public void setpNumber(double no)
{
pNumber = no;
}

public void setUnits(double u)
{
Units = u;
}

public void setPrice(double p)
{
Price = p;
}

//getter methods
public String getName()
{
return Name;
}

public double getpNumber()
{
return pNumber;
}

public double getUnits()
{
return Units;
}

public double getPrice()
{
return Price;
}

public double calculateTotalPrice()
{
return (Units * Price);
}

public double calculateAllItemsTotalPrice() //method to calculate total cost for all items in array
{
return (TotalPrice );
}