Check for null field in arraylist - java

I can find plenty of Q&A on here about whether an arraylist can equal null, which was helpful in its own way, but I can't find an answer for throwing errors if any fields in the arraylist are null. As I'm adding objects to the arraylist, I want to throw an exception if the user tries to pass in anything that is null. Here is the code:
void addInvoiceItem(Item item, Integer quantity, double discount) throws Exception {
for (InvoiceItem thing: invoiceList) {
if (thing == null) {
throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
}
else {
invoiceList.add(thing);
thing.setItemQuantity(quantity);
thing.setItemDiscount(discount);
System.out.println(invoiceList);
}
}
}
Here is the Item class:
final class Item {
String itemDescription;
double itemPrice;
Integer itemSKU;
Item (String description, double price, Integer sku) {
this.itemDescription = description;
this.itemPrice = price;
this.itemSKU = sku;
}
}
Here are the test methods that are letting me know I'm definitely omitting something. One is to test for a valid InvoiceItem, the other for an invalid one (contains nulls):
public class InvoiceTest {
//create the static values to be used
//for InvoiceItem
String goodDescription = "wheel";
double goodPrice = 500.00;
Integer goodSku = 0002;
Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);
String emptyDescription = null;
double emptyPrice = 0;
Integer emptySku = 0;
Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);
Integer itemQuantity = 0;
double itemDiscount = 0.05;
#Test
public void invalidItemAddTest() {
Invoice badInvoice = new Invoice(null);
try {
badInvoice.addInvoiceItem(badInvoiceItem, itemQuantity, itemDiscount);
System.out.println(badInvoice);
} catch (Exception e) {
e.printStackTrace();
}
}
#Test
public void validItemAddTest() {
Invoice goodInvoice = new Invoice(null);
try {
goodInvoice.addInvoiceItem(goodInvoiceItem, itemQuantity, itemDiscount);
System.out.println(goodInvoice);
} catch (Exception e) {
e.printStackTrace();
}
}
Thanks in advance for all your help
Edit with additions:
So Mel's answer was my starting point and I made some additions to get it working the way I needed to. My adding method now looks like this:
void addInvoiceItem(Item item, Integer quantity, double discount) {
if(item == null || quantity == 0 || discount == 0) {
throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
} else {
InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
invoiceList.add(invoice);
}
}
and my test methods look like this:
public class InvoiceTest {
//create the static values to be used
//for InvoiceItem
String goodDescription = "wheel";
double goodPrice = 500.00;
int goodSku = 0002;
Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);
String emptyDescription = null;
double emptyPrice = 0;
int emptySku = 0;
Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);
int badItemQuantity = 0;
double badItemDiscount = 0;
int goodItemQuantity = 1;
double goodItemDiscount = 0.05;
/**
* #Before - initialize what we need for the test
* #throws Exception
*/
#Before
public void setUp() {
//things needed for testInvalidItemAdd()
}
/**
* #throws Exception
* #Test - confirm you cannot add an item that is null
*/
#Test
public void invalidItemAddTest() {
boolean exceptionThrown = false;
Invoice badInvoice = new Invoice(null);
try {
badInvoice.addInvoiceItem(badInvoiceItem, badItemQuantity, badItemDiscount);
System.out.println(badInvoice);
} catch (Exception e) {
e.printStackTrace();
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}
#Test
public void validItemAddTest() {
boolean exceptionThrown = false;
Invoice goodInvoice = new Invoice(null);
try {
goodInvoice.addInvoiceItem(goodInvoiceItem, goodItemQuantity, goodItemDiscount);
System.out.println(goodInvoice);
} catch (Exception e) {
e.printStackTrace();
exceptionThrown = true;
}
assertFalse(exceptionThrown);
}

Your add routine is a little off. It tries to use an item that is already in the list rather than creating a new one. Try this:
void addInvoiceItem(Item item, Integer quantity, double discount) {
if(
item == null ||
quantity == null ||
item.sku == null ||
item.description == null
) {
throw new NullPointerException();
}
InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
invoiceList.add(invoice);
}
Also take a look a checkNotNull( ) from the google library to reduce typing a bit.
It may be useful to check for null in the InvoiceItem constructor rather than in the adder, unless you want to allow nulls elsewhere.

Related

Java - parsing a 2nd csv file to override a value

I have two csv files. One shows all crime data including City, State, Population etc. The other shows State and Abbreviation. I want to have the state set as the abbreviation, currently I have some very long code and I'm thinking there is definitely a better way at setting it based on the abbreviation csv file.
My main class:
public class StartApp {
public static ArrayList<CityCrime> crimes = new ArrayList<CityCrime>();
public static ArrayList<String> cities = new ArrayList<String>();
/**
* Start point for app. Directs the reads from file and shows the menu
* #param args
*/
public static void main(String[] args) {
try {
readCrimeData("crimeUSA.csv");
System.out.println("Total cities read: " + getTotalCities());
showMenu();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Reads the crime data for each city from entered file
* Adds the CityCrime objects to the crimes ArrayList
*/
public static void readCrimeData(String fromFile) {
File file = new File(fromFile);
FileReader fileReader;
BufferedReader bufferedReader;
String crimeInfo;
String[] stats;
try {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
crimeInfo = bufferedReader.readLine();
crimeInfo = bufferedReader.readLine();
do {
CityCrime crime = new CityCrime(); // Default constructor
stats = crimeInfo.split(",");
{
if (stats[0] != null) {
crime.setCity(stats[0]);
}
if (stats[1] != null) {
crime.setState(stats[1]);
}
if (stats[2] != null) {
if (Integer.parseInt(stats[2]) >= 0) {
crime.setPopulation(Integer.parseInt(stats[2]));
}
}
if (stats[3] != null) {
if (Integer.parseInt(stats[3]) >= 0) {
crime.setMurder(Integer.parseInt(stats[3]));
}
}
if (stats[4] != null) {
if (Integer.parseInt(stats[4]) >= 0) {
crime.setRobbery(Integer.parseInt(stats[4]));
}
}
if (stats[5] != null) {
if (Integer.parseInt(stats[5]) >= 0) {
crime.setAssault(Integer.parseInt(stats[5]));
}
}
if (stats[6] != null) {
if (Integer.parseInt(stats[6]) >= 0) {
crime.setBurglary(Integer.parseInt(stats[6]));
}
}
if (stats[7] != null) {
if (Integer.parseInt(stats[7]) >= 0) {
crime.setLarceny(Integer.parseInt(stats[7]));
}
}
if (stats[8] != null) {
if (Integer.parseInt(stats[8]) >= 0) {
crime.setMotorTheft(Integer.parseInt(stats[8]));
}
}
crime.setTotalCrimes(Integer.parseInt(stats[3]), Integer.parseInt(stats[4]), Integer.parseInt(stats[5]), Integer.parseInt(stats[6]), Integer.parseInt(stats[7]), Integer.parseInt(stats[8]));
}
crimes.add(crime);
System.out.println(crime);
crimeInfo = bufferedReader.readLine();
} while (crimeInfo != null);
fileReader.close();
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* For each crime, add the city to the cities ArrayList and return the count
*/
public static int getTotalCities() {
for(CityCrime crime : crimes) {
cities.add(crime.getCity());
}
int cityCount = cities.size();
return cityCount;
}
/**
* Displays the menu
* User can select which function they want to run
* #throws IOException
*/
#SuppressWarnings("unlikely-arg-type")
public static void showMenu() throws IOException {
#SuppressWarnings("resource")
Scanner menuSelect = new java.util.Scanner(System.in);
System.out.println("1. Display all crime stats by city");
System.out.println("2. Display all crime stats by selected city");
System.out.println("3. Display the murder stats by selected state ");
System.out.println("4. Display highest crime city - all crimes");
System.out.println("5. Display each state (in alphabetical order with the number of car thefts ");
System.out.println("6. Write / export all cities in descending order of Robbery rate ");
System.out.println("7. Quit");
System.out.println("Enter option 1-7");
#SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
int option = Integer.parseInt(menuSelect.next());
if(option<1 || option>7 ) {
System.out.println("Invalid input.");
return;
}
switch (option) {
case 1:
displayAllCityCrimeStats();
break;
case 2:
System.out.println("Enter city");
String cityOption = menuSelect.next();
displayCrimeStatsByCity(cityOption);
break;
case 3:
System.out.println("Enter state");
String stateOption = menuSelect.next();
displayMurdersByState(stateOption);
break;
case 4:
displayHighestCrimeStats();
break;
case 5:
displayStateCarThefts();
break;
case 6:
writeToFile("Robbery.csv");
break;
case 7:
return;
default:
option = Integer.parseInt(scanner.next());
}
}
My CityCrime file. It is a mess right now as I don't know what the right direction to go in is. I have cut down the setStates, there are 52 in reality so it's pretty long:
public class CityCrime {
//Instance variables
private String city;
private String state;
private int population;
private int murder;
private int robbery;
private int assault;
private int burglary;
private int larceny;
private int motorTheft;
public int totalCrimes;
public static ArrayList<CityState> abbreviations = new ArrayList<CityState>();
public String fromFile = ("C:/Users/ebeck/Downloads/StateAbbreviations.csv");
public static void main(String[] args) {
}
public static void readAbbrevData(String fromFile) {
File file = new File(fromFile);
FileReader fileReader;
BufferedReader bufferedReader;
String abbrevInfo;
String[] stats;
try {
fileReader = new FileReader(file);
bufferedReader = new BufferedReader(fileReader);
abbrevInfo = bufferedReader.readLine();
abbrevInfo = bufferedReader.readLine();
do {
CityState abbrev = new CityState(); // Default constructor
stats = abbrevInfo.split(",");
{
if (stats[0] != null) {
abbrev.setState(stats[0]);
}
if (stats[1] != null) {
abbrev.setAbbreviation(stats[1]);
}
}
abbreviations.add(abbrev);
System.out.println(abbrev);
abbrevInfo = bufferedReader.readLine();
} while (abbrevInfo != null);
fileReader.close();
bufferedReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
if(state.equalsIgnoreCase("ALABAMA")) {
this.state = "AL";
}
else if(state.equalsIgnoreCase("ALASKA")) {
this.state = "AK";
}
else if(state.equalsIgnoreCase("ARIZONA")) {
this.state = "AR";
}
else if(state.equalsIgnoreCase("ARKANSAS")) {
this.state = "AR";
}
else if(state.equalsIgnoreCase("CALIFORNIA")) {
this.state = "CA";
}
else if(state.equalsIgnoreCase("COLORADO")) {
this.state = "CO";
}
else if(state.equalsIgnoreCase("CONNECTICUT")) {
this.state = "CT";
}
//etc
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public int getMurder() {
return murder;
}
//etc
}
I created a CityState file, however may be unnecessary:
public class CityState {
private String state;
private String abbreviation;
public static void main(String[] args) {
}
public String getState() {
return state;
}
public String getAbbreviation() {
return abbreviation;
}
public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
public void setState(String state) {
this.state = state;
}
}
A couple reasons I want to change how I've set up the get state is 1. for my Junit test for state:
#Test
public void testValidState() {
CityCrime crimes = new CityCrime();
crimes.setState(state);
assertEquals(crimes.getState(), state);
}
I was getting the error:
expected: <A[K] but was: <A[LASKA]> if I set the state to "Alaska"
Then if I set it to "AK" I got the error:
expected: but was:
and 2. it doesn't look great either and I want to learn a better way
Thankyou for your help I appreciate it
Edit:
For each crime, if the state in CityCrimes csv file is equal to the state in the StatesAbbreviations file, then set the state as the abbreviation in the StatesAbbreviations file and return
Okay, first of all, I think it's better to split the data from the data parsing.
One class = one representation. Here CityCrime has two goals: represent quantity of crimes in a City (the goal indicated by the class name) and parse the abbreviation list. So I think it's better to create a new class whose goal is the parsing of your abbreviations.
Next, there is a consistency problem: you have a function that parse your abbreviation CSV, but you have also a list of if to "convert" a state name to a state abbreviation (setState function). There is, according to me, two ways to do this:
Define State as a type
Define State as resource
State as a type
The advantage of this method is to benefit from a strong typing. Indeed, with your setState solution, you define a State as a String, and you have to check if the passed value is correct (succession of if). Moreover, as it remains a String, you have no guarantee that the value returned by getState is formatted well (you have to trust all the functions that write on state variable).
So the solution here is to defined State as a type, using, for example, Enumeration.
public enum State {
ALABAMA("AL"),
ALASKA("AK"),
ARIZONA("AR"),
ARKANSAS("AR"),
CALIFORNIA("CA"),
COLORADO("CO"),
CONNECTICUT("CT");
private String abbreviation;
State(String abbreviation) {
this.abbreviation = abbreviation;
}
public String getAbbreviation() {
return abbreviation;
}
}
An enum is a particular type of class. Indeed, this one is not direclty instanciable (you can't do new State("foo")). The accepted instances for this type are defined as an enumeration, at the start of the class (ALABAMA, ALASKA...). So State.ALABAMA has for type State and State.ALABAMA.getAbbrevation() returns AL.
This solution works only if the set of all your states is closed, so if your have a fixed number of state.
In this situation, your state variable would have for type State, and your setState function would take a State variable. This is strong typed, because you can't pass a value that is not valid to the function (except null).
How to convert a State name to a State variable ?
Enumeration classes have a valueOf(String name) function. The parameter is the identifier of the enum constant (ALABAMA, ALASKA) as a String, and the returned value is the enum constant as State. If any enum constant was found, an IllegalArgumentException is throwed. This solution works well, but the string must correspond exactly to the identifier (case included).
If you want to do a similar function, but ignoring case, you can use values function that returns an array of all State values (State[]). For example:
public static State valueOfIgnoreCase(String name) {
for(var state: State.values())
if(state.name.equals(name)) // name function return the exact identifier of the state
return state;
return null; // Or throw IllegalArgumentException
}
This function can be create in the State class.
State as a resource
Another solution is to define State as a resource, that is, in an external file (or resource file). The advantage is you can add new State dynamically, without having to modify the program code. It's the solution you use with your parser.
For this solution, the parser is in a separated class.
Instead of put all your State in an List, you can put in a Map:
public class AbbreviationParser {
public static Map<String, State> parseState(InputStream stream) throws IOException { // InputStream is better than String or File, because it abstract the type of input (can works with a simple file, a network stream, a text downloaded from Internet...)
final var map = new HashMap<String, State>();
final var reader = new BufferedReader(new InputStreamReader(stream)); // Define a reader on the stream then bufferize it for better performance
reader.readLine();
String line;
while((line = reader.readLine()) != null) { // As long as there is an unread line
var array = line.split(",");
if(array.length == 2) {// Important, because if your line does not contain a comma, your actual code will crash at "array[1]"
var state = new State();
state.setState(array[0]);
state.setAbbreviation(array[1]);
map.put(state.getState(), state);
} else {
//TODO define a behavior if the line is not valid
}
}
reader.close();
stream.close();
return map;
}
To call this function for a File:
parseState(new FileInputStream(fromFile)); // FileInputStream is an implementation of InputStream for File
To secure your State from external instantiation, you can put the State constructor in package-only scope, and place this class and AbbreviationParser in the same package. Like this, only the class on the same package (so AbbreviationParser) can instantiate State, and you are thus sure not to have an incorrect value passed to your setState(State state):
public class State {
private String state;
private String abbreviation;
State() {}
public String getState() {
return state;
}
public String getAbbreviation() {
return abbreviation;
}
public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
public void setState(String state) {
this.state = state;
}
}
Even if you prefer the first method for your State, I recommend you to take inspiration from my parser for your first CSV parser and to isolate it in a separated class.
You want to replace certain values in the first CSV file with corresponding values in the second CSV file. The code will be extremely long if you use Java to perform the association.
But it is easy to do the coding job using Java’s open-source package SPL. One line of code is enough:
+
1
=file("crimeUSA.csv").import#ct().switch(State,file("StateAbbreviations.csv").import#ct():State).new(City,State.Abbreviation: StateAbbreviation,Population,Murder,Robbery,Assault,Burglary,Larceny,MotorTheft,TotalCrimes)
SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as abbr.splx and invoke it in Java as you call a stored procedure:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st=con.prepareCall("call abbr()");
st.execute();
…
Or execute the SPL string within a Java program as we execute a SQL statement:
…
st = con.prepareStatement("==file(\"crimeUSA.csv\").import#ct().
switch(State,file(\"StateAbbreviations.csv\").import#ct():State)
.new(City,State.Abbreviation,Population,Murder,Robbery,Assault
,Burglary,Larceny,MotorTheft,TotalCrimes)");
st.execute();
…

JAVA: JUNIT testing of class type with string

So I have a test which is to test the addNewCustomer method which does so by reading in from a text file
#Test
public void testAddNewCustomer() {
System.out.println("addNewCustomer");
try {
File nFile = new File("ProductData.txt");
File file = new File("CustomerData.txt");
Scanner scan = new Scanner(file);
ElectronicsEquipmentSupplier ees = new ElectronicsEquipmentSupplier(1, 1, InputFileData.readProductDataFile(nFile));
ees.addNewCustomer(InputFileData.readCustomerData(scan));
CustomerDetailsList expResult = ees.getDetails();
CustomerDetailsList result = ees.getDetails();
assertEquals(expResult, result);
} catch (IllegalCustomerIDException | IOException | IllegalProductCodeException e) {
fail(e.getMessage());
}
}
The problem that I'm having is to what to have as the expected result? I tried putting a string with the values that I thought would be entered but it then said I can't compare type string with type CustomerDetailsList. Any ideas?
public class CustomerDetailsList {
private final ArrayList<CustomerDetails> customerCollection;
public CustomerDetailsList() {
customerCollection = new ArrayList<>();
}
public void addCustomer(CustomerDetails newCustomer) {
customerCollection.add(newCustomer);
}
public int numberOfCustomers() {
return customerCollection.size();
}
public void clearArray() {
this.customerCollection.clear();
}
/**
*
* #param givenID the ID of a customer
* #return the customer’s details if found, exception thrown otherwise.
* #throws supplierproject.CustomerNotFoundException
*/
public CustomerDetails findCustomer(String givenID) throws CustomerNotFoundException {
CustomerNotFoundException notFoundMessage
= new CustomerNotFoundException("Customer was not found");
int size = customerCollection.size();
int i = 0;
boolean customerFound = false;
while (!customerFound && i < size) {
customerFound = customerCollection.get(i).getCustomerID().equals(givenID);
i++;
}
if (customerFound) {
return customerCollection.get(i - 1);
} else {
throw notFoundMessage;
}
}
#Override
public String toString() {
StringBuilder customerDets = new StringBuilder();
for (int i = 0; i < numberOfCustomers(); i++) {
customerDets.append(customerCollection.get(i).toString()).append("\n");
}
return customerDets.toString();
}
}
The list itself
Generally, you should test if the new customer is in the list. However, the expResult and result from your test are just the same, because at that point the ees already contains the new customer. Therefore the assertion does not make sense.
However, you can test if the Customer List contains the customer with given email (or some unique property of that customer).

Sorting Arraylist with multiple data types by date (numeric)

This is what I have. Im scanning the txt and passing it to an array as a string (per line). I need to sort by date
package stocktest;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*/
public class StockTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
ArrayList aList = new ArrayList();
java.io.File file = new java.io.File("transactions.txt");
try {
Scanner input = new Scanner(file);
while (input.hasNext()) {
String data = input.nextLine();
aList.add(data);
System.out.println(aList);
}
} catch (FileNotFoundException e) {
System.err.format("File does not exist/n");
}
}
}
Each line in transaction.txt looks like this
buy,1/2/2002,IBM,30,135.00
I need to sort the list by date.
What do you think I should do?
Thanks in advance.
first of all why dont you try to parse each line by "," and put it to a class that will have a states and methods, by that you can use comparator to sort an array list see my example.
private List<OrderDTO> orderDTOs;
public class OrderDtoDateDescComparator implements Comparator<OrderDTO> {
#Override
public int compare(OrderDTO orderDTO, OrderDTO other) {
if (orderDTO.getDateCreated() != null && other.getDateCreated() != null) {
return orderDTO.getDateCreated().compareTo(other.getDateCreated());
} else if (orderDTO.getDateCreated() != null && other.getDateCreated() == null) {
return 1;
} else if (orderDTO.getDateCreated() == null && other.getDateCreated() != null) {
return -1;
}
return 0;
}
}
Collections.sort(this.orderDTOs, new OrderDtoDateDescComparator());
see now that will be easy. as it is.
This is a cool use case for a Comparator.
Collections.sort(aList, new Comparator<String>() {
public int compare(String s1, String s2) {
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy");
try {
Date d1 = format.parse(s1.split(",")[1]);
Date d2 = format.parse(s2.split(",")[1]);
return d1.compareTo(d2);
} catch (ParseException e) {
// handle exception
}
}
});

Change the ratings in this code

In another class im using the setRating to change the ratings of these songs, however I'm not sure what I need to do to this code to be able to change the rating permanently. Thanks in advance.
import java.util.*;
public class LibraryData {
static String playCount() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
static int setRating(int stars) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static class Item {
Item(String n, String a, int r) {
name = n;
artist = a;
rating = r;
}
// instance variables
private String name;
private String artist;
private int rating;
private int playCount;
public String toString() {
return name + " - " + artist;
}
}
// with a Map you use put to insert a key, value pair
// and get(key) to retrieve the value associated with a key
// You don't need to understand how this works!
private static Map<String, Item> library = new TreeMap<String, Item>();
static {
// if you want to have extra library items, put them in here
// use the same style - keys should be 2 digit Strings
library.put("01", new Item("How much is that doggy in the window", "Zee-J", 3));
library.put("02", new Item("Exotic", "Maradonna", 5));
library.put("03", new Item("I'm dreaming of a white Christmas", "Ludwig van Beethoven", 2));
library.put("04", new Item("Pastoral Symphony", "Cayley Minnow", 1));
library.put("05", new Item("Anarchy in the UK", "The Kings Singers", 0));
}
public static String listAll() {
String output = "";
Iterator iterator = library.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Item item = library.get(key);
output += key + " " + item.name + " - " + item.artist + "\n";
}
return output;
}
public static String getName(String key) {
Item item = library.get(key);
if (item == null) {
return null; // null means no such item
} else {
return item.name;
}
}
public static String getArtist(String key) {
Item item = library.get(key);
if (item == null) {
return null; // null means no such item
} else {
return item.artist;
}
}
public static int getRating(String key) {
Item item = library.get(key);
if (item == null) {
return -1; // negative quantity means no such item
} else {
return item.rating;
}
}
public static void setRating(String key, int rating) {
Item item = library.get(key);
if (item != null) {
item.rating = rating;
}
}
public static int getPlayCount(String key) {
Item item = library.get(key);
if (item == null) {
return -1; // negative quantity means no such item
} else {
return item.playCount;
}
}
public static void incrementPlayCount(String key) {
Item item = library.get(key);
if (item != null) {
item.playCount += 1;
}
}
public static void close() {
// Does nothing for this static version.
// Write a statement to close the database when you are using one
}
}
Inside Item, you should write this method:
public static void setRating(int rating0) {
rating = rating0;
}
You should also change your instance variables into static variables by calling them "public static" instead of just "public."

connecting two objects to each other

I've got these four classes.
What I'm trying to do is to add a "Liftcard" to a "User" by using the customerID. this is what I got so far, but it don't seem to work. Im pretty sure the problem is in the public void regLiftCard() method in the window class. Anyone got any idea what i should do?
(Im using arraylist)
User class:
public class User implements Serializable {
private String surename, firstName, gender, age;
private int customerID;
public LiftCard liftCard;
User next;
Same class:
public LiftCard getLiftCard(){
return liftCard;
}
public void setLiftCard(LiftCard liftC){
liftCard = liftC;
}
Window class:
public void regLiftCard()
{
int cardtype = Integer.parseInt(cardTypeField.getText());
int customerID = Integer.parseInt(findCustomerField.getText());
if(cardtype == 1 || cardtype == 2 || cardtype == 3 || customerID != 0)
{
//JOptionPane.showMessageDialog(this, "Du må fylle inn hvilket kort du skal ha 1/2/3");
try
{
User uu = userA.findById(customerID);
if (uu != null) {
if (uu.getLiftCard() != null) {
JOptionPane.showMessageDialog(this, "Kunden har allerede kort");
}
}
String fornavn = firstNameField.getText();
String etternavn = surenameField.getText();
String alder = ageField.getText();
String kjonn = genderField.getText();
LiftCard c = new LiftCard(cardNumber, cardtype);
if (userA.findByCardNumber(cardNumber) == null) {
uu.setLiftCard(c);
} else {
uu = new User(customerID, fornavn, etternavn, alder, kjonn);
}
JOptionPane.showMessageDialog(this, "Nytt kort/kunde er registrert");
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Feil i nummerformat!");
}
}
}
UserArchive class:
public void regLiftCard(User u) {
list.add(u);
}
Liftcard class:
private int cardNumber, cardType;
public LiftCard(int cN, int cT)
{
cardNumber = cN;
cardType = cT;
}
public int getCardNumber(){
return cardNumber;
}
public int getcardType(){
return cardType;
}
public String toString()
{
return cardNumber + "\t" + cardType;
}
}
I suppose you're trying to register a card to a unique User, so on regListCard:
You're looking for a user.
If the user is not null, you look for it's card.
If the card it's not null you're saying that this user already has a card.
Then, you get all the attributes input about the user and the card.
Then, you create a card.
Then, searchs for a user with this card and you set the card you created before to this user.
BUT, there's a possibility that the user you're assigning this card maybe null, because you're looking for a user by it's card, and if it's null, you assign this card to him which is not possible if he is null, and seems that here is the problem.
Then, if there are no user with this card, you are instantiating a new User, but not assigning any card.
So, put this:
else {
uu = new User(customerID, fornavn, etternavn, alder, kjonn);
uu.setLiftCard(c);
}
after this:
if (uu != null) {
if (uu.getLiftCard() != null) {
JOptionPane.showMessageDialog(this, "Kunden har allerede kort");
}
and get the inputs before the "if". Try it and tell us how it worked, and sorry if I do not understood your code.

Categories