Java data structure for String and Class - java

I am looking to store information in a list or array, and then access the info through a method using a String parameter.
public void blah(String name){
DataStructure[apple].getInfo1; //a float
DataStucture[apple].getInfo2; //an int
Some pseudocode of how the info will be stored:
DataStructure.add(apple);
apple.setFloat(5f);
apple.setInt(1);
My main confusion is how to access this information using a String. Since I will be having a lot of objects in this structure, I figured the easiest way to access the info would be to look up the name directly.

Like this:
public class DataStructure {
private float info1;
private int info2;
public DataStructure(float info1, int info2) {
super();
this.info1 = info1;
this.info2 = info2;
}
public float getInfo1() {
return info1;
}
public void setInfo1(float info1) {
this.info1 = info1;
}
public int getInfo2() {
return info2;
}
public void setInfo2(int info2) {
this.info2 = info2;
}
}
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
final HashMap<String, DataStructure> map = new HashMap<String, DataStructure>();
map.put("apple", new DataStructure(5f, 1));
}
}

Related

save arrayList<Class> as a string inside Room

I am using Room library from jetpack and i want to save my arrayList inside Room database.
I am using typeConverter to convert arrayList into String, but still getting error.
Error Message : Cannot figure out how to save this field into database. You can consider adding a type converter for it.
this is my typeConverter.
public class TypeConverterUtils {
#TypeConverter
public static String fromArray(ArrayList<CropData> arrayList) {
return GsonUtils.toGson(arrayList);
}
}
this is my Database class.
#TypeConverters({TypeConverterUtils.class})
public abstract class CheruvuDatabase extends RoomDatabase {
private static final String DATABASE_NAME = "Cheruvu.db";
public abstract OtpDAO otpDAO();
public abstract FarmerInfoDAO farmerInfoDAO();
// For Singleton instantiation
private static final Object LOCK = new Object();
private static volatile CheruvuDatabase sInstance;
public static CheruvuDatabase getInstance(Context context) {
if (sInstance == null) {
synchronized (LOCK) {
if (sInstance == null) {
context.getApplicationContext().deleteDatabase(CheruvuDatabase.DATABASE_NAME);
sInstance = Room.databaseBuilder(context.getApplicationContext(), CheruvuDatabase.class, CheruvuDatabase.DATABASE_NAME)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build();
}
}
}
return sInstance;
}
}
I want to store this ArrayList.
private ArrayList cropData = new ArrayList<>();
this is my CropData class.
public class CropData {
private String cropName;
private Crop crop;
private Float cropAcres;
private Float cropYield;
private Float cropPrice;
public String getCropName() {
return cropName;
}
public void setCropName(String cropName) {
this.cropName = cropName;
}
public Crop getCrop() {
return crop;
}
public void setCrop(Crop crop) {
this.crop = crop;
}
public Float getCropAcres() {
return cropAcres;
}
public void setCropAcres(Float cropAcres) {
this.cropAcres = cropAcres;
}
public Float getCropYield() {
return cropYield;
}
public void setCropYield(Float cropYield) {
this.cropYield = cropYield;
}
public Float getCropPrice() {
return cropPrice;
}
public void setCropPrice(Float cropPrice) {
this.cropPrice = cropPrice;
}
}
Have you added #Embedded on the ArrayList variable? because you will be needed to Embed the Class, that you want to convert.
for example,
ArrayList<CropData> cropData;
and CropData.class is different. so just add
#Embedded
ArrayList<CropData> cropData;
and your problem will be solved.

Iterating and searching in LinkedList

