How to store names in a database in java - java

Hi this is my first time using this because I am confused as to how I should go about this problem.
I have a spreadsheet which has multiple columns such as "house owner names", "Address", "price" etc. All of the columns have 12 values in them relating to 12 individuals each with their address and other such details regarding their property.
I need to create a program where if I enter a certain price range, the program sorts through the spreadsheet and only displays the results that fall within the price range that the user enters.
I first thought of using multiple one-dimensional arrays in parallel, but I am not sure if this is the correct way to do such a thing, also I do not know if it is possible to to search through arrays for specific ranges and then have it display to the console.

In this instance I would say your spreadsheet is acting as your database.
Rather than arrays, I would read the spread sheet into instances (objects) of a class, such that each class represents a row in your spreadsheet, and then put these instances into an ArrayList
You should then be able to "search" the ArrayList.

The object oriented way to handle this is to create a Java object with the 12 column names. Since you only gave us 3 column names, I created this Java objecvt with the 3 names.
public class HomeOwner {
private final String name;
private final String address;
private final double price;
public HomeOwner(String name, String address, double price) {
this.name = name;
this.address = address;
this.price = price;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public double getPrice() {
return price;
}
}
You can now create a List of HomeOwner instances, and search the List by price or any of the other column names.

Related

Is there a way for a combobox to get unique info from enum depending on user selection?

So I am doing some revision in preparation for a uni GUI exam.
My Enum has a list of >20 items that all have a unique name and cost.
This is my current enum
public enum List
{
Brush ("Brush", 2),
Clock ("Brush", 5);
private String name;
private int cost;
private List(String name, int cost)
{
this.name = name;
this.cost = cost;
}
}
I want the user to select an item option in which it references the enum and gets the specific name and cost so I can output it into cost calculations and print lines. How would I be able to do that? All I know is that it can use indexes to search for it but I'm not too sure.

How to create a list of matching objects from multiple lists and delete objects in the matching list such that it reflects in the original list

I'm sorry if this has been asked but I couldn't find something similar from google search so here goes. Say I have two objects
Notebook
public class NoteBook {
private String name;
private String description;
public NoteBook(String name, String description) {
this.name = name;
this.description = description;
}
}
and notes
public class Note {
private String sourceNoteBook
private String name;
private String category;
private String details;
public Note(String sourceNoteBook,String name, String category, String details) {
this.sourceNoteBook = sourceNoteBook;
this.name = name;
this.category = category;
this.details = details;
}
}
In the program the user can create a number of NoteBook objects and each NoteBook stores a variable number of Notes. Ultimately I would like to add a search function that can search for notes by category or name and return a list of the found notes.
Normally I would use 2 For loops to iterate through the list of notebooks and then iterate through the list of notes for each notebook and compare strings. something like this:
For (NoteBook noteBook: noteBooks) {
For(Note note :noteBooks.getNoteList){
if (note.getCategory().contains(someString)) {
matchingNotes.add(notes);
}
}
}
However I now want to be able to delete notes from the matchingNotes list such that the note in the original notebook is also deleted.
Whats the best way of storing and searching these two class such that I can implement such a function.
EDIT:
Just for clarification , the end result is that I would like the user to be able to search for a category of notes across all the notebooks ,the program will then return a list of notes matching that category. He/she can then delete a note from that list such that it is also deleted in the original notebook. E.g. completely removed from the program.
Why don't you just store the notebook information in note class?
public class Note {
private NoteBook sourceNoteBook;
private String name;
private String category;
private String details;
public Note(NoteBook sourceNoteBook,String name, String category, String details) {
this.sourceNoteBook = sourceNoteBook;
this.name = name;
this.category = category;
this.details = details;
}
}
Every data manipulation to note will always affect the notebook which store it
Iterator:
Probably the simplest solution. And since java is using Iterators under the hood in foreach loops, the performance would be the same.
For (NoteBook noteBook: noteBooks) {
Iterator<Note> it = noteBooks.getNoteList().iterator();
while (it.hasNext()) {
Note note = it.next();
if (note.getCategory().equals(someString)) {
it.remove();
}
}
}
SQL:
This would be optimal. However even using something lightweight, such as H2, or SQLite would require refactoring. And also is not an option in very lightweight applications.
Efficient:
If you only search by category, or name, you could use 2 maps:
Map<String, Note> notesByCategory;
Map<String, Note> notesBytName
This would require O(n) memory to store the maps, but would have very efficient lookups in O(1) time (compared to the current O(n)). I would avoid this solution, because it is very easy to achieve incosistent state between content of notes and the maps.
EDIT:
var newNoteNames = newList.stream().map(Note::getName).collect(Collectors.toSet());
var oldNoteNames = noteBooks.stream().flatMap(Notebook::getNodeList).map(Note::getName).collect(Collectors.toSet());
var removedNames = oldNoteNames.removeAll(newNoteNames);
for (var removedName : removedNames) {
for (NoteBook noteBook: noteBooks) {
Iterator<Note> it = noteBooks.getNoteList().iterator();
while (it.hasNext()) {
Note note = it.next();
if (note.getName().contains(removedName)) {
it.remove();
}
}
}
}
edit my answer
you can create a map when the key is the note and the value is the notebook keySet will returned to the user and once he selected the key you have the value to know from which notebook to delete and you have the key that you can delete the note. is that what you meant?

How would I store these values into an object from a .txt file?

I'm new to programming and I was wondering how I would store values into variables in a class using arrays if I'm getting the values from a .txt file. The values would be either strings or ints, how would you adjust to make this work? Here is the text from the txt. file.
Kobe Bryant
USA
4250000
25
21.03
NBA
Lebron James
USA
6450000
27
21.03
NBA
So I need to store each set of 6 values into 6 separate variables in a class using an array. The second index of the array will call the next 6 values.
Create a Class which contains name, country, Salary, age etc.
Create a List of yourClass instances.
Read from file and store them in the list.
class Person {
private String name;
private String country;
private int salary;
private int age;
private double someValue;
private String league;
public Person() { }
public Person(String name, String country, int salary, int age, double someValue, String league) { //set them }
//add setX and getX methods
}
In you main class:
List<Person> persons = new ArrayList<>();
Create a class which contains all the necessary details you want to store in the array list. Then write the details on the text file and using the file concept read the data from the file and create an Array list object to store the data into an array list.

How do I add or subtract String values to an object, and set the value to "none" when it's empty?

I have a class Passengers which has member properties String name, int health, and String disease with setter and getter methods. The disease variable will initially hold null. Here's that class
public class Passengers
{
private String name;
private int health;
private String disease;
public Passengers(String _name, int _health, String _disease)
{
name = _name;
health = _health;
disease = _disease;
}
public void setHealth(int _health)
{
health = _health;
}
public void setDisease(String _disease)
{
disease = _disease;
}
public String getName()
{
return name;
}
public int getHealth()
{
return health;
}
public String getDisease()
{
return disease;
}
}
What I want to know is how I could add new strings onto this variable, and then how to take away. For example, a passenger Bill starts at null for his diseases, and then contracts malaria and the cold. Bill's disease variable should now hold malaria, cold. Now say the user chooses to treat Bill's malaria. How would I
1) add malaria and cold
2) subtract just malaria from disease?
Whenever I attempt to change the disease with
passengers[index].setDisease() = null;
it says "error: method setDisease in class Passengers cannot be applied to given types:
required: String
found: no arguments"
I would reccomend making disease a Set of Strings.
Set<String> diseases = new HashSet<String>();
void addDisease(String disease) {
diseases.add(disease);
}
void removeDisease(String deisease) {
diseases.remove(disease);
}
Sets are "better", in this case, than other Collections because they cannot hold duplicates.
You should give the class a List<String> such as an ArrayList<String> and put the diseases in this List.
Better still, create a class or enum of Disease and have your Passenger class use a List<Disease> and avoid over-use of String. You could then give the class public addDisease(Disease disease) and removeDisease(Disease disease) methods.
Incidentally, your class above should be named Passenger, the singular, not Passengers, the plural, since it represents the concept of a single Passenger.
For your requirement if you are using List like ArrayList you can access your elements(disease names) by index, but it will allow duplicate data to be inserted(same disease may be added multiple times, it will unnecessary increase in number of diseases and may arise some problems).
If you use Set like HashSet it will allow unique element only, so no issues related to duplicated entries but at the same time you can't access a particular disease by index (if you need so, as of now I am not aware of your further requirement).
So as best of my knowledge I suggest you to use LinkedHashSet(HashSet with Linked approach) it will provide you FIFO order without duplicate insertion problem.

