How do I use compareTo

Please help. I'm new to this and I can't figure out how to use compareTo. I need to try to get the output looking like this:
name1 bill1 bill2
name2 bill1 bill2
name3 bill1 bill2
etc.
It currently does this:
name1 bill1
name2 bill1
name1 bill2
etc.
How do I do this????
public class BillingList
private static final int MAXIMUM_NUMBER_OF_BILLS = 100;
private Bill[] bills;
private int numberOfBills;
public BillingList()
bills = new Bill[MAXIMUM_NUMBER_OF_BILLS];
numberOfBills = 0;
public void addBill(String customerName, double amountOwedByCustomer)
bills[numberOfBills] = new Bill(customerName, amountOwedByCustomer);
++numberOfBills;
public void payBill(String customerName, double amountPaid)
if(numberOfBills > 1)
     { for(int i = 0; i < numberOfBills; ++i)
          {if(bills[i].getAmount() >= amountPaid)
                    {double amountChanged = bills[i].getAmount() - amountPaid;
                         bills.setAmount(amountChanged);
public double totalOwed(String customerName)
double total = 0.00;
for (int i = 0; i < numberOfBills; ++i)
total += bills[i].getAmount();
return total;
public String toString()
String result = "";
for (int i = 0; i < numberOfBills; i++)
result += bills[i].getName() + ": " + bills[i].getAmount() +"\n";
return result;
public static void main(String[] args)
BillingList accounts = new BillingList();
accounts.addBill("Joe", 123.45);
accounts.addBill("Mary", 321.11);
accounts.addBill("Charlie", 420.15);
accounts.addBill("Laura", 124.31);
accounts.addBill("Joe", 23.55);
accounts.addBill("Mary", 122.21);
accounts.addBill("Charlie", 125.78);
accounts.addBill("Jack", 76.55);
accounts.addBill("Mary", 122.34);
accounts.addBill("Charlie", 322.11);
System.out.println("Charlie's total is " +
accounts.totalOwed("Charlie"));
accounts.payBill("Charlie", 120.00);
System.out.println("Charlie paid 120.00");
System.out.println("Charlie's total is now " + accounts.totalOwed("Charlie"));
System.out.println(accounts);
public class Bill implements Comparable
private String name;
private double amount;
public Bill(String nameOnBill, double amountOwed)
name = nameOnBill;
amount = amountOwed;
public int compareTo(Object other)
Bill otherBill = (Bill)other;
return name.compareTo(otherBill.name);
public String getName()
return name;
public double getAmount()
return amount;
public void setAmount(double newAmount)
amount = newAmount;
steve

Try this code, no compareTo.import java.util.*;
public class BillingList {
     private static final int MAXIMUM_NUMBER_OF_BILLS = 100;
     private Bill[] bills;
     private int numberOfBills;
     private Hashtable customers;
     public BillingList() {
          bills = new Bill[MAXIMUM_NUMBER_OF_BILLS];
          customers = new Hashtable();
          numberOfBills = 0;
     public void addBill(String customerName, double amountOwedByCustomer) {
          bills[numberOfBills] = new Bill(customerName, amountOwedByCustomer);
          ++numberOfBills;
          if(!customers.containsKey(customerName)) customers.put(customerName,customerName);
     public void payBill(String customerName, double amountPaid) {
          boolean found=false;
          for(int i = 0; i < numberOfBills; i++) {
               if(bills[ i ].getName().equalsIgnoreCase(customerName)) {
                    if(bills[ i ].getAmount() >= amountPaid) {
                         double amountChanged = bills[ i ].getAmount() - amountPaid;
                         bills[ i ].setAmount(amountChanged);
                         found=true;
          if(!found) System.out.println("Nothing to pay the customer "+customerName);
     public double totalOwed(String customerName) {
          double total = 0.00;
          for (int i = 0; i < numberOfBills; ++i) {
               if(bills[ i ].getName().equalsIgnoreCase(customerName))
                    total += bills[ i ].getAmount();
          return total;
     public String toString() {
          Enumeration e = customers.elements();
          String result="";
          while(e.hasMoreElements()) {
               String customer = (String)e.nextElement();
               result += customer+" : ";
               for (int i = 0; i < numberOfBills; i++) {
                    if(bills[ i ].getName().equalsIgnoreCase(customer)) {
                         result += bills[ i ].getAmount() +" ";
               result += "\n";
          return result;
     public static void main(String[] args) {
          BillingList accounts = new BillingList();
          accounts.addBill("Joe", 123.45);
          accounts.addBill("Mary", 321.11);
          accounts.addBill("Charlie", 420.15);
          accounts.addBill("Laura", 124.31);
          accounts.addBill("Joe", 23.55);
          accounts.addBill("Mary", 122.21);
          accounts.addBill("Charlie", 125.78);
          accounts.addBill("Jack", 76.55);
          accounts.addBill("Mary", 122.34);
          accounts.addBill("Charlie", 322.11);
          System.out.println("Charlie's total is " +
          accounts.totalOwed("Charlie"));
          accounts.payBill("Charlie", 120.00);
          System.out.println("Charlie paid 120.00");
          System.out.println("Charlie's total is now " + accounts.totalOwed("Charlie"));
          System.out.println(accounts);
}Sudha

Similar Messages

  • Question using compareTo ...

    Alright I have a general question on using the compareTo method...
    I was to add two different polynomial terms together, but in order to do this I need to compare the degrees of both degree inputs together. If they are the same I combine the coefficients...
    I am wondering how I go about using the compareTo method to do such a thing. The following is the code I have so far on the program. I DON'T want an answer! Really I just want to know if I am on the right track. Also notice the what I have commented out. It's lengthy, and I'm unsure if that is the right way to go about it.... thanks for the help!
    import java.util.*;
    import java.lang.*;
    public class PolyTest
    public static void main(String[] args)
         Scanner stdin = new Scanner(System.in);
         Polynomial test = new Polynomial();
    Polynomial polynomial1 = new Polynomial();
    System.out.println("Enter degree of the first polynomial");
    int degree1 = stdin.nextInt();
              for(int i = degree1; i > -1; i--)
         System.out.println("Enter a coefficient for x^ " + i);
         int coefficient1 = stdin.nextInt();
         if (coefficient1 != 0)
         polynomial1.addTerm(coefficient1, i);
         while(i == 0)
    System.out.println("The first polynomial is " + polynomial1);
              break;
              Polynomial polynomial2 = new Polynomial();
              System.out.println("Enter degree of the second polynomial");
    int degree2 = stdin.nextInt();
              for(int i = degree2; i > -1; i--)
         System.out.println("Enter a coefficient for x^ " + i);
         int coefficient2 = stdin.nextInt();
         if (coefficient2 != 0)
         polynomial2.addTerm(coefficient2, i);
         while(i == 0)
    System.out.println("The second polynomial is " + polynomial2);
              break;
              // System.out.println("The sum of the two polynomials is " + test.comparePoly(degree1, degree2, coefficient1, coefficient2));
    class Polynomial
    private LinkedList<Term> terms;
    public Polynomial()
    terms = new LinkedList<Term>();
    public void addTerm( int c, int d)
    terms.add(new Term(c, d));
    public String toString()
    String s = "";
    for(int i = 0; i < terms.size(); i++)
    s += terms.get(i);
    if(i < terms.size()-1)
    s += " + ";
    return s;
    class Term implements Comparable
    private int coeff;
    private int degree;
    String variable = "x";
    public Term(int c, int d)
    coeff = c;
    degree = d;
    public int compareTo(Object obj)
    Term temp = (Term) obj;
         int diff = degree - temp.degree;
         return diff;
    /* public String comparePoly(int de1, int de2, int co1, int co2)
    int x = de1.compareTo(de2);
    int y = 0;
    String str = String.valueOf();
              if (x == 0)
              y = co1 + co2;
                   if (de1 == 1 || de2 == 1)
                   str += y + variable;
                   else if (de1 == 0 || de2 == 0)
              str += "";
                   str += y + variable + "^" + de1;
              else if (x < 0)
              if (de1 == 1 || de2 == 1)
                   str += y + variable;
                   else if (de1 == 0 || de2 == 0)
              str += "";
                   str += co2 + variable + "^" + de2 + " + " + co1 + variable + "^" + de1;
              else
              if (de1 == 1 || de2 == 1)
                   str += y + variable;
                   else if (de1 == 0 || de2 == 0)
              str += "";
              str += co1 + variable + "^" + de1 + " + " + co2 + variable + "^" + de2;
              return str;
    public String toString()
    String s = String.valueOf(coeff);
              if (degree == 0)
              s += "";
              else
              if (degree == 1)
              s += variable;
              else
              s += variable + "^" + degree;
    return s;
    }

    Are you in the same class with this guy?
    http://forum.java.sun.com/thread.jspa?threadID=667690
    As for whether you're on the right track, I don't know, because I don't know what comparePoly is supposed to do or how you're supposed to use compareTo, and because your code isn't formatted. (When you post code, please use [code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read)
    But then, you can find out for yourself if you're on the right track. If your code does what it's supposed to do, or close to it, for a test case or two, then yeah, you're on the right track.

  • How do I use Edge Web Fonts with Muse?

    How do I use Edge Web Fonts with Muse - is it an update to load, a stand alone, how does it interface with Muse? I've updated to CC but have no info on this.

    Hello,
    Is there a reason why you want to use Edge Web Fonts with Adobe Muse?
    Assuming you wish to improve typography of your web pages, you should know that Muse is fully integrated with Typekit. This allows you to access and apply over 500 web fonts from within Muse. Here's how you do it:
    Select a text component within Muse, and click the Text drop-down.
    Select Add Web Fonts option, to pop-open the Add Web Fonts dialog.
    Browse and apply fonts per your design needs.
    Muse also allows you to create paragraph styles that you can save and apply to chunks of text, a la InDesign. Watch this video for more information: http://tv.adobe.com/watch/muse-feature-tour/using-typekit-with-adobe-muse/
    Also take a look at these help files to see if they help you:
    http://helpx.adobe.com/muse/tutorials/typography-muse-part-1.html
    http://helpx.adobe.com/muse/tutorials/typography-muse-part-2.html
    http://helpx.adobe.com/muse/tutorials/typography-muse-part-3.html
    Hope this helps!
    Regards,
    Suhas Yogin

  • How do I use Panorama / Tab Groups with the keyboard?

    Yes I know that CTRL-SHIFT-E opens the panorama window, and then I can use the mouse to organize my tabs into groups.
    But how do I do this with the keyboard?
    I've [http://lifehacker.com/#!5719596/switch-between-tab-groups-in-firefox-with-a-keyboard-shortcut read] [http://ubuntuforums.org/showthread.php?t=1705714 that] Ctrl-`should move me through tab groups. That doesn't work for me on my Danish keyboard. (Where the ' and ` chars are weird. But is how they are on a valid standard Danish keyboard) I've tried changing the keyboard to USA and then moving through tab groups works fine.
    In short: Pretend I don't have a mouse. How do I use Panorama / Tab Groups?

    Sorry. These are both known bugs:
    [https://bugzilla.mozilla.org/show_bug.cgi?id=587010 Bug 587010] - Add keyboard UI for manipulated groups and tabs in Tab Candy
    and
    [https://bugzilla.mozilla.org/show_bug.cgi?id=626594 Bug 626594] - shortcut for switching tab groups is badly accessible on many non-US keyboard layouts

  • How do I use a custom UserInfo with a T3Client?

    This is related to my previous post, 4655. The problem is I if
    use a custom implementation of the interface
    weblogic.security.acl.UserInfo in a T3Client constructor, the
    client fails to connect. If I use
    weblogic.security.acl.DefaultUserInfoImpl, the client connects
    and disconnects without any problems. How do I use a custom
    UserInfo implementation with a T3Client?

    In don't know about the Gmail part, but for the Hotmail, try this link.
    Here's another link from a different source.

  • How do I use HP iPAQ H4300 WITH Windows 7?

    I recently upgraded from Windows XP to Windows 7.  How do I use my HP iPAQ h4300 pocket PC with this recently installed Operating System?
    Ed Walsh [Personal Information Removed]

    Hello edwalsh606,
    I understand you are seeking assistance with the iPAQ H4300 and your new Windows 7. I am sorry, but to get your issue more exposure, I would suggest posting it in the commercial forums, since this is a commercial product. You can do this at Notebook - HP ProBook, EliteBook, Compaq, Slate/Tablet PC, Armada, LTE.
    I hope this helps. Thank you for posting on the HP Forums. Have a great day!
    Please click the "Thumbs Up" on the bottom right of this post to say thank you if you appreciate the support I provide!
    Also be sure to mark my post as “Accept as Solution" if you feel my post solved your issue, it will help others who face the same challenge find the same solution.
    Dunidar
    I work on behalf of HP
    Find out a bit more about me by checking out my profile!
    "Customers don’t expect you to be perfect. They do expect you to fix things when they go wrong." ~ Donald Porter

  • How do I use my Time Capsule with hotel wired internet

    How do I use my Time Capsule as a wireless router on my hotel free internet ethernet cable?

    It may not be possible..
    If they use a web access page, then there is no way you can set the TC to authenticate.. you must use the computer to authenticate and then use the computer to bridge to the TC for wireless clients to access.
    Look up instructions on using ICS (internet connection sharing) in the Mac.
    eg for 10.6.8 http://support.apple.com/kb/PH6589
    eg http://support.apple.com/kb/PH10740
    Of course if there is no login then you could just use it.. but I doubt you would be asking if it just worked.

  • How do i use my own image with keying (or green screen)

    how do i use my own image witrh keying (or green screen)

    Put your sky clip on a layer below the clip you want to key. One or the other will be a connected clip. Apply  the Keying filter to foreground clip (the one with the green screen). The green will be seen as transparent and the sky will show through as the BG. Adjust as necessary in the Inspector.
    Good luck.
    Russ

  • How do I use ipod 5th classic with new itunes

    how do I use my old ipod classic 5th generation with the latest itunes software?

    The same way as before...
    If the new look of iTunes is confusing, it may look more familiar to you if you show the sidebar.  It is hidden by default to streamline the look and feel.
    From the iTunes menu bar, under View, select Show Sidebar.  With the sidebar not hidden, iTunes mostly works like it did before.

  • How do I use my airport extreme with my FIOS router?

    How do I use my AirPort Extreme base station with my FIOS Router to extend my network?  I have hard disks connected to my AirPort Extreme and would lik to access them.

    Probably can't answer all your questions - but.... I use a Linksys (wired and wireless) router as my primary entry point for FIOS. I use a Time Capsule and an Airport Express as a common wireless connection. So I have two visibile wireless networks and use them both depending on where I am in the house. Both the TC and AEx can be seen either wirelessly or wired from the entire network. Note - the Linksys provides all the DHCP - you set the apple routers as "bridge mode."

  • How do I use Qt and OpenGL with Visual Studio

    Hi! I mainly want to program in C++ and I want to use Qt and OpenGL with Visual Studio.
    I am currently revising C++ and later on i am going to start reading Qt and OpenGL. I have a background of
    Embedded firmware design(C and Assembly).
    The Visual Studio Version I have is 2013 ultimate. How do I use Qt and OpenGL with Visual Studio?
    Thanks
    Alexandros

    Hi ClassicalGuitar,
    The forum supports VS setup and installation. And your issue is not about the forum. I will move the thread to off-topic forum. Thanks for your understanding.
    Regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

  • How do i use my mac book with my HP 6510 e all in one series printer

    how do i use my mac book with HP photosmart 6510 e all in one series

    Hello Marilynatsea,
    Thanks for using Apple Support Communities.
    To learn more about printing in OS X on your Mac, and how to add a printer, take a look at the article linked to below.
    Mac Basics: Printing in OS X - Apple Support
    Cheers,
    Alex H.

  • How do I use my gigaware webcam with my mac?

    How do I use my gigaware webcam with my mac? I just bought this webcam on amazon http://www.amazon.com/gp/product/B00IJE0ISS/ref=oh_aui_detailpage_o07_s00?ie=UTF 8&psc=1 and I want to use it with my mac. Is there an adapter or any software to make it compatible? Thanks.

    Your amazon link states, in part, that this webcam is:
    Compatible with Microsoft Windows 7,Vista, XP, 2000
    Use a Mac OS X Compatible Webcam like one of those shown in the linked article instead.
    For best compatibility and security, also update your OS X Mavericks (10.9.2) to the current OS X version by restarting Mac and then downloading and installing OS X Mavericks 10.9.4 Update (Combo).

  • How do I use a foot pedal with my mac?

    how do I use a foot pedal with my mac to use on YouTube?

    Can you be more specific? What kind of foot pedal? What is the foot pedal used for?

  • How do i use a new computer with my ipod?

    how do i use a new computer with my ipod?

    See this apple support communities user tip…
    Syncing to a "New" Computer or replacing a "crashed" Hard Drive

Maybe you are looking for

  • Macbook Pro not seeing my surround sound interface

    I use a PreSonus Firebox interface to output surround sound from Logic 8. It worked fine on my previous computer, a standard MacBook. I recently upgraded to a MacBook Pro and it won't show up in Logic or the computer's own System Preferences. I never

  • Problem with SAP 7.10

    I've a problem with download to xlsf rom ALV GRID with SAP 7.10 Can I execute a patch download for resolve thi problem? Where I can find it? Best regards. Cristiano

  • Display Current Quarter and Previous Quarter???

    Hi Gurus, I Had a requirement to display only the current Quarter and previous quarter in the report level using single quarter column. and my quarter value type is Q1 2013. Please anyone help me out on this asap/ Thanks.

  • Problem viewing .mid files in Score Editor

    Help please! Used Finale several years ago before I upgraded to OS 10 and Logic 7.2 Express. I recently opened one of my .mid files from Finale and it played back fine but when I tried to see the score in the Score Editor it was altered and only some

  • Deactivate Final Cut Studio

    Hello, I've recently purchased a new MBP. I plan to run FCS, and other apps on this new computer, restore the previous one to it's original state, and hand it down to my niece. The question I have is how do you deactivate FCS on the previous computer