I cannot see why this code is giving me the error cannot find method print everything looks okay to me, and I've been over the code checking for spelling errors and tried a different version of it that basically did the same thing but instead of things being called account they were called student. Ignore the comments as I have not changed them
import java.util.*;
public class AccountList
{
private ArrayList < Account > accounts;
/**
* Create a LabClass with no limit on number of enrolments.
* All other details are set to default values.
*/
public AccountList()
{
accounts = new ArrayList < Account >();
}
/**
* Add a account to this LabClass.
*/
public void addAccount(Account newAccount)
{
accounts.add(newAccount);
}
/**
* Return the number of accounts currently enrolled in this LabClass.
*/
public int getNumberOfAccounts()
{
return accounts.size();
}
/**
* Print out a class list with other LabClass
* details to the standard terminal.
*
* Method uses a for .. each loop
*/
public void getAllAccounts()
{
for(Account account : accounts)
{
**account.print();**
}
System.out.println("Number of accounts: " + getNumberOfAccounts());
}
/**
* Print out details of a account
* #param accountEntry The entry in the list
*/
public void getAccount(int accountEntry)
{
if(accountEntry < 0)
{
System.out.println("Negative entry :" + accountEntry);
}
else if(accountEntry < getNumberOfAccounts())
{
Account account = accounts.get(accountEntry);
System.out.print(account);
}
else
{
System.out.println("No such entry :" + accountEntry);
}
}
/**
* removes a account from the list
* #param accountEntry The entry in the list
*/
public void removeAccount(int accountEntry)
{
if(accountEntry < 0)
{
System.out.println("Negative entry :" + accountEntry);
}
else if(accountEntry < getNumberOfAccounts())
{
accounts.remove(accountEntry);
}
else
{
System.out.println("No such entry :" + accountEntry);
}
}
/**
* removes a account from the list
*
* #param aAccount the account to remove
*/
public void removeAccount(Account aAccount)
{
accounts.remove(aAccount);
}
}
You seem to be missing some fundamental concepts here.
First: you have no "default" .print() method; that is, the base, bare-bones Java class Object has no .print() method.
Second: even if it had, what do you expect it to do anyway? Where do you want it to print, what do you want it to print? The first question (where) is answered by classes dedicated to performing output duties (a PrintStream for instance), the second question (what) is answered by implementing your class' .toString().
Since you Account class is obviously not a class dedicated to output duties, you need to do two things:
make your Account class override .toString();
use a dedicated output class to print it; the fastest way to do this is to use System.out, which happens to be a PrintStream, which implements a .print() method.
Seeing your code it appears that you have a .printAccountDetails() method; this contradicts the Law of Demeter for starters; and note how it uses System.out.
Also, the difference between .print() and .println() in a PrintStream (which System.out is) is that a newline will be appended to the output if you use the "ln version"; custom has it that for most text based output channels, this also triggers an output flush from the underlying OS libraries.
Related
I have written a method which creates an input menu for the console, which displays multiple options for the user to choose from and returns the option which the user has chosen. The method accepts an array of objects, and will display them by calling the toString() method on them. The problem is, that in some cases I don't want to call the toString() method on these objects, but maybe the getName() method. Therefore I want to make it possible to pass a method reference, which can be called on the objects and will return a String.
Then I could f.e. pass an array of persons and the getFullName() method. The persons would be displayed with their full name on the console, but I would still get the person object returned, I wouldn't have to find the person object by its full name.
Here is my current code for the method:
/**
* Prints the provided question and the options to choose from
*
* #param question
* the question to ask the user
* #param options
* list of objects the user can choose from
* #return chosen object
*/
public Object getMultipleChoiceResult(String question, List<?> options) {
int result = 0;
while (result > options.size() | result < 1) {
System.out.println(question);
for (int i = 1; i <= options.size(); i++) {
System.out.println("(" + i + ") " + options.get(i - 1).toString());
}
try{
result = scanner.nextInt();
} catch (InputMismatchException e) {
System.err.println("wrong input");
scanner.next();
}
}
return options.get(result - 1);
}
Do you understand what I am looking for and is it possible?
I think you're looking for this:
public <T> T getMultipleChoiceResult(String question, List<T> options, Function<T, String> toString) {
// ...
System.out.println("(" + i + ") " + toString.apply(options.get(i - 1)));
// ...
}
In your example, you can call it like this:
Object result = getMultipleChoiceResult(question, options, Object::toString);
Or you can pass a list of Person and print Person.getFullName():
Person result = getMultipleChoiceResult(question, persons, Person::getFullName);
There are many ways of achieving this. A relatively modern approach is to pass a method reference:
private static <T> void showList(List<T> list, Function<T,String> f) {
for (T t : list) {
System.out.println(f.apply(t));
}
}
The call to this method would look as follows:
showList(myList, MyType::getFullName);
The above assumes that myList is a List<MyType>, and MyType has a non-static method getFullName() returning a String.
Demo.
Scenario:
I've two reports: Main Report (let's call it, A) and sub-report (let's call it, B).
Report A contains sub-report B at the detail band, so sub-report B is displayed for each element at the Report A datasource. Sub-report B also returns a variable to the Main report A.
What I want is to sum those return values from sub-report B and totalize them at the Main report summary.
To do that, I have tried to create a new report variable that sum those returns values... Something like this:
However, I've found that such variables expression are always evaluated before the band detail is rendered, so I always miss the first sub-report return value...
Sadly, the evaluation time (as this link says) cannot be changed on those kind of variables, so I'm stuck...
After been struggling with this for some hours... and searching the internet for a solution... I came with a Workaround (the enlightening forums were these ones: one and two).
First, you need to define a java Class Helper that allows you calculate some arithmetic operation, in my case a Sum operation. I defined these classes:
package reports.utils;
import java.util.Map;
/**
* Utility that allows you to sum Integer values.
*/
public class SumCalculator {
/**
* Stores a map of {#code SumCalculator} instances (A Map instance per thread).
*/
private static final ThreadLocalMap<String, SumCalculator> calculatorsIndex = new ThreadLocalMap<>();
/**
* The sum total.
*/
private int total = 0;
/**
* No arguments class constructor.
*/
private SumCalculator() {
super();
}
/**
* Instance a new {#code SumCalculator} with the given ID.
*
* #param id {#code SumCalculator}'s ID
* #return the new {#code SumCalculator} instance
*/
public static SumCalculator get(String id) {
Map<String, SumCalculator> map = calculatorsIndex.get();
SumCalculator calculator = map.get(id);
if (calculator == null) {
calculator = new SumCalculator();
map.put(id, calculator);
}
return calculator;
}
/**
* Destroy the {#code SumCalculator} associated to the given ID.
*
* #param id {#code SumCalculator}'s ID
* #return {#code null}
*/
public static String destroy(String id) {
Map<String, SumCalculator> map;
map = calculatorsIndex.get();
map.remove(id);
if (map.isEmpty()) {
calculatorsIndex.remove();
}
return null;
}
/**
* Resets the {#code SumCalculator} total.
*
* #return {#code null}
*/
public String reset() {
total = 0;
return null;
}
/**
* Adds the given integer value to the accumulated total.
*
* #param i an integer value (can be null)
* #return {#code null}
*/
public String add(Integer i) {
this.total += (i != null) ? i.intValue() : 0;
return null;
}
/**
* Return the accumulated total.
*
* #return an Integer value (won't be null, never!)
*/
public Integer getTotal() {
return this.total;
}
}
package reports.utils;
import java.util.HashMap;
import java.util.Map;
/**
* Thread Local variable that holds a {#code java.util.Map}.
*/
class ThreadLocalMap<K, V> extends ThreadLocal<Map<K, V>> {
/**
* Class Constructor.
*/
public ThreadLocalMap() {
super();
}
/* (non-Javadoc)
* #see java.lang.ThreadLocal#initialValue()
*/
#Override
protected Map<K, V> initialValue() {
return new HashMap<>();
}
}
Second, at your jasper report, you need to define four text fields:
1) A text field that iniatializes your calculator; it should be (ideally) at the title section of the report and should have an expression like this: SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").reset(). This text field should have the evaluation time: NOW.
2) A text field that calls the increment function (i.e. SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").add($V{SUB_REPORT_RETURN_VALUE}). This text field will reside at your detail band, after the subreport element; and it should have the evaluation time: BAND (this is very important!!)
3) A text field that prints the calculator total. This text field will reside at your summary band, it will evaluate to NOW. Its expression will be: SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").getTotal()
4) A text field that destroy the calculator. This text field will also reside at your summary band and must appear after the text field 3. The text field should have an expression like: SumCalculator.destroy("$V{SUB_REPORT_RETURN_VALUE}"). This text field should have the evaluation time: NOW.
Also, the text fields: 1, 2, and 4, should have the attribute "Blank when Null", so they will never be printed (that's why those java operations always return null).
And That's it. Then, your report can look something like this:
if i understand the problem, you can not summarize the amount returned by the sub report in the main report, i had the same problem and i solved in this way.
1.- Create a class which extends from net.sf.jasperreports.engine.JRDefaultScriptlet. and override the method beforeReportInit()
this is the code from this class.
package com.mem.utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
public class SumarizacionSubtotales extends JRDefaultScriptlet {
private final Log log = LogFactory.getLog(getClass());
private Double total;
public Double getTotal() {
return total;
}
public Double add(Double cantidad) {
if(log.isDebugEnabled())log.debug("AGREGANDO LA CANTIDAD : " + cantidad);
this.total += cantidad;
return cantidad;
}
#Override
public void beforeReportInit() throws JRScriptletException {
if(log.isDebugEnabled())log.debug("beforeReportInit");
total = 0.0D;
}
}
2.- add your project's jar in your ireport's classpath.
3.- Replace the class of the REPORT scriptlet.
in the properties with your class.
3.- add in the group footer where you want to print the value returned by the sub-report a textfield with the following expression.
$P{REPORT_SCRIPTLET}.add( $V{sum_detalles} )
In this case $V{sum_detalles} is a variable in the main report which contains the value returned by the sub-report.
4.- Add in the Last page footer another textfield with the following expression.
$P{REPORT_SCRIPTLET}.getTotal()
I have the following code. However I am doubting about if it is the right way to implement it or not.
What I mean is: in the collection framework there are many data structures to use and creating the class (MemberList) to manage the aggregations of many members can be implemented using ArrayList, LinkedList, priority queue ...
I would like to use a data structure that fits with my needs, and that has the least Big O possible when it comes to searching, sorting, deleting, adding, modifying and deleting.
public class MemberList{
/**
*a list of accounts existing in the database
*/
private static List<Member> members = new ArrayList<Member>();
/**
* add a member to our member list
* #param m the member to be added
*/
public static void Add(Member m)
{
members.add(m);
/**
* delete a member from our member list
* #param m the member to be deleted
*/
public static void Delete(Member m)
{
Iterator<Member> it = members.iterator();
while(it.hasNext())
{
if(m.equals(it.next()))
{
it.remove();
}
}
}
/**
* Search for a specific member in the member list
* #param m the member that needs to be found
* #return the reference of the object Member
* #throws UserNotFoundExeption whether the member was not found in the list
*/
public static Member Search (Member m) throws UserNotFoundExeption
{
Iterator<Member> it = members.iterator();
while(it.hasNext())
{
if(m.equals(it.next()))
{
return it.next();
}else{
UserNotFoundExeption ex = new UserNotFoundExeption(it.next().getUsername());
throw ex;
}
}
return null;
}
/**
* The login method enables checking whether the login was made successfully or not. if not, it can throw two
* exceptions to handle the errors
* #param member
* #return
* #throws UserNotFoundExeption
* #throws FailedLoginException
*/
public static boolean login (Member m)
throws UserNotFoundExeption,FailedLoginException {
try{
Member member = Search(m);
if (!m.authenticate(member.getPassword()))
{
FailedLoginException ex2 = new FailedLoginException (member.getPassword());
throw ex2;
}
else
{
return true;
}
}catch(UserNotFoundExeption ex){
throw ex;
}
}
/**
* this behavior modify attributes of the corresponding class
* #param <T> this generic helps to accept any type of parameter change, hence we can change any type
* #param m this is the member that need to change his information
* #param choice the choice of which information to change
* #param change the new change on the member attribute
* #throws UserNotFoundExeption
*/
public static <T> void Modify(Member m, int choice, T change) throws UserNotFoundExeption
{
try{
Member member = Search(m);
switch(choice)
{
case 1:
member.setUsername((String)change);
break;
case 2:
member.setPassword((String)change);
break;
case 3:
member.setCommunity((Community)change);
break;
}
}catch(UserNotFoundExeption ex){
throw ex;
}
}
/**
* display the member list objects information
*/
public static void Display()
{
Iterator<Member> it = members.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
/**
* Sort objects in the list
*/
public static void Sort()
{
Iterator<Member> it = members.iterator();
Member[] Members_Array = members.toArray(new Member[members.size()]);
Member temp;
for(int i = 0; i<members.size(); i++)
{
for(int j = 0; j < members.size() - (i+1); j++)
{
if(Members_Array[j].compareTo(Members_Array[j+1]) > 0)
{
temp = Members_Array[j];
Members_Array[j] = Members_Array[j+1];
Members_Array[j+1] = temp;
}
}
}
}
}
Thank you!
This question is too broad, and "the right way to use collections in Java" is also a philosophical question, so it cannot be scientifically answered.
Specifically to your case, depending on the pool of your members, you probably don't want to iterate over them when you need to pull a member out. I would recommend you use something like a HashMap<String,Member>, where each member has an identifiable unique key (a username for instance). This will grant you O(1) access speed and allow you to iterate when you need it using .values().
You can use a HashMap like so:
// This is how you create a hash map:
HashMap<String,Member> members = new HashMap<String,Member>();
// This is how you add an object to it. It is slower than lists,
// but since reading happens far often, it pays off.
members.put("ben", new Member());
// This is how you access an object in the hash map.
// Accessing a hash map is O(1).
Member member = members.get("ben");
// This is how you remove an object from the hash map.
// Removing an object is also O(1)
members.remove("ben");
// Hash maps are also iterable
for(Member member : members.values()) {
}
I would use array list if you dont want to use JDBC.
But later if your project going to growe, you have to use JDBC.
I've been working at this for a couple hours now and I feel (I hope) I'm right on the verge of figuring it out. This program reads in a bunch of values from an external file and places them in an array of objects which seems to be working just fine.
The Objects properties are:
Bank Account #
Customer Name
Bank Account Balance
1. I can output them in order of Account # (That's how their read in from the file, no sorting is necessary)
2. I've setup a method from implementing Comparable to sort by Bank Account Balance and it's working fine.
3. I need a second sort method, to sort by Customer Name.
- The problem I'm having with this is based on the research I've done and what I've tried I've come to the conclusion that the only way to make this work will be to build my own Comparable Objects (sorry if my terminology is skewed.) I've attempted this as well multiple times with both Java Doc and some similar questions on SE.
When all is said and done I'm going to throw some Listeners into my checkbox group to allow the user to toggle the different sort methods.
Here's the chunks i'm working on:
public class bankAccounts implements Comparable<bankAccounts> {
/* PRIVATE FIELDS HERE, FOLLOWED BY TYPICAL GET AND SET METHODS */
/*SORTS BY ACCOUNT BALANCE WORKING GREAT*/
public int compareTo(bankAccounts b) {
if (accountBalance < b.accountBalance)
{
return -1;
}
if (accountBalance > b.accountBalance) {
return 1;
}
return 0;
}
/* BEGIN SNIPPET OF MAIN CLASS */
/*METHOD I CALL FROM MAIN CLASS, SORTS BY BALANCE ^^ AS SEEN ABOVE */
Arrays.sort(retrievedAccounts);
for (int i=0; i<retrievedAccounts.length; i++) {
String resultFull = Integer.toString(retrievedAccounts[i].getAccountNumber()) + retrievedAccounts[i].getAccountLastName() + Double.toString(retrievedAccounts[i].getAccountBalance());
box.append(resultFull + "\n");
}
/* NORMAL METHOD WHICH OUTPUTS IN ORDER OF ACCOUNT NUMBER, NO SORTING HAPPENING HERE */
for(int x = 0; x < retrievedAccounts.length; ++x)
{
String resultFull=Integer.toString(retrievedAccounts[x].getAccountNumber()) + retrievedAccounts[x].getAccountLastName() + Double.toString(retrievedAccounts[x].getAccountBalance());
box.append("\n\n\n" + resultFull + "\n\n");
}
I'm hoping someone will have some insight towards a next step which might allow me to finish this up. If you have suggestions to take this a completely different direction I'm open to that as well.
This is an idea haven't tested.
Create a another private method to store compareType
public class bankAccounts implements Comparable<bankAccounts> {
private int compareType = 0; // 0 - compare by balance 1-compare by name
In your compare method
public int compareTo(bankAccounts b) {
if(this.compareType == 0){
if (accountBalance < b.accountBalance)
{
return -1;
}
if (accountBalance > b.accountBalance) {
return 1;
}
return 0;
}else{
return customerName.compareTo(b.customerName)
}
Use an implementation of Comparator<bankAccounts> that compares the names of your objects and pass that into the Arrays.sort() method.
Use an anonymous class like this:
Arrays.sort(retrievedAccounts, new Comparator<bankAccounts>() {
public int compare(bankAccounts a, bankAccounts b) {
return a.getName().compareTo(b.getName());
}
});
This code assumes you have a getter method on bankAccounts for customer name called getName()
You would do well to follow java naming conventions:
class names start with a capital letter
class names are singular, not plurals
Implement a method
public void search (String searchString) { }
to iterate through the notes ArrayList until it
finds a note that contains the searchString.
It should then print either the item found or
the message "String not found".
So far, I have:
import java.util.ArrayList;
import java.util.Iterator;
/**
* A class to maintain an arbitrarily long list of notes.
* Notes are numbered for external reference by a human user.
* In this version, note numbers start at 0.
*
* #author David J. Barnes and Michael Kolling.
* #version 2008.03.30
*/
public class Notebook
{
// Storage for an arbitrary number of notes.
private ArrayList<String> notes;
/**
* Perform any initialization that is required for the
* notebook.
*/
public Notebook()
{
notes = new ArrayList<String>();
}
/**
* Store a new note into the notebook.
* #param note The note to be stored.
*/
public void storeNote(String note)
{
notes.add(note);
}
/**
* #return The number of notes currently in the notebook.
*/
public int numberOfNotes()
{
return notes.size();
}
/**
* Show a note.
* #param noteNumber The number of the note to be shown.
*/
public void showNote(int noteNumber)
{
if(noteNumber < 0) {
// This is not a valid note number, so do nothing.
System.out.println("invalid index given");
}
else if(noteNumber < numberOfNotes()) {
// This is a valid note number, so we can print it.
System.out.println(notes.get(noteNumber));
}
else {
System.out.println("there are fewer items in the notebook");
// This is not a valid note number, so do nothing.
}
}
public void removeNote(int noteNumber)
{
if(noteNumber < 0) {
// This is not a valid note number, so do nothing.
System.out.println("invalid index given");
}
else if(noteNumber < numberOfNotes()) {
// This is a valid note number.
notes.remove(noteNumber);
}
else {
System.out.println("there are fewer items in the notebook");
// This is not a valid note number, so do nothing.
}
}
/* Edit note.
* I tried to improve the formatting of the code below, but I'm completely
* unable to figure out how on earth anything of that should make sense
* and therefore the indentation is completely without any meaning.
*/
public void search (String searchString)
{
for each notes in ArrayList {
if notes = searchString;
System.out.println("String found"); + searchString
return end
}
if}
System.out.println("String not found");
}
}
But it is not working, and I am not able to work it out.
Several problems:
Your search method is actually outside the class.
The body of your search method makes no sense at all.
If you're counting on the monkeys writing Shakespeare, you'll be waiting a while.
To iterate over the array list, you can use a 'for-each' loop:
for (String note: notes) {
// Do something with note
}
This is very basic syntax. Have you seen it before? If not, you should start by reading a very basic tutorial to Java before attempting this homework.
Fundamentally you need to look at each item in your ArrayList and test to see if it matches the search condition. In Pseudocode
for each note in notes
{
if note equals searchString then
print "Found " + searchString
return
end if
}
print "not found"
Given that basic outline, want to take a second stab at coding it in Java?
Basically you want to loop over the elements, and for each one check whether it equals the element you are searching for. You can use a for loop or a foreach loop to do the actual iteration.
should you be checking if the whole note matches your searchstring, or if the note contains your searchstring?
i.e. given notes "foobar","baz","spam", should a search on "foo" return "foobar" or not match on anything?
so in pseudocode:
for each note in notes
{
if searchstring in note
{
print "Found :"+note
}
}
check this website http://pleac.sourceforge.net/pleac_java/arrays.html it may be useful