Hello All,
I am working on a project for class and I have everything work except for one little thing. I can compile there is no issues with running the program. However, when get prints the information I get a null as the value for the Mobile OS. I do not expect anyone to do my homework for me, just seeking advice. To bring you up to speed, this week we had to extend a class and add an additional feature to that class. I will post my code below for visualization of what I am working towards.
Orginally it wasn't printing anything, becuase I had it set to printf. I changed it to print then I started to see null. I have looked through this over and over I just don't see where I am going wrong.
import java.util.Scanner;
class Mobile {
private String name;
private int number;
private int quantity;
private double cost;
public Mobile(String name, int number, int quantity, double cost) {
this.name = name;
this.number = number;
this.quantity = quantity;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getValue() {
return cost * quantity;
}
}
class SmartPhone extends Mobile {
private String mobileOS;
public SmartPhone(String name, int number, int quantity, double cost,
String mobileOS) {
super(name, number, quantity, cost);
}
@Override
public double getValue()
{
return super.getValue()+ getRestockFee();
}
public double getRestockFee() {
return getQuantity() * getCost() * 0.05;
}
public String getMobileOS() {
return mobileOS;
}
public void setMobileOS(String mobileOS) {
this.mobileOS = mobileOS;
}
}
public class MobilePhone {
public static void main(String args[]) {
String itemName;
String mobileOS;
int itemNum;
int itemQuan;
double unitCost;
SmartPhone mobileValues[] = new SmartPhone[5];
for (int i = 0; i < 5; i++) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Item Name: ");
itemName = input.nextLine();
System.out.print("Enter Mobile OS: ");
mobileOS = input.nextLine();
System.out.print("Enter Item Number: ");
itemNum = input.nextInt();
System.out.print("Enter Quantity of Item: ");
itemQuan = input.nextInt();
System.out.print("Enter Price of Single Unit: ");
unitCost = input.nextDouble();
System.out.println();
mobileValues[i] = new SmartPhone(itemName, itemNum, itemQuan, unitCost, mobileOS);
}
for (int i = mobileValues.length - 1; i > 0; i--)// begin of bubble sort
// for array
{
for (int j = 0; j < i; j++) {
if (mobileValues[j].getName().compareToIgnoreCase(
mobileValues[j + 1].getName()) > -1) {
SmartPhone temp = mobileValues[j];
mobileValues[j] = mobileValues[j + 1];
mobileValues[j + 1] = temp;
}
}
}// end of bubble sort
double overAll = 0;// begin find overall value
for (int i = 0; i < 5; i++) {
overAll = overAll + mobileValues[i].getValue();
}// end of algorthim for finding overall value
for (int i = 0; i < 5; i++) {
SmartPhone m = mobileValues[i];
System.out.println();
System.out.println("Electronics Department");
System.out.println("Product: " + m.getName());
System.out.println("Mobile OS: "+ m.getMobileOS());
System.out.println("Item Number: " + m.getNumber());
System.out.println("Quantity: " + m.getQuantity());
System.out.printf("Unit Cost: $%.2f\n", m.getCost());
System.out.printf("Restock Fee: $%.2f\n", m.getRestockFee());
System.out.printf("Inventory Value: $%.2f\n", m.getValue());
System.out.printf("Overall Inventory Value: $%.2f\n", overAll);
}
}
}