I am printing LinkedList like this:
public class cinema{
public static LinkedList <cinema> cinList = new LinkedList<cinema>();
private int cinemaNum,numSeats;
private String row;
public cinema(int getCinemaNumber,String getRowName,int getNumSeats){
this.cinemaNum = getCinemaNumber;
this.row = getRowName;
this.numSeats = getNumSeats;
//todo
}
public static void addHalls(cinemaValues cin){
cinema Cinema = new cinema(cin.getCinemaNumber(), cin.getRowName(),cin.getNumSeats());
cinList.add(Cinema);
}
public static void print(){
System.out.println(cinList);
}
#Override
public String toString() {
return "cinnum:"+cinemaNum+" row:"+row+" seats:"+numSeats;
}
}
public class session{
public static LinkedList <session> sessList = new LinkedList<session>();
private int CinemaSessionNum;
private String time,movie;
public session(int getCinemaSessionNum, String getTime,String getMovie){
this.CinemaSessionNum = getCinemaSessionNum;
this.time = getTime;
this.movie = getMovie;
}
public static void addSession(CinemaSessions sess){
session ses = new session(sess.getCinemaSessionNum(),sess.getTime(),sess.getMovie());
sessList.add(ses);
}
public static void print(){
System.out.println(sessList);
}
#Override
public String toString() {
return "cinnum:"+CinemaSessionNum+" time:"+time+" movie:"+movie;
}
}
the output:
I want a way to be able search all three parts of an elements using one of the keys
basically,
when the condition is Cinnum:2 I want it to return
Cinnum:1 time:9:00 movie:toy
Cinnum:1 time:14:30 movie:Ratatouille
Any help would be appreciated.
Thanks

I am trying to declare an array from my main class with setters

I am trying to declare an array from my main class with setters I don' t know if this is the correct way.In my other class i have all the methods and they work fine but i need to declare the array and it compiles but it doesn't seem like the arrays CC and ccBal are declared.
Here is my code and this is where the problem is. I dont know if i am initializing the array with the setters correctly.
public class handleCustomers {
public static void main(String[] args) {
Customer [] CC = new Customer[1];
CC [0] = new Customer();
CC[0].setCC(new String[]{"1234567894123569"});
CC[0].setCCBal(new double []{3070.00});
}
This is the Customer class
public class Customer {
private String[] CC;
private double[] ccBal;
public Customer() {}// default constructor
public Customer(String [] CreditCards){
CC = CreditCards;
}
public Customer(double [] creditBalance){
ccBal = creditBalance;
}
public String [] getCC(){// Getters
return CC;
}
public double [] getCCBal() {
return ccBal;
}
public void setCC(String [] CreditCards){// Setters
CC = CreditCards;
}
public void setCCBal(double [] creditBalance){
ccBal = creditBalance;
}
I'm not sure what you trying to do, I can't ask you in comments because I'm new in StackOverflow. Provide me more information. But that's how you go with this type of problem. And I don't know why you creating array as you just have only one value. Best
public class handleCustomers {
public static void main(String[] args) {
Customer bank = new Customer("1234567894123569",3070.00);
System.out.println(bank.toString());
}
}
Customer Class
public class Customer {
private String CC;
private double ccBal;
public Customer(String CC, double ccBal) {
this.CC = CC;
this.ccBal = ccBal;
}
public String getCC(){// Getters
return this.CC;
}
public double getCCBal() {
return this.ccBal;
}
public void setCC(String CC){// Setters
this.CC = CC;
}
public void setCCBal(double creditBalance){
this.ccBal = ccBal;
}
public String toString() {
return "CC:" +CC + "balance: " +ccBal;
}
}

can i print a setter value (sysout) or do i have to use only getters in order to get an output ? (java)

I have just recently learned about setter , getters and this.(somthing).
I having quite a hard time undersatnding when to use getters and when to use setters .
Another thing , can i use setter method to print out ?
For Example :
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
newAge = workerAge;
}
public void setWorkerName(String newName) {
newName = workerName;
}
public int setIde(int ide) {
ide = workerIde;
return ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde());
}
}
the system out print shows an error and i didnt understand why , is it because only getters can be used in the sysout command ?
No offense intended, but your setters are all wrong. You should assign your properties to the values passed in the setter, not setting the value again. So your code should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
}
If you need getters, it should look like this:
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName = newName;
}
public int setIde(int ide) {
workerIde = ide;
}
public int getIde() {
return workerIde;
}
}
Then you can print, e.g. System.out.println(worker1.getIde());
You should be using a getter method to get the values.
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
workerAge = newAge;
}
public void setWorkerName(String newName) {
workerName=newName;
}
public int getIde() {
return workerIde;
}
public void setIde(int ide) {
workerIde = ide;
}
}
public class App {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.getIde());
}
}
class workerId {
private int workerAge;
private String workerName;
private int workerIde;
public void setWorkerAge(int newAge) {
this.workerAge = newAge;
}
public void setWorkerName(String newName) {
this.workerName = newName;
}
public int setIde(int ide) {
this.workerIde = ide;
return this.workerIde;
}
}
public class Car {
public static void main(String[] args) {
workerId worker1 = new workerId();
worker1.setWorkerAge(41);
worker1.setWorkerName("dan ");
worker1.setIde(318574524);
System.out.println(worker1.setIde(56));
}
}

