英文:
Inheritance in java using array list
问题
I'm using simple inheritance in java but i'm not able to get the values from the parent class. I'm following all the standards in java but it's returning empty. I know in order to access the parents class we've to use `super` keyword. But i'm getting an empty array for `price_ofProducts` in my program. Where I'm getting things wrong? Need a direction. Here's my code:
package project;
import java.util.ArrayList;
public class Main{
public static void main(String args[])
{
Product p = new Product();
p.name_ofProducts.add("laptop");
p.price_ofProducts.add(5000);
p.Quantity_ofProducts.add(3);
p.name_ofProductCompany.add("dell");
p.Product_idNumber.add(287);
p.name_ofProducts.add("Moblie");
p.price_ofProducts.add(3000);
p.Quantity_ofProducts.add(3);
p.name_ofProductCompany.add("Nokia");
p.Product_idNumber.add(3567);
p.name_ofProducts.add("I pad");
p.price_ofProducts.add(9000);
p.Quantity_ofProducts.add(5);
p.name_ofProductCompany.add("hp");
p.Product_idNumber.add(7845);
p.name_ofProducts.add("ispot");
p.price_ofProducts.add(600);
p.Quantity_ofProducts.add(2);
p.name_ofProductCompany.add("Nokia");
p.Product_idNumber.add(233);
p.name_ofProducts.add("Oven");
p.price_ofProducts.add(10000);
p.Quantity_ofProducts.add(5);
p.name_ofProductCompany.add("Orient");
p.Product_idNumber.add(766);
p.name_ofProducts.add("AC");
p.price_ofProducts.add(15000);
p.Quantity_ofProducts.add(5);
p.name_ofProductCompany.add("Haier");
p.Product_idNumber.add(577);
p.dispalyProduct();
Transaction tr1 = new Transaction();
tr1.Product_Number();
}
}
My Product:
package project;
import java.util.*;
public class Product {
protected int Quantity;
protected int price;
protected String name;
protected int id;
protected String company;
protected ArrayList<String> name_ofProducts= new ArrayList<String>();
protected ArrayList<Integer> price_ofProducts = new ArrayList<Integer>();
protected ArrayList<Integer> Quantity_ofProducts = new ArrayList<Integer>();
protected ArrayList<String> name_ofProductCompany = new ArrayList<String>();
protected ArrayList<Integer> Product_idNumber = new ArrayList<Integer>();
public Product()
{
}
public Product(int price, String name, int Quantity, String company, int id)
{
this.Quantity=Quantity;
this.price = price;
this.name = name;
this.company=company;
this.id=id;
}
Product(ArrayList<Integer> price_ofProducts) {
throw new UnsupportedOperationException("Not supported yet.");
}
// Setter Method
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
public void setQuantity(int Quantity)
{
this.Quantity=Quantity;
}
public void setCompany(String company)
{
this.company=company;
}
public void setId(int id)
{
this.id=id;
}
// Getter Methods
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getQuantity()
{
return Quantity;
}
public String getCompany()
{
return company;
}
public int getId()
{
return id;
}
public void dispalyProduct()
{
System.out.println("Name\tPrices\tQuantity\tcompany\tid_no");
for(int i=0;i<name_ofProducts.size();i++)
{
System.out.println((i+1)+")"+name_ofProducts.get(i)+"\t"+price_ofProducts.get(i)+"\t"+Quantity_ofProducts.get(i)+"\t"+name_ofProductCompany.get(i)+"\t"+Product_idNumber.get(i));
}
}
}
My Transaction class:
package project;
import java.util.*;
public class Transaction extends Product{
private int product_Number;
private int calculation;
public Transaction()
{
}
public void setProduct_Number(int product_Number)
{
this.product_Number=product_Number;
}
public int getProduct_Number()
{
return product_Number;
}
//@SuppressWarnings("resource")
public void Product_Number()
{
/*
Products Display
*/
dispalyProduct();
/*
Declaring Variables
*/
int quantity;
int product_number=0;
/*
Performing Transaction
*/
System.out.println("Enter the Number of product that you want to parchased");
Scanner Input = new Scanner(System.in);
product_number= Input.nextInt();
System.out.println("Product Number is: "+product_number);
System.out.println("Enter Quantity");
quantity = Input.nextInt();
System.out.println("Quantity Entered "+quantity);
//System.out.println(super.price_ofProducts);
switch (product_number)
{
case 1:
try {
System.out.println("Price of Product: " + super.price_ofProducts.get(1-1));
calculation = super.price_ofProducts.get(1-1);
System.out.println(calculation);
calculation(calculation,quantity);
System.out.println("It works.");
} catch (Exception e) {
System.out.println("Error: " + e);
}
break;
case 2:
try {
System.out.println("Price of Product: " + super.price_ofProducts.get(2-1));
calculation = super.price_ofProducts.get(2-1);
calculation(calculation,quantity);
} catch(Exception e) {
System.out.println("Error: " + e);
}
break;
case 3:
try{
System.out.println(super.price_ofProducts.get(3-1));
calculation= super.price_ofProducts.get(3-1);
calculation(calculation,quantity);
} catch(Exception e){
System.out.println("Error: " + e);
}
break;
case 4:
try{
calculation= super.price_ofProducts.get(4-1);
calculation(calculation,quantity);
}
catch(Exception e){
System.out.println("Error: " + e);
}
break;
case 5:
try{
calculation=super.price_ofProducts.get(5-1);
calculation(calculation,quantity);
}catch(Exception e){
System.out.println("Error: " + e);
}
break;
case 6:
try{
calculation=super.price_ofProducts.get(6-1);
calculation(calculation,quantity);
}catch(Exception e){
System.out.println("Error: " + e);
}
break;
default:
System.out.println("you entered wrong number");
break;
}
}
public void calculation(int a, int b)
{
int total;
total
<details>
<summary>英文:</summary>
I'm using simple inheritance in java but i'm not able to get the values from the parent class. I'm following all the standards in java but it's returning empty. I know in order to access the parents class we've to use `super` keyword. But i'm getting an empty array for `price_ofProducts` in my program. Where I'm getting things wrong? Need a direction. Here's my code:
package project;
import java.util.ArrayList;
public class Main{
public static void main(String args[])
{
Product p = new Product();
p.name_ofProducts.add("laptop");
p.price_ofProducts.add(5000);
p.Quantity_ofProducts.add(3);
p.name_ofProductCompany.add("dell");
p.Product_idNumber.add(287);
p.name_ofProducts.add("Moblie");
p.price_ofProducts.add(3000);
p.Quantity_ofProducts.add(3);
p.name_ofProductCompany.add("Nokia");
p.Product_idNumber.add(3567);
p.name_ofProducts.add("I pad");
p.price_ofProducts.add(9000);
p.Quantity_ofProducts.add(5);
p.name_ofProductCompany.add("hp");
p.Product_idNumber.add(7845);
p.name_ofProducts.add("ispot");
p.price_ofProducts.add(600);
p.Quantity_ofProducts.add(2);
p.name_ofProductCompany.add("Nokia");
p.Product_idNumber.add(233);
p.name_ofProducts.add("Oven");
p.price_ofProducts.add(10000);
p.Quantity_ofProducts.add(5);
p.name_ofProductCompany.add("Orient");
p.Product_idNumber.add(766);
p.name_ofProducts.add("AC");
p.price_ofProducts.add(15000);
p.Quantity_ofProducts.add(5);
p.name_ofProductCompany.add("Haier");
p.Product_idNumber.add(577);
p.dispalyProduct();
// System.out.println("Price of Products Array: " + p.price_ofProducts.get(1));
Transaction tr1 = new Transaction();
tr1.Product_Number();
//p.displayProduct();
}
}
My Product:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package project;
import java.util.*;
/**
*
* @author zfhas
*/
public class Product {
protected int Quantity;
protected int price;
protected String name;
protected int id;
protected String company;
protected ArrayList<String> name_ofProducts= new ArrayList<String>();
protected ArrayList<Integer> price_ofProducts = new ArrayList<Integer>();
protected ArrayList<Integer> Quantity_ofProducts = new ArrayList<Integer>();
protected ArrayList<String> name_ofProductCompany = new ArrayList<String>();
protected ArrayList<Integer> Product_idNumber = new ArrayList<Integer>();
public Product()
{
}
public Product(int price, String name, int Quantity, String company, int id)
{
this.Quantity=Quantity;
this.price = price;
this.name = name;
this.company=company;
this.id=id;
}
Product(ArrayList<Integer> price_ofProducts) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
// Setter Method
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
public void setQuantity(int Quantity)
{
this.Quantity=Quantity;
}
public void setCompany(String company)
{
this.company=company;
}
public void setId(int id)
{
this.id=id;
}
// Getter Methods
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getQuantity()
{
return Quantity;
}
public String getCompany()
{
return company;
}
public int getId()
{
return id;
}
public void dispalyProduct()
{
System.out.println("Name\tPrices\tQuantity\tcompany\tid_no");
for(int i=0;i<name_ofProducts.size();i++)
{
System.out.println((i+1)+")"+name_ofProducts.get(i)+""+price_ofProducts.get(i)+"\t"+Quantity_ofProducts.get(i)+"\t"+name_ofProductCompany.get(i)+"\t"+Product_idNumber.get(i));
}
}
}
My Transaction class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package project;
import java.util.*;
/**
*
* @author zfhas
*/
public class Transaction extends Product{
private int product_Number;
private int calculation;
public Transaction()
{
}
public void setProduct_Number(int product_Number)
{
this.product_Number=product_Number;
}
public int getProduct_Number()
{
return product_Number;
}
//@SuppressWarnings("resource")
public void Product_Number()
{
/*
Products Display
*/
dispalyProduct();
/*
Declaring Variables
*/
int quantity;
int product_number=0;
/*
Performing Transaction
*/
System.out.println("Enter the Number of product that you want to parchased");
Scanner Input = new Scanner(System.in);
product_number= Input.nextInt();
System.out.println("Product Number is: "+product_number);
System.out.println("Enter Quantity");
quantity = Input.nextInt();
System.out.println("Quantity Entered "+quantity);
//System.out.println(super.price_ofProducts);
switch (product_number)
{
case 1:
try {
System.out.println("Price of Product: " + super.price_ofProducts.get(1-1));
calculation = super.price_ofProducts.get(1-1);
System.out.println(calculation);
calculation(calculation,quantity);
System.out.println("It works.");
} catch (Exception e) {
System.out.println("Error: " + e);
}
break;
case 2:
try {
System.out.println("Price of Product: " + super.price_ofProducts.get(2-1));
calculation = super.price_ofProducts.get(2-1);
calculation(calculation,quantity);
} catch(Exception e) {
System.out.println("Error: " + e);
}
break;
case 3:
try{
System.out.println(super.price_ofProducts.get(3-1));
calculation= super.price_ofProducts.get(3-1);
calculation(calculation,quantity);
} catch(Exception e){
System.out.println("Error: " + e);
}
break;
case 4:
try{
calculation= super.price_ofProducts.get(4-1);
calculation(calculation,quantity);
}
catch(Exception e){
System.out.println("Error: " + e);
}
break;
case 5:
try{
calculation=super.price_ofProducts.get(5-1);
calculation(calculation,quantity);
}catch(Exception e){
System.out.println("Error: " + e);
}
break;
case 6:
try{
calculation=super.price_ofProducts.get(6-1);
calculation(calculation,quantity);
}catch(Exception e){
System.out.println("Error: " + e);
}
break;
default:
System.out.println("you entered wrong number");
break;
}
}
public void calculation(int a, int b)
{
int total;
total = a*b;
billDisplay(total);
}
public void billDisplay(int total)
{
System.out.println("your total bill = "+total);
billrecive(total);
}
public void billrecive(int total)
{
@SuppressWarnings("resource")
Scanner Input = new Scanner(System.in);
int enteredMoney=Input.nextInt();
int i=0;
while(i>1)
{
if(enteredMoney==total)
{
displayRecipt();
break;
}
else if (enteredMoney<total)
{
System.out.print("Enter More= ");
int enteredMoney1=Input.nextInt();
enteredMoney+=enteredMoney1;
continue;
}
else if(enteredMoney>total)
{
enteredMoney=enteredMoney-total;
System.out.println("Your remaining are : "+enteredMoney);
displayRecipt();
break;
}
}
}
public void displayRecipt()
{
System.out.println("billing done");
dispalyProduct();
}
public void addProduts(String name,int price,int quantity,String company, int id)
{
name_ofProducts.add(name);
price_ofProducts.add(price);
Quantity_ofProducts.add(quantity);
name_ofProductCompany.add(company);
Product_idNumber.add(id);
}
}
</details>
# 答案1
**得分**: 1
```Product p``` 和 ```Transaction tr1``` 是两个完全独立的对象实例。
你在 ```p``` 中设置了所有的产品,然后通过 ```tr1``` 查询用户。当你询问用户要选择哪个产品时,```tr1``` 不具有任何相同的产品数据(或任何数据)。之所以能够显示产品,仅因为你调用了 ```p.displayProduct```。
<details>
<summary>英文:</summary>
```Product p``` and ```Transaction tr1``` are two completely separate object instances.
You set up all of your products in ```p``` and then query the user via ```tr1```. ```tr1``` doesn't have any of the same product data (or any data at all) when you ask the user which one to pick. It only displays the products at all because you call ```p.dispalyProduct```
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论