Table storage for strings and booleans

I've implemented in-app-billing v3 and I'd like a way to keep some sort of table which contains 3 columns
(String) Name of product, (String) sku, (boolean) true/false
What is the best way to do this? The best way would (in my opinion) allow me to keep a static easily readable list of values where the first two were strings, the third a boolean. At the start all the values for the boolean column were false, but as I query purchases I am able to easily reset the value.
Just to add,
I'm against SQLite databases for the reason that they're just too easy to hack. I'd rather create it in code and thus I'm debating between an ArrayList within an arraylist or matrices or something like the following:
private static class CatalogEntry {
public String sku;
public String name;
public CatalogEntry(String sku, String name) {
this.sku = sku;
this.name = name;
}
}
private static final CatalogEntry[] CATALOG = new CatalogEntry[] {
new CatalogEntry("android.test.purchased", "Item1"),
new CatalogEntry("android.test.canceled", "Item2"),
new CatalogEntry("android.test.refunded", "Item3"),
new CatalogEntry("android.test.item_unavailable", "Item4")
};
Is there a way to reset values in the CATALOG array if I add a third column, otherwise I'm considering using both that and standard arraylists.
Use sqlite with FOUR columns:
sku
name
purchaseDate (or null if not purchased)
md5Hash
When a purchase is made, you just concatenate the sku, name, purchaseDate, device-id, and a PSK, make an md5Hash and store it along with the other data in the row. When you check the contents of the table, you compare the md5 stored against the data, and you can verify whether the purchase was valid, or the table has been tampered with. You can even let the user back up the purchase table to sdcard (or use Android backup).

Categories

Resources