Made correction to previous errors, this error now comes up.
C:\Documents and Settings\AdminUser\My Documents\InventoryPart3.java:93: invalid method declaration; return type required
public Television(int ItemNumber, String ProductName, int UnitsStock, double UnitPrice, String mfgName) {
^
1 error
Tool completed with exit code 1
class Television { //class name and attributes
private String ItemNumber; //item # of product
private String ProductName; //product name
private double UnitsStock; //# of units in stock
private double UnitPrice; //Price per unit
private String SerialNumber; //serial number of product
private double InventoryValue; //The dollar value of the inventory in stock
//constructor
public Television (String item, String product, double units, double price, String serial) {
ItemNumber = item;
ProductName = product;
UnitsStock = units;
UnitPrice = price;
SerialNumber = serial;
} //end constructor
///getter and setter methods for Television
public void setItemNumber (String item) { //setter for item number
this.ItemNumber = item;
} //end setter item number
public String getItemNumber() { //getter for item number
return ItemNumber;
} //end getter item number
public void setProductName (String product) { //setter for product name
this.ProductName = product;
} //end setter product name
public String getProductName() { //getter for product name
return ProductName;
} //end getter product name
public void setUnitsStock (double units) { //setter for units in stock
this.UnitsStock = units;
} //end setter units in stock
public double getUnitsStock() { //getter for units in stock
return UnitsStock;
} //end getter units in stock
public void setUnitPrice (double price) { //setter for unit price
this.UnitPrice = price;
} //end setter unit price
public double getUnitPrice() { //getter for unit price
return UnitPrice;
} //end getter for unit price
public void setSerialNumber (String serial) { //setter for serial number
this.SerialNumber = serial;
}//end setter for serial number
public String getSerialNumber() { //getter for serial number
return SerialNumber;
}//end getter for serial number
//calculate individual product inventory value
public double getInventoryValue(){
return UnitsStock * UnitPrice;
}//end calculate individual product inventory value
//calculate total inventory value
public double calculateInventory(){
return UnitPrice * UnitsStock;
}//end calculate total inventory value
///end getter and setter methods for Laptop
} //end class Television
class Manufacturer extends Television {
private String manufacturerName;
//constructor
public Television(int ItemNumber, String ProductName, int UnitsStock, double UnitPrice, String mfgName) {
super(ItemNumber, ProductName, UnitsStock, UnitPrice, ManufacturerName);
this.manufacturerName = mfgName;
}//end constructor
//getter and setter for class Manufacturer
public void setmanufacturerName(String mfgName) {
this.manufacturerName = mfgName;
}
public String getmanufacturerName() {
return manufacturerName;
}
//end getter and setter for class Manufacturer
//calculate total inventory value
public double calculateInventory(){
return ((UnitPrice * UnitsStock));
}//end calculate total inventory value
//calculate restocking fee method
public double getcalculateRestockFee() {
return getInventoryValue();
}//end calculate restocking fee method
}//end class Manufacturer
So there are two errors. The first one occurs on line 93 of the file InventoryPart3.java. On that line you will find the code:
super(ItemNumber, ProductName, UnitsStock, UnitPrice);
Which is actually a call to the constructor of the television class. However, the constructor of the television class accepts 5 parameters: String, String, double, double, String but you are passing only 4.
The second error occurs on line 120 of the same file. In this case you seem to be calling a method but you left out the parenthesis. That is, the code should be:
getInventoryValue()
The constructor of television which you are trying to call from the manufacturer class using super does not exist in television class. Please see the parameters in the super() call and the actual parameters in the Television constructor. The getInventoryValue function is not there in the Television class.Hence it is throwing an error for the super.getInventory call
The first error is related to your Television constructor. Your Television constructor takes 5 arguments of type String, String, double, double, String. In your Manufacturer class, you're calling the superclass constructor with an int, string, int, and double. In a subclass, your super() constructor call must match one of the constructors in the base class (Television).
For the second error, your call to getInventoryValue has a syntax error. To call a method in Java, you have to put parentheses after the name of the method:
return super.getInventoryValue() * 0.10;
Note that in this case, the super. part is optional. The getInventoryMethod() exists in the super class, and is not private, so you can call it from the subclass like this:
return getInventoryValue() * 0.10;
Related
I'm a new student in computer science and i have to create an inventory program that read informations about products from a txt file in this format: Department;unit of measure;quantity;price;unique code;name; (example: E;U;20;1,50;87678350;Lamp) .Subsequently i have to :
-calculate the total value of the stock
-selling of a product
-insertion of a product
-searching of a product by unique code.
If there are lines with the same unique code, the program will report an error.
I managed to read the lines in the txt file but i dont have any idea on how to calculate the total value of the stock from it.
public class Emporium{
public static void main (String[]args) throws IOException {
Scanner input = new Scanner(new File("EMPORIUM.txt"));
input.useDelimiter(";|\n");
Product[] products = new Product[0];
while(input.hasNext()) {
String department = input.next();
String unit=input.next();
int quantity=input.nextInt();
double price=input.nextDouble();
long code=input.nextLong();
String name=input.next();
Product newProduct = new Product(department,unit,quantity,price,code,name);
products= addProducts(products,newProducts);
}
for (Product product: products) {
System.out.println(product);
}}private static Product[] addProduct(Product[] products, Product productToAdd) {
Product[] newProducts =new Product[products.length+1];
System.arraycopy(products,0,newProducts,0, products.length);
newProducts[newProducts.length-1]= productToAdd;
return newProducts;
}
public static class Product {
protected String department;
protected String unit;
protected int quantity;
protected double price;
protected long code;
protected String name;
private static NumberFormat formatter = new DecimalFormat("#0.00");
public Product(String dep,String uom,int qnt,double prz,long cod,String nm) {
department=dep;
unit=uom;
quantity=qnt;
price=prz;
code=cod;
name=nm;
}
#Override
public String toString() {
return String.format(department+";"+unity+";"+quantity+";"+formatter.format(price)+";"+code+";"+name);
}
}
}
My question is: How can i read the value of the product in the file and sum it with the prices of the other products ? This mean that i need to do the same with the unique code to find a specific product.
Thank you very much in advance for your assistance.
The correct method would be to have Getters and Setters in your Product Class.
As you can see in your code, you just pass in your variables and initiate them in your constructor, but initializing them using getters and setters is better as it is a good programming practice and adds more functionality to your code.
Example
public static class Product {
protected String department;
protected String unit;
protected int quantity;
protected double price;
protected long code;
protected String name;
private static NumberFormat formatter = new DecimalFormat("#0.00");
public Product(String dep,String uom,int qnt,double prz,long cod,String nm) {
setDep(dep);
setUom(uom);
setQnt(qnt);
setPrz(prz);
setCod(cod);
setNm(nm);
}
public void setPrz(double prz){
this.price = price;
}
//Other setters
public double getPrz(){
return price;
}
//Other getters
#Override
public String toString() {
return String.format(department+";"+unity+";"+quantity+";"+formatter.format(price)+";"+code+";"+name);
}
}
With getters and setters in your Products Class, you can:
Create a method that calculates the sum of all Products in your ArrayList
Search for a particular product in your array using your unique identifier
Sort your Arraylist by a variable, be it department, price etc.
You've already read your products into an array, you need to loop through the array and add together price * quantity for each one.
For example...
double totalValue = Arrays.stream(products).mapToDouble(p -> p.quantity * p.price).sum();
This will iterate over each product and map each product to a double (which is the quantity times the price for that product) it then sums all of those results.
Am trying to implement a system for a car dealer, but when I try to instantiate my Car class in a derived class, I get the error message
Multiple markers at this line
- The constructor Car(String, int, String, String, double, double) is
undefined
Here's the parent class Car:
package Number3;
public class Car {
private String plateNum;
private int year;
private String make;
private String model;
protected double costPrice;
protected double sellingPrice;
public Car()
{
plateNum = "";
year = 1990;
make = "";
model = "";
costPrice = 0.0;
sellingPrice = 0.0;
}
public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
{
this.plateNum = plateNum;
this.year = year;
this.make = make;
this.model = model;
this.costPrice = costPrice;
this.sellingPrice = sellingPrice;
}
public double getCostPrice()
{
return costPrice;
}
public double computeSellPrice()
{
sellingPrice = costPrice;
return sellingPrice;
}
public void displayCarDetails()
{
System.out.println("Plate number: "+plateNum);
System.out.println("Year: "+year);
System.out.println("Make: "+make);
System.out.println("Cost price: "+costPrice);
System.out.println("Selling price: "+sellingPrice);
}
}
and the subclass newCar:
package Number3;
public class newCar extends Car{
private double tax;
public newCar(String plateNum,int year, String make, double costPrice, double sellingPrice, double tax)
{
super(plateNum,year,make,costPrice,sellingPrice); //where the error is found
this.tax = (25/100);
}
public double computeSellPrice()
{
sellingPrice = costPrice + (costPrice * tax);
return sellingPrice;
}
public void displayCarDetails()
{
super.displayCarDetails();
}
}
Any help would be appreciated.
The signature of your Car constructor does not match the signature in the derived class.
In your Car class, this is the constructor:
public Car(String plateNum,int year,
String make,String model,double costPrice,double sellingPrice) {
...
}
It is String, int, String, String, double, double)
While in the derived class:
you have:
super(plateNum,year,make,costPrice,sellingPrice)
Which is int, int, String, double, double
Change the arguments in call to Super in your newCar class to match the constructor of your Car class. That is, in your newCar class, the line
super(plateNum,year,make,costPrice,sellingPrice)
should be :
super(plateNum, year,
make, model, costPrice, sellingPrice)
The Car class does not have a constructor that takes 5 parameters.
It is defined as
public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
{
...
}
and you're trying to call it without passing the model parameter.
super(plateNum,year,make,costPrice,sellingPrice); //where the error is found
Your super/parent class Car has a no-argument constructor public Car() { and the following 6-parameter constructor which is being called from the sub/child class constructors using the keyword super.
public Car(String plateNum,int year,String make,String model,double costPrice,double sellingPrice)
Notice, that it expects String model as its fourth parameter but your public newCar() constructor is passing it only five parameters. The parameter model is missing.
public newCar(String plateNum,int year, String make, double costPrice, double sellingPrice, double tax)
{
super(plateNum,year,make,costPrice,sellingPrice); // model MISSING!
So, to fix it, either modify the constructor to accept model as well (just like in your usedCar() constructor) or pass null to the super class constructor as
super(plateNum,year,make,null,costPrice,sellingPrice); // model = null
Ok guys I've got an assignment that has a abstract class 'Order' and three other classes that extend it 'OverseasOrder', 'RegularOrder', and 'NonProfitOrder'
Here is my abstract class:
public abstract class Order {
protected String location;
protected double price;
public Order(double price, String location){
}
public abstract double calculateBill();
public String getLocation() {
return location;
}
public double getPrice() {
return price;
}
public abstract String printOrder(String format);
}
and here is my 'NonProfitOrder':
public class NonProfitOrder extends Order {
public NonProfitOrder(double price, String location) {
super(price, location);
}
public double calculateBill() {
double bill;
bill = price;
return bill;
}
public String printOrder(String format){
String Long = "Non-Profit Order" + "\nLocation: " + getLocation() + "\nTotal Price: " + getPrice();
return Long;
}
}
I'm taking it step by step to make sure everything is working so this is the only classes I have written so far. The problem I having is when I test something like
public class OrderTester {
public static void main(String[] args) {
Order o;
o = new NonProfitOrder(2000.0, "NY");
System.out.println(o.printOrder("Long"));
}
}
Non-Profit Order
Location: Null
Total Price: 0.0
I'm not sure if I'm calling the 'price' and location' wrong in my string, or if I'm doing something wrong in trying to implement those methods from the abstract Order class
Thanks for any and all help!
Your super constructor doesn't set the location
public Order(double price, String location){
}
so this constructor
public NonProfitOrder(double price, String location) {
super(price, location); // calls super class' constructor
}
doesn't actually set the price and location.
Change the constructor for Order to
public Order(double price, String location){
this.double = double;
this.location = location;
}
Un-initialized fields are given a default value. For reference types, that value is null. For numerical types, the value is 0. For boolean types, the value is false. That's what you were seeing.
You have just missing to assign your attributes in the abstract class.
public Order(double price, String location){
this.price = price;
this.location = location;
}
You have not initialized instance variables location and price.
So Java provides default values for those.
Here location is type String object so default value is null
The price is double type so default value is 0.0.
Those values are printing as output in your case.
So try to initialize them in Order class constructor
public Order(double price, String location){
this.double = double;
this.location = location;
}
I have a method to create a list of objects of class
public List<Product> initProducts(){
List<Product> product = new ArrayList<Product>();
Product prod = new Product(product.getId(),product.getItemName(),product.getPrice(),product.getCount());
product.add(prod);
return product;
}
My Product class is:
public class Product {
int ItemCode;
String ItemName;
double UnitPrice;
int Count;
/**
* Initialise the fields of the item.
* #param Name The name of this member of product.
* #param id The number of this member of product.
* #param Price The price of this member of product.
*/
public Product(int id, String Name, double Price, int c)
{
ItemCode=id;
ItemName=Name;
UnitPrice=Price;
Count = c;
}
public int getId()
{
return this.ItemCode;
}
public String getItemName()
{
return this.ItemName;
}
public double getPrice()
{
return this.UnitPrice;
}
public int getCount()
{
return this.Count;
}
/**
* Print details about this members of product class to the text terminal.
*/
public void print()
{
System.out.println("ID: " + ItemCode);
System.out.println("Name: " + ItemName);
System.out.println("Staff Number: " +UnitPrice);
System.out.println("Office: " + Count);
}
}
I am getting an error that the method getId() is undefined for the type List<Product>, Similarly for other methods. Please help me out with this error.
Is my statement correct??
Product prod = new Product(product.getId(),product.getItemName(), product.getPrice(),
product.getCount());
product.add(prod);
product is a reference of List
List<Product> product = new ArrayList<Product>();
which doesn't have that method
product is reference to List object.
and List/ArrayList has no methosd named getId().
You have written getId() method for Prodct class , so you can call this method using ref to Product class object.
If you want to get any product object form list use get(int index) method of ArrayList.
eg.
Product prod = product.get(0);
String id= prod.getId();
I believe, the reason you are facing this issue is more due not following the code conventions, that any other.
Whenever you make a collection of any objects, the convention is to use plurals for reference names of the collection. And singular reference name of the Object itself.
You can find more details here.
Below is the re-written code with the code conventions being followed:
Method to create a list of objects of class Product:
public List<Product> initProducts(){
List<Product> products = new ArrayList<Product>();
Product product = new Product(products.getId(), products.getItemName(), products.getPrice(), products.getCount());
products.add(prod);
}
Product Class:
class Product {
int itemCode;
String itemName;
double unitPrice;
int count;
public Product(int itemCode, String itemName, double unitPrice, int count)
{
this.itemCode = itemCode;
this.itemName = itemName;
this.unitPrice = unitPrice;
this.count = count;
}
public int getId()
{
return this.itemCode;
}
public String getItemName()
{
return this.itemName;
}
public double getPrice()
{
return this.unitPrice;
}
public int getCount()
{
return this.count;
}
}
Now, it is easy to see, that the products Object (which is of the type List) will not have any methods name getId() or getCount(). Infact , these are methods of the Object contained in the List.
Following conventions will help you avoid, such hassles in futures.
Is my statement correct??
Product prod = new Product(product.getId(),product.getItemName(), product.getPrice(),
product.getCount());
product.add(prod);
NO this is incorrect. product is not an instance of class Product,rather it is an instance of List. List does not have any method called getId.
If you want to retrieve the elements from the list and use it to create another instance of you can do something like:
Product exisProd = product.get(0);
Product prod = new Product(exisProd .getId(),exisProd .getItemName(), exisProd .getPrice(),
exisProd .getCount());
But make sure that you have elements in the list, otherwise u may run into exception.
product.add(prod);
I want to be able to give a specific value to discount depending on certain requirements like the following age: > 25 and profession = teacher / professor get 10% discount, age < 25 and gradepoint > 7 get 25% discount
this is my code so far I am using double OO paradigm:
public class customer {
//attribute definitions
private String name;
private String address;
private String profession;
private Integer age;
private Integer gradepoint;
private double discount;
//constructor
public customer(String newName, String newAddress, String newProfession, Integer newAge, Integer newGradepoint, double newDiscount)
{
setName(newName);
setAddress(newAddress);
setProfession(newProfession);
setAge(newAge);
setGradepoint(newGradepoint);
setDiscount (newDiscount);
}
//getters
public String getName()
{ return name;}
public String getAddress()
{ return address;}
public String getProfession()
{ return profession;}
public Integer getAge()
{ return age;}
public Integer getGradepoint()
{ return gradepoint;}
public double getDiscount()
{ return discount;}
//setters
public void setName (String newName)
{ name = newName;}
public void setAddress (String newAddress)
{ address = newAddress;}
public void setProfession (String newProfession)
{ profession = newProfession;}
public void setAge (Integer newAge)
{ age = newAge;}
public void setGradepoint (Integer newGradepoint)
{ gradepoint = newGradepoint;}
public void setDiscount (double newDiscount)
{ discount = newDiscount;}
//methods
}
Would I need to create a sub class called discount or each type of discount? or I can write a method directly into this customer class to control the discount?
write a method directly into this customer class to control the discount?
This. Make it a calculated field. Kill setDiscount function, kill discount variable, and make the getDiscount function into something like:
public double getDiscount() {
if (...) return ...;
if (....) return ...;
...
}
...unless you want to have this as the default discount, and still allow modification, in which case keep discount as a property, and move this whole logic into the constructor, having conditional setDiscount() calls.
Your getDiscount function would ideally do the calculation and return the appropriate discount for the current object. For example:
public double getDiscount()
{
if (getAge() < 25 && getGradepoint() > 7)
{
return .25;
}
else if // other logic...
}
Although not the simplest solution, I would abstract the discount calculation to a separate interface and class as well as having an override discount value in the customer object.
E.g.
public interface DiscountManager<T>
{
public double getDiscount(T discountObject);
}
public abstract class AbstractCustomerDiscountManager extends DiscountManager<Customer>
{
public double getDiscount(Customer customer)
{
if (customer.hasCustomDiscount()) { return customer.getDiscount(); }
else { return calculateDiscount(customer); }
}
public abstract double calculateDiscount(Customer customer);
}
public class DefaultDiscountManager extends AbstractCustomerDiscountManager
{
public double calculateDiscount(Customer customer)
{
double discount = 0;
if ((customer.getAge() != null) && (customer.getAge() < 25)) { discount += 25; }
...
return discount;
}
}
Probably over time different rules evolve. At the spot where the discounting takes place, in the order, the discount and and a reference to the rule applied should be stored together.
This kind of business logic could have its own class. A generic solution would even be to store the rule as scriptable code (BeanShell = Java, or JavaScript) and use java's scripting API. So that this kind of business logic resides more with the business managers, and the rules can be presented and edited.