I have an inventory program written to include an array and a method to calculate total cost for all inventory items entered. I now have to include a subclass that overrides the original to include "one unique feature". I created a new file named ItemDetails to set up for the subclasses of the original Item. I need to include one unique feature and calculate the value of the inventory and calculate a 5% restocking fee in this subclass. Do I just transfer some of the relevant lines into the other class? Or do I write some code twice? I don't know what to do next. Any help is useful. Thanks. This is what I have so far:
package inventory3;
public class ItemDetails extends Items
{
public static void override()
{
private String Name;
private double pNumber, Units, Price;
public ItemDetails()
{
}
}
}
This is the Item class file that it is supposed to override:
package inventory3;
import java.lang.Comparable;
public class Items implements Comparable
{
private String Name;
private double pNumber, Units, Price;
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());
}
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 static double getCombinedCost(Items[] item)
{
double combined = 0;
for(int i =0; i < item.length; ++i)
{
combined = combined + item[i].calculateTotalPrice();
}
return combined;
}
}
You simply declare a method with the same signature as the method in the parent class. So yours would look like:
package inventory3;
public class ItemDetails extends Items {
private String Name;
private double pNumber, Units, Price;
public ItemDetails(String Name, double pNumber, double Units, double Price) {
this.Name = Name;
this.pNumber = pNumber;
this.Units = Units;
this.Price = Price;
}
// getters and setters....
// The #Override is optional, but recommended.
#Override
public double calculateTotalPrice() {
return Units * Price * 1.05; // From my understanding this is what you want to do
}
}
Related
A1. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call.
A2. Also, include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstracts get methods—one that returns the phone number, another that returns the price of the call, and a third that displays information about the call.
A3. Create two child classes of PhoneCall: IncomingPhoneCall and OutgoingPhoneCall.
The IncomingPhoneCall constructor passes its phone number parameter to its parent’s constructor and sets the price of the call to 0.02. The method that displays the phone call information displays the phone number, the rate, and the price of the call (which is the same as the rate).
-----The OutgoingPhoneCall class includes an additional field that holds the time of the call in minutes. The constructor requires both a phone number and the time. The price is 0.04 per minute, and the display method shows the details of the call, including the phone number, the rate per minute, the number of minutes, and the total price.
---Write an application that demonstrates you can instantiate and display both IncomingPhoneCall and OutgoingPhoneCall objects. Save the files as PhoneCall.java, IncomingPhoneCall.java, OutgoingPhoneCall.java, and DemoPhoneCalls.java.
I have done only that below:
public abstract class PhoneCall
{
private String phoneno;
private double price;
public PhoneCall(String phoneno, double price)
{
this.phoneno = phoneno;
this.price = price;
}
public String getphoneno()
{
return phoneno;
}
public double getprice()
{
return price = 0.0;
}
public static void main(String[] args) {
}
}
public class IncomingPhoneCall extends PhoneCall
{
public IncomingPhoneCall(String phoneno, double price) {
super(phoneno, price);
}
}
public class OutgoingPhoneCall extends PhoneCall
{
public OutgoingPhoneCall(String phoneno, double price) {
super(phoneno, price);
}
}
abstract class PhoneCall
{
String phoneNumber;
double price;
PhoneCall(String phoneNumber)
{
this.phoneNumber = phoneNumber;
this.price = 0.0;
}
public String getPhoneNumber() {
return phoneNumber;
}
public double getPrice() {
return price;
}
public abstract void setPrice();
}
class IncomingPhoneCall extends PhoneCall {
final static double RATE=0.02;
IncomingPhoneCall(String phoneNumber){
super(phoneNumber);
setPrice();
}
public void setPrice() {
price = 0.02;
}
void info(){
System.out.println("Incoming phone call"+getPhoneNumber()+
" "+RATE+" per call.Total is $"+getPrice());
}
public String getPhoneNumber()
{
return phoneNumber;
}
public double getPrice()
{
return price;
}
}
class OutgoingPhoneCall extends PhoneCall {
final static double RATE = 0.04;
private int minutes;
OutgoingPhoneCall(String phoneNumber, int minutes){
super(phoneNumber);
this.minutes = minutes;
setPrice();;
}
public void setPrice() {
price = 0.04;
}
void info() {
System.out.println("Outgoing phone call " + getPhoneNumber() + " "
+ RATE + " per minute at " + minutes + " minutes. Total is $" + price*minutes);
}
public String getPhoneNumber()
{
return phoneNumber;
}
public double getPrice()
{
return price;
}
}
public class DemoPhoneCalls {
public static void main(String [] args) {
IncomingPhoneCall incomingPhoneCall=new IncomingPhoneCall("310-332-0908");
OutgoingPhoneCall outgoingPhoneCall=new OutgoingPhoneCall("310-000-0102",20);
incomingPhoneCall.info();
outgoingPhoneCall.info();
}
}
I have been trying to make three different extended classes from a superclass. The problem is that only the first extended class works fine while the other two gets an error when I name them, and its says they should be in their own file. I have looked around to see if someone had a similar problem, but nothing exactly like this. The extended class that is working is the first one called "Smycken".
here is the code:
abstract public class Vardesaker
{
private String name;
double value;
public Vardesaker(String name, double value)
{
this.name = name;
this.value = value;
}
public String getName()
{
return name;
}
abstract public double getValue();
}
class Smycken extends Vardesaker
{
private int adelstenar;
private String material;
public Smycken(String name, double value, int adelstenar, String material)
{
super(name, 0);
this.adelstenar = adelstenar;
this.material = material;
}
public int getadelstenar()
{
return adelstenar;
}
public String getMaterial()
{
return material;
}
public double getValue()
{
if(material.equalsIgnoreCase("guld"))
{
double sum = 2000 + (500*adelstenar);
value = sum*1.25;
}
else
{
double sum = 700 + (500*adelstenar);
value = sum*1.25;
}
return value;
}
}
public class Aktier extends Vardesaker
{
private double kurs;
private int amount;
public Aktier (String name, double value, int amount, double kurs)
{
super(name, 0);
this.kurs = kurs;
this.amount = amount;
}
public double getKurs()
{
return kurs;
}
public int getAmount()
{
return amount;
}
public double getValue()
{
double sum = (int) (amount*kurs);
value = sum*1.25;
return value;
}
}
public class Apparater extends Vardesaker
{
private double price;
private int slitage;
public Apparater(String name, double value, double price, int slitage)
{
super(name, 0);
this.price = price;
this.slitage = slitage;
}
public double getPrice()
{
return price;
}
public int getSlitage()
{
return slitage;
}
public double getValue()
{
double sum = price*(slitage/10);
value = sum*1.25;
return value;
}
}
It is not because of extends, it is because of public keyword for the other classes. If you create multiple class with public keyword then they should be in their own compilation unit.
Check this answer for why each public class should have separate file.
There is a simple rule - 1 public type (class,interface,enum) = 1 java file
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
public class TimsOrder {
private int size;
private String name;
private static TimsProduct[] items;
private TimsOrder(String name, int size) {
this.name = name;
this.size = size;
}
#Override
public double getRetailPrice(){
return price;
}
private static void orderItem(TimsProduct item) {
Donut chocolate = Donut.create();
item = chocolate;
}
public static TimsOrder create() {
items = new TimsProduct[size];
for (int i = 0; i < items.length; i++) {
orderItem(items[i]);
}
TimsOrder order = new TimsOrder("OrderName", 1); //Where 1 is the # of items
}
public double getAmountDue() {
double total = 0;
System.out.println("Testpoint");
for (int i = 0; i < items.length; i++) {
total = total + (((TimsProduct) items[i]).getRetailPrice()); //Line with issue
}
return total;
}
}
public abstract class TimsProduct extends Commodity {
private String name;
private double cost;
private double price;
#Override
public double getRetailPrice(){
System.out.println("Testpoint2");
return price;
}
}
public class Donut extends TimsProduct {
private String description;
private int calorieCount;
private Donut(String name, String description, double cost, double price, int calorieCount) {
super(name, cost, price);
this.description = description;
this.calorieCount = calorieCount;
}
public static Donut create() {
Donut chocolate = new Donut("Chocolate", "Glazed", 0.30, 0.99, 500);
return chocolate;
}
}
Test Code:
TimsOrder t = TimsOrder.create();
System.out.println(t);
System.out.printf("Total Price: $%.2f\n", t.getAmountDue());
I realize that t.items has 0 values which is the problem here. What I do not know is why these values are not there.
If anyone wants to see the files:
Commodity.java
https://pastebin.com/raw/9sWbDWV8
TimsProduct.java extends commodity
https://pastebin.com/raw/jzgfkd0P
TimsOrder.java
https://pastebin.com/raw/vc0VtDq6
Donut.java extends TimsProduct
https://pastebin.com/raw/w7iEQG1H
This is an initial response based on what little the OP provided.
You are not overriding your getters in your deriving classes (and the basic implementation in the base can not compile) :
public abstract class Commodity {
public double getProductionCost() {
// no return value!
}
public double getRetailPrice() {
// no return value!
}
}
public abstract class TimsProduct extends Commodity{
private String name;
private double cost;
private double price;
public TimsProduct(String name, double cost, double price){
this.name = name;
this.cost = cost;
this.price = price;
}
public String getName(){
return name;
}
// no #Override!
public double getProductionCost(){
return cost;
}
// no #Override!
public double getRetailPrice(){
return price;
}
public String toString(){
return ("Name is: " + name + "cost is: " + cost + "price is: " + price );
}
}
I got 5 classes, and 4 of them are working as intended.
I'm an absolut beginer with java and I hope someone can provide me with help.
its a little application to make orders and bundle orders.
my problem is the class bundleItem.
I expect to get the value of all items with a discount added to them, but I allways get negative return values.
Here are my classes
class Order
package testateins;
import java.util.ArrayList;
public class Order {
private ArrayList<Item> allOrders;
public Order(){
this.allOrders = new ArrayList<Item>();
}
public void addItem(Item item) {
this.allOrders.add(item);
}
public void removeItem(Item item) {
this.allOrders.remove(item);
}
public double getTotalprice(){
double totalprice = 0;
for(Item element : allOrders) {
totalprice += element.getPrice();
}
return totalprice;
}
public void printItems() {
for(Item element : allOrders) {
element.print();
}
}
}
Class Item
package testateins;
public abstract class Item{
String description;
double price;
public Item(String description) {
this.description = description;
}
public abstract double getPrice();
public void setItem() {
}
public void print() {
System.out.println("Beschreibung " + description);
}
}
class ProductItem
package testateins;
public class ProductItem extends Item{
public double pricePerUnit;
public double amount;
public double price;
public ProductItem(String description, int pricePerUnit, int amount) {
super(description);
this.pricePerUnit = pricePerUnit;
this.amount = amount;
this.price = amount * pricePerUnit;
}
public void print() {
System.out.println("Beschreibung " + description);
System.out.println("menge " + amount);
System.out.println("Preis pro Einheit " + pricePerUnit);
System.out.println("Gesammtpreis " + price);
}
public double getPrice() {
return this.price;
}
}
class ServiceItem
package testateins;
public class ServiceItem extends Item{
public double price;
public ServiceItem(String description, Integer packageprice) {
super(description);
this.price = packageprice;
}
public void print() {
System.out.println("Beschreibung " + description);
System.out.println("Servicepreis " + price);
}
public double getPrice() {
return this.price;
}
}
class BundleItem(my problemclass)
package testateins;
import java.util.ArrayList;
public class BundleItem extends Item {
private double rabatt;
private ArrayList<Item> allOrders;
public BundleItem(String description, double rabatt) {
super(description);
this.allOrders = new ArrayList<Item>();
setrabatt(rabatt);
}
public void setrabatt(double rabatt) {
this.rabatt = rabatt;
}
public double getrabatt() {
return this.rabatt;
}
public void addItem(Item item) {
if(item instanceof ProductItem || item instanceof ServiceItem) {
this.allOrders.add(item);
}
}
#Override
public double getPrice() {
double result = 0;
double sum = 0;
double discount = (rabatt/100);
for(Item element : allOrders) {
result+=element.getPrice();
}
return sum - (result * discount);
}
public void print() {
super.print();
System.out.println(rabatt);
for(Item element: allOrders) {
element.print();
}
}
}
and my testing class which returns negative discounts, the important part is after "part 2"
package testateins;
public class Testing {
public static void main(String[] args) {
ServiceItem wischen = new ServiceItem("wischen", 20);
System.out.println(wischen.description);
System.out.println(wischen.price);
System.out.println("___________________________");
ProductItem kuchen = new ProductItem("kuchen", 3, 15);
System.out.println(kuchen.description);
System.out.println(kuchen.amount);
System.out.println(kuchen.pricePerUnit);
System.out.println(kuchen.price);
System.out.println("___________________________");
Order simpleOrder = new Order();
simpleOrder.addItem(wischen);
simpleOrder.addItem(kuchen);
simpleOrder.addItem(kuchen);
simpleOrder.printItems();
System.out.println("___________________________");
System.out.println("totalpreis:");
System.out.println(simpleOrder.getTotalprice());
//part 2
System.out.println("--------uebung 2------------.-");
Order BundleOrder = new Order();
ProductItem heft = new ProductItem("Heft", 1, 10);
ProductItem stift = new ProductItem("Stift", 1, 10);
BundleItem Discounteins = new BundleItem("Discounteins(50)", 50);
BundleItem Discountzwei = new BundleItem("Discountzwei(50)", 50);
BundleItem Discountdrei = new BundleItem("Discounteins(10)", 10);
Discounteins.addItem(heft);
Discounteins.addItem(stift);
Discountzwei.addItem(Discounteins);
Discountzwei.addItem(wischen);
Discountzwei.addItem(wischen);
Discountdrei.addItem(wischen);
Discountdrei.addItem(wischen);
BundleOrder.addItem(Discountzwei);
BundleOrder.addItem(Discounteins);
BundleOrder.printItems();
System.out.println(BundleOrder.getTotalprice());
System.out.println("---------------------------.-");
Discounteins.setrabatt(50);
System.out.println(Discounteins.getrabatt());
BundleOrder.printItems();
System.out.println("---------------------------.---");
System.out.println(BundleOrder.getTotalprice());
System.out.println("---------------------------.----");
}
}
Your sum is always zero, that's why you always get negative values.
I suppose you intended to do something like this:
#Override
public double getPrice() {
double sum = 0;
double discount = (rabatt / 100);
for(Item element : allOrders) {
sum += element.getPrice();
}
return sum - (sum * discount);
}
Try replacing
return sum - (result * discount);
with
return result-(result * discount);
and remove the sum variable that is not used. Now
(result * discount)
will give you the total discount. And Subtracting it from result will give you the actual amount after discounts
Alright so I need help here I created a cash register java file and a retailItem file in my cash register file it cant read a section i put in retailItem because its private. So my question is how do i access that private variable that i wrote in my retailItem class into the cash register file
So here's my code for retailItem
import java.text.DecimalFormat;
public class RetailItem
{
private String description;
private double price;
private double unitsOnHand;
private CostData cost;
public void setDescription(String userDescription)
{
description = userDescription;
}
public void setPrice(double p)
{
price = p;
}
public void setUnitsOnHand(double userUnitsOnHand)
{
unitsOnHand = userUnitsOnHand;
}
public String getDescription()
{
return description;
}
public double getPrice()
{
return price;
}
public double getUnitsOnHand()
{
return unitsOnHand;
}
public RetailItem(RetailItem object2)
{
description = object2.description;
price = object2.price;
unitsOnHand = object2.unitsOnHand;
cost = new CostData(object2.cost.wholeSale,object2.cost.Retail);
}
public RetailItem( String descriptionGet,double pri, double
unitsOnHandGet,double wholeSale,double retail)
{
description = descriptionGet;
price = pri;
unitsOnHand = unitsOnHandGet;
cost = new CostData(wholeSale,retail);
}
public String toString()
{
String str;
DecimalFormat dollar = new DecimalFormat("#,##0.00");
str = "Description: " + description +
"Item Price: " + price +
"\nItem Number: " + unitsOnHand +
"\nWhole Cost: $" + dollar.format(cost.wholeSale) +
"\nRetail Price: $" + dollar.format(cost.Retail);
return str;
}
public class CostData
{
public double wholeSale;
public double Retail;
public CostData(double whole,double re)
{
wholeSale = whole;
Retail = re;
}
public void setRetail(double re)
{
Retail = re;
}
public void setWholeSale(double whole)
{
wholeSale = whole;
}
public double getRetail()
{
return Retail;
}
public double getWholeSale()
{
return wholeSale;
}
}
}
Here is the Cash Register class:
public class CashRegister
{
private RetailItem retail;
private int quantityItem;
private final double SALES_TAX = 0.06;
private int subTotal;
public CashRegister()
{
quantityItem = 0;
subTotal = 0;
}
public CashRegister(RetailItem retailObject,int quantity)
{
retail = new RetailItem(retailObject);
quantityItem = quantity;
}
public RetailItem getRetailItem()
{
return new RetailItem(retail);
}
public double getSubTotal()
{
return quantityItem * retail.cost.getRetail();//Here is where the problem is
}
public double getTax()
{
return SALES_TAX;
}
public double getTotal()
{
return subTotal + SALES_TAX;
}
}
You have a number of options:
If they are in the same package, you can set the access of cost to
default (remove the word private from in front of it.)
Change cost from private to public.
Create a default access (neither private nor public) getCost() method to return cost.
Create a public getCost() method to return cost.
The first option gives read/write access to any class in the same package as RetailItem.
The second option gives read/write access to all classes.
The fourth option gives read-only access to all classes.
protected access could also be an option. Its like package access, but also gives read/write access to subclasses.
See also: In Java, difference between default, public, protected, and private