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.
Related
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.
QUESTION: I'm trying to find the win percentage (the formula for win percentage is wins/(wins + loses)). How do I take the values from wins and loses the user enters and add them to my Sysout function. Every time I run the program it displays:
East W L PCT
Braves 45 66 0.000000
Cubs 87 77 0.000000
So what I'm trying to do is get the actual values instead of it saying "0.0000000"
public class Team {
// Data fields...
private int wins;
private int loses;
private String teamName;
private String city;
private String division;
private double winPercentage;
// Getters and setters...
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getLoses() {
return loses;
}
public void setLoses(int loses) {
this.loses = loses;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public double getWinPercentage() {
return wins/(wins + loses);
}
public void setWinPercentage(double winPercentage) {
this.winPercentage = winPercentage;
}
}
public class PlayoffSelectorClass extends Team{
public static void main(String[] args) {
List<Team> teams = new ArrayList<Team>();
for (int i = 0; i < 6; i++) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter team name: ");
String name = input.nextLine();
System.out.println("\nPlease enter the city " + name + " played in: ");
String city = input.nextLine();
System.out.println("\nPlease enter the division " + name + " play in: ");
String division = input.nextLine();
System.out.println("\nPlease enter the number of wins " + name + " has: ");
Integer wins = input.nextInt();
System.out.println("\nPlease enter the number of losses " + name + " has: ");
Integer loses = input.nextInt();
if (i < 5) {
System.out.println("\nEnter your next team...\n");
}
Team team = new Team();
team.setTeamName(name);
team.setCity(city);
team.setDivision(division);
team.setWins(wins);
team.setLoses(loses);
team.setWinPercentage(wins / (wins + loses));
teams.add(team);
}
System.out.println("East W L PCT\n");
for (Team team : teams) {
System.out.printf("%s\t%s\t%s\t%f\n",team.getTeamName() + " ", team.getWins() + " " , team.getLoses(), team.getWinPercentage());
}
}
}
The problem is here :
public double getWinPercentage() {
return wins/(wins + loses);
}
You are doing integer division, i.e only keep the int part of the result, and implicitly cast it to a double. So for example if:
wins = 10;
loses = 20;
then winPercentage == 10/30 == 0.33.. and you only keep the 0 and the cast it to double. Hence the 0.0000... etc What you can do instead is:
public double getWinPercentage() {
return wins/(double) (wins + loses);
}
I am completed the whole program, and it is working fine. But, I am having trouble with the StringTokenizer. I have class named Worker.java and a test class named TestWorker.java...
In the test class I have calculated and printed out the total hoursworked, grossSalary and etc..
But I am stuck in 1 position, that is I have to store name,id, and hourly rate in array, and then using the StringTokenizer, I have to prompt the user to enter worker ID and hourly rate.
When the user enter the user worker name and ID, the program output will show the user the hourly rate of the worker...
or in other words...
Allow the user to enter worker-ID and Hours repeatedly until user enter and empty string. Read the values and invoke the methods addWeekly() on the appropriate object(by searching through the array to locate the object with the specified ID). If a nonexistent ID is entered display an appropriate error message.
Please see my codings below and modify as your needs....
//Worker.java
public class Worker {
public final double bonus=100;
protected String name;
protected String workerID;
protected double hourlyRate;
protected double hoursWorked;
protected double weekHour;
protected double tax;
protected double grossSalary;
protected double netSalary;
public Worker() {
}
public Worker(String name,String workerID,double hourlyRate){
this.name = name;
this.workerID = workerID;
this.hourlyRate = hourlyRate;
}
public void addWeekly(double weekHour){
hoursWorked = hoursWorked + weekHour;
}
public double gross()
{
grossSalary = (hoursWorked*hourlyRate);
if(hoursWorked>=150)
{
grossSalary = grossSalary +100;
}
return grossSalary;
}
public double taxAndNet()
{
tax = (grossSalary - 500) * 0.3;
netSalary = (grossSalary-tax);
return netSalary;
}
public String getName()
{
return name;
}
public String getWorkerID()
{
return workerID;
}
public double getHourly()
{
return hourlyRate;
}
public double getTotalHours()
{
return hoursWorked;
}
public double getGrossSalary()
{
return grossSalary;
}
public void addToGross(double amt)
{
grossSalary = grossSalary + amt;
}
public void displaySalary()
{
System.out.println("Name :" + name + "\nID :" + workerID +"\nHourly rate : "+ hourlyRate +"\nTotal Hours Worked " + hoursWorked +"\nGross Pay: "+getGrossSalary()+ "\nTax : "+tax +"\nNetpay :" +netSalary);
}
}
//TestWorker.java
import java.util.StringTokenizer;
import java.util.Scanner;
public class TestWorker {
public static void main(String args[]) {
Worker e = new Worker("John Major","s123",15.0);
e.addWeekly(45);
e.addWeekly(40);
e.addWeekly(32);
e.addWeekly(38);
e.gross();
e.taxAndNet();
e.displaySalary();
Worker[] worklist = new Worker[5];
worklist[0] = new Worker("Richard Cooper","s1235",20.0);
worklist[1] = new Worker("John Major","s1236",18.0);
worklist[2] = new Worker("Mary Thatcher","s1237",15.0);
worklist[3] = new Worker("David Benjamin","s1238",16.0);
worklist[4] = new Worker("Jack Soo","s1239",25.0);
Scanner input = new Scanner(System.in);
String name;
do{
System.out.print("Please enter your name and worker ID: ");
name = input.nextLine();
StringTokenizer string = new StringTokenizer(name,"+");
System.out.println("******|||||*******");
while(string.hasMoreElements()){
System.out.println(string.nextElement());
}
}
while(name.isEmpty()==false);
String s = "Five+Three=Nine-One";
String arr[];
//declare it with 3 tokens as seen above:
StringTokenizer st = new StringTokenizer(s, "+=-");
//the array size is the number of tokens in the String:
arr = new String[st.countTokens()];
//when there are still more tokens, place it in the array:
int i = 0;
while(st.hasMoreTokens()){
arr[i] = st.nextToken();
i++;
}
//determine the word with the largest length:
int indexMax = 0;
for( i = 1; i < arr.length; i++){
if(arr[i].length() > arr[indexMax].length())
indexMax = i;
}
System.out.println("The largest element is in index: "
+ indexMax);
} //main
} //class
Please tell us your java version. Since jdk 1.4.2 you should use String.split(...) instead of the old Stringtokenizer. Check out this tutorial TIP: A little tutorial about String.split();
I have programmed a Worker class and the driverclass for Worker..
My working class is compiling fine and showing the desired output..
But in array and StringTokenizer I am facing the main problem... And in line 32 of TestWorker.java there is an error, and I donot know why is it giving
The question is...
(i)Declare an array that can store the references of up to 5 Worker objects constructed with the data(name,workerID and hourlyRate).
(ii)And now allow the users to enter workerID and hours repeatedly until user enter an empty string. Read these values and invoke the method addWeekly() on the appropriate object (by searching through the array to locate the object with the specified ID). If a nonexistent ID is entered, display an appropriate error message.
(iii)Compute and display the salary for all the workers
Please see my codes below.....
//Worker.java
public class Worker {
public final double bonus=100;
protected String name, workerID;
protected double hourlyRate, totalHoursWorked,tax,grossSalary,netSalary;
public Worker(){
}
public Worker(String name, String workerID, double hourlyRate){
this.name = name;
this.workerID = workerID;
this.hourlyRate = hourlyRate;
}
public void addWeekly(double hoursWorked){
this.totalHoursWorked = this.totalHoursWorked + hoursWorked;
}
public double gross(){
grossSalary = (totalHoursWorked*hourlyRate);
if(totalHoursWorked>=150){
grossSalary = grossSalary +100;
}
return grossSalary;
}
public double netAndTax(){
netSalary = grossSalary;
if(grossSalary>500){
tax = (grossSalary - 500) *0.3;
netSalary = (grossSalary - tax);
}
return netSalary;
}
public String getName(){
return this.name;
}
public String getWorkerID(){
return this.workerID;
}
public double getHourlyRate(){
return this.hourlyRate;
}
public double getTotalHours(){
return totalHoursWorked;
}
public double getGrossSalary(){
return grossSalary;
}
public void addToGross(double amt){
grossSalary = grossSalary + amt;
}
public void displaySalary(){
System.out.print("Name: " +getName() + "\nID :" + getWorkerID()
+ "\nHourly Rate: " + getHourlyRate()+ "\nTotalHours Worked" + getTotalHours() +
"\nGross pay" + getGrossSalary() + "\nTax: " + netAndTax() +
"\nNet Pay: " + netAndTax());
}
}
//TestWorker.java
import java.util.Scanner;
import java.util.StringTokenizer;
public class TestWorker {
public static void main(String args[]){
Worker e = new Worker("John Major","s123",15.0);
e.addWeekly(45);
e.addWeekly(40);
e.addWeekly(32);
e.addWeekly(38);
e.gross();
e.netAndTax();
e.displaySalary();
Worker[] worklist = new Worker[5];
worklist[0] = new Worker("Richard Cooper","s1235",20.0);
worklist[1] = new Worker("John Major","s1236",18.0);
worklist[2] = new Worker("Mary Thatcher","s1237",15.0);
worklist[3] = new Worker("David Benjamin","s1238",16.0);
worklist[4] = new Worker("Jack Soo","s1239",25.0);
Scanner input = new Scanner(System.in);
String name;
double hourly;
do{
System.out.println("\n");
System.out.print("Please Enter ID and hours-worked in a given week: ");
name = input.nextLine();
StringTokenizer string = new StringTokenizer(name,"+");
String[] array =new String[(string.countTokens)];
for(int i=0;i<=4;i++){
if(array[0].equals(worklist[0])){
e.getName();
e.getWorkerID();
e.addWeekly(Double.parseDouble(array[0]));
e.gross();
e.displaySalary();
}
else if(array[0].equals(worklist[1])){
e.getName();
e.getWorkerID();
e.addWeekly(Double.parseDouble(array[1]));
e.gross();
e.displaySalary();
}
else if(array[0].equals(worklist[2])){
e.getName();
e.getWorkerID();
e.addWeekly(Double.parseDouble(array[2]));
e.gross();
e.displaySalary();
}
else if(array[0].equals(worklist[3])){
e.getName();
e.getWorkerID();
e.addWeekly(Double.parseDouble(array[3]));
e.gross();
e.displaySalary();
}
else if(array[0].equals(worklist[4])){
e.getName();
e.getWorkerID();
e.getHourlyRate();
e.addWeekly(Double.parseDouble(array[4]));
e.gross();
e.displaySalary();
}
else{
System.out.println("Please Enter correct information");
}
}
System.out.println();
while(string.hasMoreElements()){
System.out.println(string.nextElement());
}
}
while(name.isEmpty()==false);
}
}
Firstly, you've specified countTokens as a field when it is actually a method on this line:
String[] array = new String[(string.countTokens)]; // Incorrect
String[] array = new String[(string.countTokens())]; // Correct
Now you seem to be splitting the input on a + sign? So you're expected input is something along the lines of s123+12 which would mean ID s123 worked 12 hours this week. So in order to solve something like you probably want a loop that looks like this:
Scanner input = new Scanner(System.in);
System.out.println("\n");
System.out.print("Please Enter ID and hours-worked in a given week: ");
String enteredString = input.nextLine();
while (!enteredString.isEmpty()) {
StringTokenizer stringtok = new StringTokenizer(enteredString, "+");
String id = stringtok.nextToken();
Double hours = Double.parseDouble(stringtok.nextToken());
for (int i = 0; i < 5; i++) {
if (worklist[i].getWorkerID().equals(id)) {
worklist[i].addWeekly(hours);
break;
}
}
System.out.println("\n");
System.out.print("Please Enter ID and hours-worked in a given week: ");
enteredString = input.nextLine();
}
for (int i = 0; i < 5; i++) {
worklist[i].gross();
worklist[i].netAndTax();
worklist[i].displaySalary();
}
Keep in mind that this is very rough and I'm not entirely clear from your instructions on what exactly you are trying to achieve.
Why not consider String.split() name.split("\\+") that will give you a string array
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;
}