JAVA Storing 3 or more classes in array - java

I'm a little stuck and would definitely appreciate your help. As the title says I want to store different classes on an array, which is in another class
This would be my code
The Customer class:
public class Customer {
public List<Product> products = new ArrayList<Product>();
public void addProduct(Product p){
products.add(p);
}
}
The Product class:
public abstract class Product{ }
The Ham class:
public class Jamon extends Product{
protected String marca;
protected int precio;
public Jamon(String marca,Double precio){
this.marca=marca;
this.precio=precio;
}
public void setMarca(String marca){
this.marca=marca;
}
public String getMarca(){
return marca;
}
public void setPrecio(int precio){
this.precio=precio;
}
public int getPrecio(){
return precio;
}
*/
public void comprar(){
System.out.println("Se ha comprado el jamon");
}
What I want to know is How do i store the Jamon class in the array on the Customer class, i want this because there will be multiple classes like Jamon that extend from Product. Also, if all this classes (Pepino, Jelly, etc) have precios, how can I add all the precios?
Thank you, if you have any doubt about my question please ask me (english is not my first language) Nice Day (:

Here's a small implementation of what you appear to be trying to achieve
public abstract class Product {
protected double precio;
public void setPrecio(double precio){
this.precio=precio;
}
public double getPrecio(){
return precio;
}
}
Here's another one
public class Jamon extends Product{
protected String marca;
public Jamon(String marca,Double precio){
this.marca=marca;
this.precio=precio;
}
public void setMarca(String marca){
this.marca=marca;
}
public String getMarca(){
return marca;
}
public void comprar(){
System.out.println("Se ha comprado el jamon");
}
}
Here's also this one
public class Pepino extends Product{
protected String marca;
public Pepino(String marca,Double precio){
this.marca=marca;
this.precio=precio;
}
public void setMarca(String marca){
this.marca=marca;
}
public String getMarca(){
return marca;
}
public void comprar(){
System.out.println("Se ha comprado el pepino");
}
}
And finally
import java.util.List;
import java.util.ArrayList;
public class Customer {
public List<Product> products = new ArrayList<Product>();
public void addProduct(Product p){
products.add(p);
}
public static void main(String[] args) {
Customer customer = new Customer();
Jamon jamon = new Jamon("Jabugo", 25.0);
Pepino pepino = new Pepino("Extra picante", 8.50);
Jamon negro = new Jamon("Pata Negra", 32.99);
customer.addProduct(jamon);
customer.addProduct(pepino);
customer.addProduct(negro);
for (Product product : customer.products) {
System.out.println(product.getClass());
}
}
}
Okay, so to be clear. Both Jamon and Pepino extend from Product, which means:
-Any variable (such as precio) or method (setPrecio or getPrecio) is available to them. Since its defined in the abstract class. One exception to this are the ones with private, those won't be accesible in the extending classes.
-Since customer adds a bunch of elements as Product there is no way to access the methods defined in Jamon or Pepino unless the item in the list is casted.
-It is not very common practice to declare an inner field a public variable (I'm referring to products in Customer, make a getter/setter).
-Also in your small example you seem to mix the ideas of double and int both represent numbers, but only the first has decimal point. If you are using decimal values, as it happens with precio, declare the field a double and work always with a double.
New Customer class (only storing prices, as oposed to Product, in the ArrayList)
import java.util.List;
import java.util.ArrayList;
public class Customer {
public List<Double> products = new ArrayList<Double>();
public void addProduct(Product p){
products.add(p.getPrecio());
}
public static void main(String[] args) {
Customer customer = new Customer();
Jamon jamon = new Jamon("Jabugo", 25.0);
Pepino pepino = new Pepino("Extra picante", 8.50);
Jamon negro = new Jamon("Pata Negra", 32.99);
customer.addProduct(jamon);
customer.addProduct(pepino);
customer.addProduct(negro);
for (Double productPrice : customer.products) {
System.out.println(productPrice);
}
}
}

As others have already commented, you can add an instance of any subclass of Product to an ArrayList<Product>. That you have done already.
If you want all products to have a precio, then you can define it on the abstract class Product instead of in Jamon and other subclasses. Move the declaration of the variable precio from Jamon to Product. Also move the methods getPrecio and setPrecio to Product. Create a constructor in Product to accept a precio. In constructors of subclasses such as Jamon, call super(precio) to initialize precio.
Then, you will be able to loop over all Products in your ArrayList and call the getPrecio() method to add up all precios.

Related

Java factory but objects have slightly differrent attributes

I want to program a factory that creates two types of People which are "Employee" and "Entrepreneur". They both share the same basic "Person" attributes but they also implements their unique ones.
The problem is that if i want to return a method or an attribute that is not declared in the "Person" abstract class the program doesn't find it (since obviously I'm generating an object that is type "Person" and not specifically "Employee" or "Entrepreneur" ).
How do i tackle this problem?
This is the Demo class
public class Demo{
public static void main(String[] args){
PersonFactory pf = new PersonFactory();
Person p1 = pf.getPerson("Employee");
p1.presentation();
System.out.println(p1.getComplanyName());
}
}
This is the abstract class
public abstract class Person{
String name;
String surname;
abstract void presentation();
}
Those are the two concrete classes that extend Person
public class Entre extends Person{
int licenseNumber;
#Override
public void presentation(){
System.out.println("hi i'm an Entrepreneur");
}
public int licenseNumber(){
return licenseNumber;
}
}
public class Empl extends Person{
String companyName;
#Override
public void presentation(){
System.out.println("hi i'm an employee");
}
public String getCompanyName(){
return companyName;
}
}
Finally the Factory
public class PersonFactory{
public Person getPerson(String type){
if(type.equalsIgnoreCase("ENTREPRENEUR")){
return new Entre();
}
else if(type.equalsIgnoreCase("Employee")){
return new Empl();
}
return null;
}
}

using arraylist to create to inputs

I think I have got myself into a bit of muddle. I had teacher and student below in two separate addMember sections. For the purpose of what I am doing they have to in one section but when someone wants to add either student or teacher they need to be able to be added separately (so one call which will give the option of adding either). But my knowledge of arrayLists is not very good and as you can see isn't working very well. Any help be much needed and appreciated.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Committee {
private String name;
private List<Object> members;
public Committee(String name)
{
this.name = name;
members = new ArrayList<Object>();
List list = new ArrayList();
}
public void addMember(Student student, Teacher teacher)
{
List members1 = new ArrayList();
members1.add(student);
System.out.println(members1);
List members2 = new ArrayList();
members2.add(teacher);
System.out.println(members2);
}
public void printMembership()
{
System.out.println("Membership of the " + name + " Committee ");
Iterator<Object> it = members.iterator();
while (it.hasNext())
{
Object member = it.next();
System.out.println(members);
}
}
}
I'm not sure what you want to achieve, so I will write two solutions:
Situation where you want to have one list
interface Member {...}
class Student implements Member {...}
class Teacher implements Member {...}
public class Comittee {
private list<Member> members;
public void addMember(Member member) {
members.add(member);
}
public void printMembership() {
members.forEach(System.out::println);
}
}
Situation where you want to have two lists
interface Member ...
class Student implements Member ...
class Teacher implements Member ...
public class Comittee {
private list<Teacher> teachers;
private list<Student> students;
public void addMember(Student student, Teacher teacher) {
students.add(student);
teachers.add(teacher);
}
public void printMembership() {
getAllMembers().forEach(System.out::println);
}
private void getAllMembers(): List<Member> {
List<Member> allMembers = ArrayList<>(students.size() + teachers.size());
allMembers.addAll(teachers);
allMembers.addAll(students);
return allMembers;
}
}

How can I express object which should be 2 different classes at the same time

Today I had test in OOP and I was given the following task to code:
Imagine you have two classes: Employee (which represents being an employee) and Ninja (which represents being a Ninja). An Employee has both state and behaviour; a Ninja has only behavior. You need to represent an employee who is also a ninja (a common problem in the real world). By creating only one interface and only one class (NinjaEmployee), show how you can do this without having to copy method implementation code from either of the original classes. Test your code in main method
I did not really understand the problem well, but this is the solution I came with (I know it's not what was asked):
I created 4 classes except main. As Employee has state and behaviour I came up with this code:
public class Employee {
private int ID;
private String Name;
private double salary;
public Employee(int ID, String Name, double salary) {
this.ID = ID;
this.Name = Name;
this.salary = salary;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void startWorking() {
System.out.println("Started Working");
}
}
Class ninja has only behaviour:
public class Ninja {
public Ninja(){}
public void moveNinja(){
System.out.println("Ninja moved");
}
}
Class NinjaEmployee:
public class NinjaEmployee extends Employee implements MyInterface {
public NinjaEmployee(int ID, String Name, double salary) {
super(ID, Name, salary);
}
public void moveNinja() {
System.out.println("Ninja Moved");
}
}
Interface which does not make sense in my code:
public interface MyInterface {
public void moveNinja();
public void startWorking();
}
Main class:
public static void main(String[] args){
MyInterface em = new NinjaEmployee(123,"Sandro",1000000);
em.moveNinja();
em.startWorking();
}
My question is following:
1) Specifically/Technically what was asked in test?
2) What would be correct approach/code for given problem?
Nice question.
The key point of the question is:
we should use one interface.
Ninja class should have some methods (not attributes).
So we should try to use these key point.
I provide a class diagram below:
First of all: We have Employee class and implement it like other simple classes. It has some implemented attributes and classes.
Secondly: We have an Interface named Ninja_Interface that have some method declarations about ninja. (moveNinja1 and moveNinja2)
Thirdly: Ninja Class that implemented (or Realized) Nijna_Interface and have some implementation of any method declarations in Ninja_Interface.
Fourthly: the NinjaEmployee class. It inherited from Employee. So it has all Employee's attributes and methods. Also it implements Ninja_Interface. So it should implements all Ninja_Interface methods declarations. On the other hand, NinjaEmployee have an instance of Ninja (notice that Ninja class implements all Ninja_Interface methods). So, In Ninja_Employee class, in implementation of Ninja_Interface methods, we can use Ninja instance methods to call.
For example some parts of NinjaEmployee is like below code:
private Ninja ninja=new Ninja();
public void moveNinja1()
{
ninja.moveNinja1();
}
public void moveNinja2()
{
ninja.moveNinja2();
}
Main question is: why Ninja class should have only some methods?
It is because of Ninja class is just the implementations of Ninja_Interface methods and there no need to have attributes. So instances of Ninja class are the same. So we can declare Ninja attribute in NinjaEmployee as static attribute.
Finally: we can add some attributes of ninja into NinjaEmployee class too.
I don't know correct answer (task is kinda not very strictly defined, there is some unclear moments), but i would do something like this:
public interface IAmNinja {
public void moveNinja();
}
public interface IAmEmployer {
public void startWorking();
}
public class NinjaEmployee implements IAmNinja, IAmEmployer {
private Ninja _ninja;
private Employer _employer;
public NinjaEmployee(int ID, String Name, double salary) {
_employer = new Employer(ID, Name, salary);
_ninja = new Ninja();
}
public void moveNinja() {
_ninja.moveNinja();
}
public void startWorking() {
_employer.startWorking();
}
}
You cant create 1 object of 2 class es
You can extend class so whenever child class is instantiated it calls parent class constructor
Then You can create object of another class in that constructor
Add employees in array and add option to add employee in ninja? 1.yes or 2.no?
if yes , add to ninja..then in main method print names of ninja using for loop one by one

Calling a method on every created object

I've been wondering is there any way to call a method for EVERY object of one specific class?
Let's say I have a class:
public class Employee {
private double salary;
private boolean hired;
public double getSalary() {return salary;}
public double setSalary(double x) {salary=x;}
public boolean getHired() {return hired;}
public boolean setHired(boolean check) {hired=check;}
}
then I declare another class that extends Employee, e.g. Boss (because Boss is an employee too, right)
so he inherits "gets" and "sets" from Employee and also has the possibility to fire employee:
public class Boss extends Employee {
public void fireTheGuy(Employee unluckyYou) {
boolean temp;
temp=false;
unluckyYou.setHired(temp);
unluckyYou.setSalary(0.0);
}
this allows in main:
public static void main(String args[])
{
Employee worker1 = new Employee();
Employee worker2 = new Employee();
Boss slacker = new Boss();
slacker.fireTheGuy(worker1);
slacker.fireTheGuy(worker2);
}
but what if I have like 100 employees and the Boss wants to fire them all? how this method should be called (is this even possible by some kind of loop?)
You could make all employees register themselves in a common static List. Then when the boss is told to fire everyone he just walks the list.
static List<Employee> allEmployees = new ArrayList<>();
public class Employee {
private double salary;
private boolean hired;
public Employee() {
// All employees must register themselves in the allEmployees list.
allEmployees.add(this);
}
public double getSalary() {return salary;}
public double setSalary(double x) {salary=x;}
public boolean getHired() {return hired;}
public boolean setHired(boolean check) {hired=check;}
}
public class Boss extends Employee {
public void fireTheGuy(Employee unluckyYou) {
unluckyYou.setHired(false);
unluckyYou.setSalary(0.0);
}
public void fireThese(Iterable<Employee> fireThem) {
for ( Employee e : fireThem ) {
fireTheGuy(e);
}
}
public void fireEveryone() {
fireThese(allEmployees);
}
}
This is an example of code of what you could do
List<Employee> employees=new ArrayList<Employee>();
employees.add(worker1);
employees.add(worker2);
for(Employee worker:employees){
slacker.fireTheGuy(worker);
}
But like mentionned in the comments you should try to understand all the concepts behind this example.

How to store an object in to a variable

I am a beginner in JAVA and I could use some help. So I have 3 classes : Client,BankAccount and Test. I need to add into a Client object the details from a BankAccount. This is the code:
public class Client {
private String name;
private String adress;
private BankAccount accounts[];
public Client(String name,String adress,BankAccount accounts){
this.name=name;
this.adress=adress;
this.accounts=accounts;
}
public class BankAccount {
private String numarCont;
private float suma;
public ContBancar(String accNumber,float sum){
this.accNumber=accNumber;
this.sum=sum;
}
public class Test {
public static void main(String[] args) {
Bankaccount b=new Bankaccount("f211s1",200);
Bankaccount b1=new Bankaccount("f23131EUR",5000);
System.out.println(b);
Client c=new Client("John","142VineYard",b);
System.out.println(c);
}
}
A client can have multiple accounts. The problem is that I don't know how can I add the details of object b into object c.
Use a List for your BankAccounts
public class Client {
private String name;
private String adress;
private List<BankAccount> accounts = new ArrayList<BankAccount>();
public Client(String name,String adress,BankAccount account){
this.name=name;
this.adress=adress;
this.accounts.add(account);
}
public void addBankAccount(BBankAccount account){
this.accounts.add(account);
}
}
This way it is a easier to add Accounts.
c.addBankAccount(b);
c.addBankAccount(b1);
Maybe it would help you to take a look at some basic Java tutorials.
It sounds like what you want is inheritance. Inheritance will allow you to use the methods of one class in another class as if it was the same object. The keyword for inheriting in Java is extends used like this:
Public class foo extends foobar

Categories