I've been working on my shopping cart program, but I keep having problems with entering itemTax in and with adding new items into cart and I don't know what I've done wrong.
class Item:
public class Item {
private int id;
private String name;
private double price;
private String description;
private int quantity;
private double tax;
public Item (int itemID, String itemName, double itemPrice, String itemDescription, int itemQuantity, double itemTax){
id = itemID;
name = itemName;
price = itemPrice;
description = itemDescription;
quantity = itemQuantity;
tax = itemTax;
}
public int getID(){
return id;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public String getDescription(){
return description;
}
public int getQuantity(){
return quantity;
}
public double getTax(){
return tax;
}
}
class Cart:
import java.util.Scanner;
public class Cart {
private int itemCount;
private double totalPrice;
private static int capacity;
private static Item[] cart = new Item[capacity];
public Cart(){
itemCount = 10;
totalPrice = 0.0;
capacity = 0;
}
public void add(int itemID, String itemName, double itemPrice, String itemDescription, int itemQuantity, double itemTax){
Item item = new Item(itemID, itemName, itemPrice, itemDescription, itemQuantity, itemTax);
totalPrice += (itemPrice * itemQuantity);
cart[itemCount] = item;
itemCount += 1;
if(itemCount==capacity)
{
increaseSize();
}
}
public static void remove(String itemName){
Scanner s = new Scanner(System.in);
for (int i = 0; i < cart.length; i++) {
Item remove = (Item) cart.get(i);
if (itemName.equals(remove.getName())) {
cart.remove(i);
}
}
System.out.println("\n" + "Item " + itemName + " wasn't found.");
}
private void increaseSize()
{
Item[] item = new Item[capacity+5];
for(int i=0; i < capacity; i++)
{
item[i] = cart[i];
}
cart = item;
item = null;
capacity = cart.length;
}
public static void prLine (int itemID, String itemName, int itemQuantity, double itemPrice, double total, double itemTax) {
System.out.printf("\n%-10.10d %30s %10.2f %10d %10.2f", itemID, itemName, itemPrice, itemQuantity, itemTax, total);
}
public static void prTitles () {
System.out.printf("\n%-10s 30% %10s %10s %10s %10s", "ID", "Item", "Price", "Quantity", "Tax", "Total");
}
}
class Shop:
import java.util.ArrayList;
import java.util.Scanner;
public class Shop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Item> cart = new ArrayList<Item>();
Item item;
int itemID;
String itemName;
double itemPrice;
String itemDescription;
int itemQuantity;
double itemTax;
int ch;
String choice;
Cart shoppingCart = new Cart();
while (true) {
System.out.println("Menu:");
System.out.println("0) Exit " + "\n"
+ "1) Add item in shopping cart" + "\n"
+ "2) Remove item from shpping cart");
ch = sc.nextInt();
switch (ch) {
case 0:
System.out.println("\n" + "Good bye!");
System.exit(0);
case 1:
System.out.println("Enter item ID: ");
itemID = sc.nextInt();
System.out.println("Enter item name: ");
itemName = sc.next();
System.out.println("Enter item price: ");
itemPrice = sc.nextDouble();
System.out.println("Enter short description of item: ");
itemDescription = sc.next();
System.out.println("Enter quantity: ");
itemQuantity = sc.nextInt();
System.out.println("Enter tax rate:");
itemTax = sc.nextDouble();
shoppingCart.add(itemID, itemName, itemPrice, itemDescription, itemQuantity, itemTax);
break;
case 2:
System.out.println("Enter name of the item that you would like to remove: ");
choice = sc.next();
shoppingCart.remove(choice);
break;
}
}
}
}
This block of code looks...suspect...
private static Item[] cart = new Item[capacity];
public Cart(){
itemCount = 10;
totalPrice = 0.0;
capacity = 0;
}
You provide no other way to instantiate the Cart, so every time you new one up, you have a cart of size 0. Nothing could be added to that.
I'm willing to bet that, semantically, you mean to do something like this:
private Item[] cart;
public Cart(){
itemCount = 0;
totalPrice = 0.0;
capacity = 10;
cart = new Item[capacity];
}
You have a capacity of ten items now, and itemCount should be used in place of capacity in your add method to move the elements into their appropriate spot.
Related
I'm a beginner in java. I have a problem with my code. I want to design my program but I stuck in ArrayList. How can I add a new price and display all food has price smaller or equals than the price of a new food? and sort the list f by price. Help me please, thank so much.
1.accept a new price named p: list all Food which have price <= p
2.sort list f ascending by price and output list after sorting
public class Food {
//states
private String name;
private double price;
//accessor
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
//mutator
public String getName() {
return name;
}
public double getPrice() {
return price;
}
//methods
public Food() {
name = "";
price = 0;
}
public Food(String name, double price) {
this.name = name;
this.price = price;
}
public double getSalePrice() {
double tax = 0.07;//7%;
if(name.toLowerCase().startsWith("k")) tax = 0.05;
return price + price * tax;
}
public void print() {
System.out.printf("%-20s%-10.1f ($)%-10.1f ($)\n",
name,price,getSalePrice());
//System.out.println(String.format("%-20s%-10.1f ($)%-10.1f ($)\n",
// name,price,getSalePrice()));
}
public int listp(ArrayList<Food> f, int priceP) {
int s,i,n;
n = f.size();
s = 0;
for(i=0;i<n;i++) {
if(f.get(i).price > priceP) s++;
}
return(s);
}
}
public class Main {
public static void main(String[] args) {
//a list of food
Scanner in = new Scanner(System.in);
final int MAX = 10;
Food [] f = new Food[MAX];
f[0] = new Food("BBQ",3.35);
f[1] = new Food("KFC",3.3);
f[2] = new Food("Ga 36",4.5);
int n = 3;
while(true) {
System.out.print("Enter food name: ");
String name = in.nextLine();
System.out.print("Enter food price: ");
double price = Double.parseDouble(in.nextLine());
f[n] = new Food();
//f[n].name = name;f[n].price = price;
f[n].setName(name);
f[n].setPrice(price);
n++;
System.out.print("Add more food (yes/no)? ");
String s = in.nextLine();
if(s.equalsIgnoreCase("no")) break;
}
//output list
for (int i = 0; i < n; i++) {
f[i].print();
}
System.out.print("Enter price of food p:");
double price = Double.parseDouble(in.nextLine());
System.out.println(listp(f,price));
}
public int listp(ArrayList<Food> f, int priceP) {
int s,i,n;
n = f.size();
s = 0;
for(i=0;i<n;i++) {
if(f.get(i).getPrice() > priceP) s++;
}
return(s);
}
}
Here is a working solution where the array was converted to a list and the listp method was changed to use streams which are really a good fit for your requirements:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//a list of food
Scanner in = new Scanner(System.in);
List<Food> f = new ArrayList<>();
f.add(new Food("BBQ", 3.35));
f.add(new Food("KFC", 3.3));
f.add(new Food("Ga 36", 4.5));
int n = 3;
while (true) {
System.out.print("Enter food name: ");
String name = in.nextLine();
System.out.print("Enter food price: ");
double price = Double.parseDouble(in.nextLine());
Food food = new Food();
//f[n].name = name;f[n].price = price;
food.setName(name);
food.setPrice(price);
f.add(food);
n++;
System.out.print("Add more food (yes/no)? ");
String s = in.nextLine();
System.out.println(s);
if (s.trim().equalsIgnoreCase("no")) break;
}
System.out.print("Enter price of food p:");
double price = Double.parseDouble(in.nextLine());
listp(f, price);
}
private static void listp(List<Food> f, double priceP) {
f.stream()
.filter(food -> food.getPrice() <= priceP)
.sorted(Comparator.comparingDouble(Food::getPrice))
.forEach(food -> System.out.printf("%s : %s%n", food.getName(), food.getPrice()));
}
}
Here is a variation of the listp method which allows you to limit the list by a number:
private static void listp(List<Food> f, double priceP, int limit) {
f.stream()
.filter(food -> food.getPrice() <= priceP)
.sorted(Comparator.comparingDouble(Food::getPrice))
.limit(limit)
.forEach(food -> System.out.printf("%s : %s%n", food.getName(), food.getPrice()));
}
This ensures that the printed out list is always as long as specified in the limit parameter.
Usage example:
listp(f, price, 2);
This will print a maximum of two items, even if the total list has more than that.
1.
public List<Food> findFoodWhichPriceLessThan(Collection<Food> listOfFood, double price){
return listOfFood.stream().filter(it -> it.getPrice() <= price).collect(Collectors.toList());
}
2.
public void sortFoodByPrice(List<Food> listOfFood){
Collections.sort(listOfFood, Comparator.comparingDouble(Food::getPrice));
}
or
public List<Food> sortFoodByPrice2(List<Food> listOfFood){
return listOfFood.stream().sorted(Comparator.comparingDouble(Food::getPrice)).collect(Collectors.toList());
}
Instead of the calculated output I get a "null" without the actual total printed output I am looking for. I'm thinking it's because Item isn't properly defined. Disregard shop.txt, it has no affect on my problem
package Shopping;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.NumberFormat;
public class Shop
{
public static void main(String[] args)
{
//I/O stream
String fileName = "shop.txt";
Scanner inputStream = null;
System.out.println("The file " + fileName +
"\ncontains the following lines:\n");
try
{
inputStream = new Scanner(new File(fileName));
}
catch (FileNotFoundException e)
{
System.out.println("This is a shopping list " +
fileName);
System.exit(0);
}
while (inputStream.hasNextLine())
{
String line = inputStream.nextLine();
System.out.println(line);
}
inputStream.close();
ArrayList<Item> Cart = new ArrayList<Item>();
Item item;
String itemName;
double itemPrice;
int quantity;
double totalPrice = 0.0;
double sum = 0.0;
int priority;
Scanner scan = new Scanner(System.in);
String continueShopping = "y";
do
{
System.out.print("Enter the name of the item: ");
itemName = scan.nextLine();
System.out.print("Enter the unit price: ");
itemPrice = scan.nextDouble();
System.out.print("Enter the quantity: ");
quantity = scan.nextInt();
// create a new item and add it to the cart
item = new Item(itemName, itemPrice, quantity);
Cart.add(item);
for (int i = 0; i < Cart.size(); i++)
{
Item itm = Cart.get(i);
System.out.println(itm);
}
// Print out the results
System.out.print("Continue shopping (y/n)? ");
scan.nextLine();
continueShopping = scan.nextLine();
}
while (continueShopping.equals("y"));
for (int i = 0; i < Cart.size(); i++)
{
Item itm = Cart.get(i);
System.out.println(itm);
totalPrice = itm.getQuantity() * itm.getPrice();
sum += totalPrice;
}
NumberFormat type = NumberFormat.getCurrencyInstance();
System.out.println("The total price is: " + type.format(sum));
}
}
Item class
package Shopping;
import java.text.NumberFormat;
public class Item
{
private String name;
private double price;
private int quantity;
public Item(String itemName, double itemPrice, int quantity2)
{
}
public void Item(String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
//Info about the item
public String toString()
{
NumberFormat type = NumberFormat.getCurrencyInstance();
return (name + "\t" + type.format(price) + "\t" + quantity + "\t"
+ type.format(price * quantity));
}
//Retrieve the item price
public double getPrice()
{
return price;
}
//Retrieve item name
public String getName()
{
return name;
}
//Retrieve quantity
public int getQuantity()
{
return quantity;
}
}
Your Item class has one empty constructor
public Item(String itemName, double itemPrice, int quantity2) {
}
and a method called Item
public void Item (String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
Remove the empty constructor and remove the return type void from the method to turn it into a constructor.
I have a main class Basket1 and Item for setter and getter, Please help me in writing the Junit for this project. I have to write a JUNIT to test my project. The JUnit test function can be a Scan test or getter setter one and also the sum of the Item.
package Basket;
import java.util.ArrayList;
import java.util.Scanner;
import java.text.NumberFormat;
class Basket1 {
public static void main(String[] args) {
ArrayList<Item> Cart = new ArrayList<Item>();
Item item;
String itemName;
double itemPrice;
int quantity;
Scanner scan = new Scanner(System.in);
double totalPrice = 0.0;
double sum = 0.0;
String keepShopping = "y";
do {
System.out.print("Enter the name of the item: ");
itemName = scan.nextLine();
System.out.print("Enter the unit price: ");
Double n1 = 0.0;
boolean bError = true;
while (bError) {
if (scan.hasNextDouble())
n1 = scan.nextDouble();
else {
scan.next();
System.out.print("Please Enter valid Price");
continue;
}
bError = false;
}
itemPrice = n1;
System.out.print("Enter the quantity: ");
int n2 = 0;
boolean tError = true;
while (tError) {
if (scan.hasNextInt())
n2 = scan.nextInt();
else {
scan.next();
System.out.print("Please Enter valid Unit");
continue;
}
tError = false;
}
quantity = n2;
item = new Item(itemName, itemPrice, quantity);
Cart.add(item);
for (int i = 0; i < Cart.size(); i++) {
Item itm = Cart.get(i);
System.out.println(itm);
}
System.out.print("You want to continue? Y/N ");
scan.nextLine();
keepShopping = scan.nextLine();
} while (keepShopping.equals("y"));
scan.close();
for (int i = 0; i < Cart.size(); i++) {
Item itm = Cart.get(i);
System.out.println(itm);
totalPrice = itm.getQuantity() * itm.getPrice();
sum += totalPrice;
}
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println("The total price is: " + fmt.format(sum));
}
}
package Basket;
import java.text.NumberFormat;
public class Item {
private String name;
private double price;
private int quantity;
public Item(String itemName, double itemPrice, int numPurchased) {
name = itemName;
price = itemPrice;
quantity = numPurchased;
}
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t" + fmt.format(price * quantity));
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
}
Instead of writing your basket code inside the main() method you should add a new class Basket which has calculation methods like addItem() and getSum() (along with other methods relevant for the Basket class). This new class will not have any user inputs or output messages, just a container for your items and calculations.
When you have such a class you have actually something you can write JUnit tests for. One test method would be "create a new basket, add some times, check that the sum of the items is the same as the expected one". Other tests would be like "throws an exception when the quantity is not positive" or "throws an exception when one argument is null".
OK so I can see the "setUniqueType" on line 33 and the "ItemDetails" on line 50 are what need to be corrected, but I can't figure out what's wrong with it and how to fix it. Please help so I can move in with my program...
MAIN
package inventory4;
import java.util.Scanner;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class RunApp
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ItemDetails theItem = new ItemDetails();
int number;
String Name = "";
String Type = "";
String sNumber = JOptionPane.showInputDialog(null,"How many items are to be put into inventory count?: ");
number = Integer.parseInt(sNumber);
ItemDetails[] inv = new ItemDetails[number];
for (int count = 0; count < inv.length; ++count)
{
Name = JOptionPane.showInputDialog(null,"What is item " + (count + 1) + "'s name?");
theItem.setName(Name);
Type = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product type");
theItem.setUniqueType(Type);
String spNumber = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product number");
double pNumber = Double.parseDouble(spNumber);
theItem.setpNumber(pNumber);
String sUnits = JOptionPane.showInputDialog(null,"How many " + Name + "s are there in inventory?");
double Units = Double.parseDouble(sUnits);
theItem.setUnits(Units);
String sPrice = JOptionPane.showInputDialog(null,Name + "'s cost");
double Price = Double.parseDouble(sPrice);
theItem.setPrice(Price);
inv[count] = new ItemDetails(Name, Price, Units, pNumber, Type);
}
for (int i = 0; i < inv.length; ++i)
{
JOptionPane.showMessageDialog(null, "Product Name" + inv[i].getName());
JOptionPane.showMessageDialog(null, "Product Type" + inv[i].getUniqueType());
JOptionPane.showMessageDialog(null, "Product Number" + inv[i].getpNumber());
JOptionPane.showMessageDialog(null, "Amount of Units in Stock" + inv[i].getUnits());
JOptionPane.showMessageDialog(null, "Price per Unit" + inv[i].getPrice());
JOptionPane.showMessageDialog(null, String.format("Total cost for %s in stock: $%.2f", inv[i].getName(), inv[i].calculateTotalPrice()));
JOptionPane.showMessageDialog(null,String.format("Restocking fee for %s is $%.2f", inv[i].getName(), inv[i].calculateRestock()));
String combinedData = inv[i].toString();
if(i == (inv.length -1)){
String lastItem = String.format("\nTotal Cost for all items entered: $%.2f\n", Items.getCombinedCost(inv));
combinedData = combinedData + lastItem;
JOptionPane.showMessageDialog(null, combinedData);
}else{
JOptionPane.showMessageDialog(null, combinedData);
}
} //end for
} //end main
} //end class
ITEMS
package inventory4;
public class Items implements Comparable
{
private String Name;
public double pNumber, Units, Price;
String allInfo;
public Items()
{
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
}
public int compareTo(Object item)
{
Items tmp = (Items)item;
return this.getName().compareTo(tmp.getName());
} // end compareTo method
public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
{
Name = productName;
pNumber = productNumber;
Units = unitsInStock;
Price = unitPrice;
}
public String toString()
{
StringBuffer allInfo = new StringBuffer();
allInfo.append(String.format("Name: %s\n pNumber: %.0f \n Units: %.0f \n Price: %.2f\n",
getName(),getpNumber(),getUnits(),getPrice()));
return allInfo.toString();
}
//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 static double getCombinedCost(Items[] item)
{
double combined = 0;
for (int i = 0; i < item.length; ++i)
{
combined = combined + item[i].calculateTotalPrice();
} //end loop
return combined;
} //end method
} //end class
ITEM DETAILS
package inventory4;
public class ItemDetails extends Items
{
private String UniqueType;
public ItemDetails()
{
super();
}
public String enterUniqueType()
{
return UniqueType;
}
public String getUniqueType()
{
return UniqueType;
}
public double calculateRestock() //setting up to calculate the restocking fee at 5%
{
return (Price * .05);
}
}
Compile errors:
RunApp.java:31: cannot find symbol
symbol : method setUniqueType(java.lang.String)
location: class ItemDetails
theItem.setUniqueType(Type);
^
RunApp.java:48: cannot find symbol
symbol : constructor ItemDetails(java.lang.String,double,double,double,java.lan
g.String)
location: class ItemDetails
inv[count] = new ItemDetails(Name, Price, Units, pNumber, Type);
^
2 errors
Your ItemDetails class has no setUniqueType method. You should add one.
The constructor you're using at :48 is "String, double, double, double, String" but your constructor is really only "String double double double" Either remove Type or add a String parameter to the end of your Item constructor, or make an ItemDEtails constructor with the five parameters.
Your ItemDetails has no constructor that matches that argument. If you want to chain the constructor, declare it in ItemDetails
Chaining the constructor:
public ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice
super(productName,productNumber,unitsInStock,unitPrice);
}
Regarding ItemDetails: it looks like your ItemDetails constructor, inherited from Items, takes only 4 args and you're passing it 5.
There are a few things I would like some help on. First and foremost, when I compile, my product number and price get mixed up. Why? Secondly, why does the product type always return null? I would also like to combine all the message boxes but every attempt I have made fails. If someone could lead me in the right direction, I would appreciate it. Here is my code:
MAIN
package inventory4;
import java.util.Scanner;
import java.util.Arrays; //Needed to include data for arrays
import javax.swing.JOptionPane; //JOptionPane import tool
public class RunApp
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ItemDetails theItem = new ItemDetails();
int number;
String Name = "";
String Type = "";
String sNumber = JOptionPane.showInputDialog(null,"How many items are to be put into inventory count?: ");
number = Integer.parseInt(sNumber);
ItemDetails[] inv = new ItemDetails[number];
for (int count = 0; count < inv.length; ++count)
{
Name = JOptionPane.showInputDialog(null,"What is item " + (count + 1) + "'s name?");
theItem.setName(Name);
Type = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product type");
String spNumber = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product number");
double pNumber = Double.parseDouble(spNumber);
theItem.setpNumber(pNumber);
String sUnits = JOptionPane.showInputDialog(null,"How many " + Name + "s are there in inventory?");
double Units = Double.parseDouble(sUnits);
theItem.setUnits(Units);
String sPrice = JOptionPane.showInputDialog(null,Name + "'s cost");
double Price = Double.parseDouble(sPrice);
theItem.setPrice(Price);
inv[count] = new ItemDetails(Name, Price, Units, pNumber);
}
for (int i = 0; i < inv.length; ++i)
{
JOptionPane.showMessageDialog(null, "Product Name: " + inv[i].getName());
//Why can't I use this instead of having multiple boxes?:
//JOptionPane.showMessageDialog(null, "Product Name: \nProduct Type: \nProduct Number: \nUnits in Stock: \nPrice Per Unit: " + inv[i].getName() + inv[i].getUniqueType() + inv[i].getpNumber() + inv[i].getUnits(), + inv[i].getPrice());
JOptionPane.showMessageDialog(null, "Product Type: " + inv[i].getUniqueType());
JOptionPane.showMessageDialog(null, "Product Number: " + inv[i].getpNumber());
JOptionPane.showMessageDialog(null, "Amount of Units in Stock: " + inv[i].getUnits());
JOptionPane.showMessageDialog(null, "Price per Unit: " + inv[i].getPrice());
JOptionPane.showMessageDialog(null, String.format("Total cost for %s in stock: $%.2f", inv[i].getName(), inv[i].calculateTotalPrice()));
JOptionPane.showMessageDialog(null,String.format("Restocking fee for %s is $%.2f", inv[i].getName(), inv[i].calculateRestock()));
String combinedData = inv[i].toString();
if(i == (inv.length -1)){
String lastItem = String.format("\nTotal Cost for all items entered: $%.2f\n", Items.getCombinedCost(inv));
combinedData = combinedData + lastItem; //combine total value to the end of the last object output
JOptionPane.showMessageDialog(null, combinedData); //Show Message
}else{
JOptionPane.showMessageDialog(null, combinedData); //Show Message
}
} //end for
} //end main
} //end class
ITEMS
package inventory4;
public class Items implements Comparable
{
private String Name;
public double pNumber, Units, Price;
String allInfo;
public Items()
{
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
}
public int compareTo(Object item)
{
Items tmp = (Items)item;
return this.getName().compareTo(tmp.getName());
} // end compareTo method
public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
{
Name = productName;
pNumber = productNumber;
Units = unitsInStock;
Price = unitPrice;
}
public String toString()
{
StringBuffer allInfo = new StringBuffer();
allInfo.append(String.format("Name: %s\n pNumber: %.0f \n Units: %.0f \n Price: %.2f\n",
getName(),getpNumber(),getUnits(),getPrice()));
return allInfo.toString();
}
//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 static double getCombinedCost(Items[] item) //This is used to set up the method
{
double combined = 0; //Loops through array after array is complete
for (int i = 0; i < item.length; ++i)
{
combined = combined + item[i].calculateTotalPrice(); //Sets up to combine all TotalPrice
//calculations in array
} //end loop
return combined;
} //end method
} //end class
ITEM DETAILS
package inventory4;
public class ItemDetails extends Items
{
private String UniqueType;
public ItemDetails()
{
super();
}
public ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice)
{
super(productName,productNumber,unitsInStock,unitPrice);
}
public String enterUniqueType()
{
return UniqueType;
}
public String setUniqueType()
{
return UniqueType;
}
public String getUniqueType()
{
return UniqueType;
}
public double calculateRestock() //setting up to calculate the restocking fee at 5%
{
return (Price * .05);
}
}
// getter???
public String setUniqueType() {
return UniqueType;
}
should be:
//setter
public void setUniqueType(String type) {
UniqueType = type;
}
and
inv[count] = new ItemDetails(Name, Price, Units, pNumber);
should be:
inv[count] = new ItemDetails(Name, pNumber, Units,Price );//look at the order
inv[count].setUniqueType(Type);//you have not set it.
I would also like to combine all the message boxes…
As an example, see JOptionPaneTest.
First and foremost, when I compile, my product number and price get mixed up. Why?
You're creating a new ItemDetails object with the call to
new ItemDetails(Name, Price, Units, pNumber);
but your constructor for ItemDetails is
ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice)
i.e., it takes the product number first and price last, not the other way around
Secondly, why does the product type always return null?
You're never actually setting your type, both your set and get methods do the same thing! That the setter is returning a value should've been a warning!
public String setUniqueType()
{
return UniqueType;
}
public String getUniqueType()
{
return UniqueType;
}
This is what it should be
public void setUniqueType(String type)
{
this.UniqueType = type;
}