I need help with creating subclasses, the subclass doesn't override the parents method. I use a Registry object to collect all the items.
public class Registry {
private final Item[] items = new Item[100];
private int pos =0;
public Item[] getItems(){
return items;
}
public void addItem(Item item) {
items[pos] = item;
pos ++;
}
public void addItem(Item item, int nrTimes) {
for(int i = 0; i<nrTimes;i++){
items[pos] = item;
pos++;
}
}
public double totPrice(){
double total = 0.0;
for(int i = 0; i < items.length; i ++){
if(items[i] == null) break;
total += items[i].getNr(this);
}
return total;
}
public String toString() {
String result = "";
for(int i= 0; i < items.length;i++){
if (items[i] == null) break;
result = result + items[i].getName()+" $" + items[i].getNr(this) + " Sold by " + items[i].getName2()+ "\n";
}
return result;
}
}
public class Item {
private final String name;
private final String name2;
private final double nr1;
public Item (String name, String name2, double nr1) {
this.name = name; this.name2 = name2; this.nr1 = nr1;
}
public Item (Item obj) {
name = obj.name; name2 = obj.name2; nr1 = obj.nr1;
}
public double getNr(Registry reg) { return nr1;}
public final String getName() { return name;}
public final String getName2() { return name2;}
}
The subclass
public class ReducedItem extends Item {
private final Item obj = this;
private final double reduce; // in %
public ReducedItem (Item obj, double reduce) {
super(obj);
this.reduce = reduce/100;
}
public double getNr(Registry reg){
double nr;
nr = super.getNr(reg) - (super.getNr(reg)*reduce);
return nr;
}
}
here is the main where I create the objects
import java.util.*;
public class Main {
static Scanner scan = new Scanner(System.in);
public static void registerItems(Registry reg){
String name,seller;
double price;
int nrprod,discount;
scan.nextLine();
System.out.print("Product name: ");
name = scan.nextLine();
System.out.print("Seller: ");
seller = scan.nextLine();
System.out.print("Price: ");
price = scan.nextDouble();
System.out.print("How many: ");
nrprod = scan.nextInt();
System.out.print("Discount (enter 0 if no discount applies): ");
discount = scan.nextInt();
Item item = new Item(name,seller,price);
if(discount >0) {
ReducedItem redItem = new ReducedItem(item, discount);
if(nrprod == 1){
reg.addItem(redItem);
}else{
reg.addItem(redItem, nrprod);
}
}else{
if(nrprod == 1){
reg.addItem(item);
}else{
reg.addItem(item, nrprod);
}
}
}
public static void main(String[] args) {
System.out.println("Welcome");
System.out.println("What's your name?");
String customer = scan.nextLine();
System.out.println("Hi "+customer+". Please choose one of the following options:");
System.out.print("buy product (1) or checkout (0)");
Registry reg = new Registry();
int input = scan.nextInt();
while(input == 1){
registerItems(reg);
System.out.println("buy product (1) or checkout (0)");
input = scan.nextInt();
}
System.out.println(reg.toString());
System.out.println("Your total is " + reg.totPrice());
}
}
input in the registerItems
Product name = Car
Seller = Jack
Price = 10000
how many =1
discount = 20
checkout = 0
output
Car $8000 Sold by jack
Your total is 8000.0
The car is now discounted by 20%