Passing a Collection of Java Beans as DataSource to Jasper, How to design this in ireports

I need to design a report, that displays the data from a collection(Say List). This list contains multiple POJOs.
The POJOs are populated by the data access layer of the application. How do I design a report template for this requirement in iReports?
Use JRBeanCollectionDataSource for your report.
Ok! Found the answer. The steps are as below.
Compile the bean classes and create a JAR file. This needs to have the complete package
Add this jar to the LIB folder in ireports
Create a factory/wrapper class that has a createBeanCollection method that populates a collection
Use this class's top level package as the class path in ireports
Use this class as the JavaBean datasource with the method.
Once all this is done, create a report with the new datasource and in report query, give the FQN on the Java bean and add the desired field.
BankDetailsList list = new BankDetailsList();
ArrayList<BankDetails> lst = list.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(lst);
Here BankDetails is a POJO
public class BankDetails {
public String bank_name;
public Account account;
public String custodian_account;
public String custodian_name;
public String agreement_type;
public double exposure;
public double collateral;
public double independant_amount;
public double net_exposure;
BankDetails(String b_name, Account acc, String cust_account,
String cust_name, String agr_type, double expo, double collat,
double independant_amt, double net_exp) {
this.bank_name = b_name;
this.account = acc;
this.custodian_account = cust_account;
this.custodian_name = cust_name;
this.agreement_type = agr_type;
this.exposure = expo;
this.collateral = collat;
this.independant_amount = independant_amt;
this.net_exposure = net_exp;
}
public String getBank_name() {
return bank_name;
}
public void setBank_name(String bank_name) {
this.bank_name = bank_name;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getCustodian_account() {
return custodian_account;
}
public void setCustodian_account(String custodian_account) {
this.custodian_account = custodian_account;
}
public String getCustodian_name() {
return custodian_name;
}
public void setCustodian_name(String custodian_name) {
this.custodian_name = custodian_name;
}
public String getAgreement_type() {
return agreement_type;
}
public void setAgreement_type(String agreement_type) {
this.agreement_type = agreement_type;
}
public double getExposure() {
return exposure;
}
public void setExposure(double exposure) {
this.exposure = exposure;
}
public double getCollateral() {
return collateral;
}
public void setCollateral(double collateral) {
this.collateral = collateral;
}
public double getIndependant_amount() {
return independant_amount;
}
public void setIndependant_amount(double independant_amount) {
this.independant_amount = independant_amount;
}
public double getNet_exposure() {
return net_exposure;
}
public void setNet_exposure(double net_exposure) {
this.net_exposure = net_exposure;
}
}
Account POJO:
public class Account {
public int account_id;
public String account_name;
Account(int acc_id, String acc_name){
this.account_id = acc_id;
this.account_name = acc_name;
}
public int getAccount_id() {
return account_id;
}
public void setAccount_id(int account_id) {
this.account_id = account_id;
}
public String getAccount_name() {
return account_name;
}
public void setAccount_name(String account_name) {
this.account_name = account_name;
}
}

Categories