Solved

invoice code written in java

Posted on 2003-11-10
5
447 Views
Last Modified: 2006-11-17
I have to write a program that displays an invoice of several items such as item name, item id number, quantity, proce, and total cost on each line for the quantity and item cost. I have to use two classes, the first class Inv contains the item data and methods to get and set the item name, quantity, and price. The other class created objects for the items and uses the objects to call the set and get methods. Below is what I have so far, I'm not sure what to do next, I'm a beginner at this.
public class Invoice
{
      private int itemIdNumber;
      private String itemName;
      private int itemQuantity;
      private float itemPrice;
      private float itemTotalCost;
      
      public int getitemIdNumber ( )
      {
            return itemIdNumber;
      }
      public void setItemIdNum (int num)
      {
            itemIdNumber = num;
      }
      public StringgetName ( )
      {
            return itemName;
      }
      public void setName (String name)
      {
            itemName=name;
      }
      public int getQuantity ( )
      {      
            return itemQuantity;
      }
      public void setQuantity (int qua)
      {
            itemQuantity = qua;
      }
      public float getPrice ( )
      {
            return itemPrice;
      }
      public void setPrice (float pri)
      {
            itemPrice = pri;
      }
      public float getTotalCost ( )
      {
            return itemTotalCost;
      }
      public void setTotalCost (float tot)
      {
            itemTotalCost=tot;
      }
}
0
Comment
Question by:dam5800
  • 4
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9716481
That's OK. What you should do, with most classes anyway actually, is override the toString method of your class:

public String toString() {
   return "Name=" + name;
}

and put the other variables in
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9716500
That should be 'itemName' of course.

Your 'controlling' class can then say

System.out.println(myInvoice);

where 'myInvoice' is a variable of type Invoice
0
 
LVL 2

Expert Comment

by:Dannin
ID: 9716978
Honestly I think an invoice and item should be 2 different classes since 1 invoice will have or could possibly have more then 1 item in it. or an inner class that more up to you. For handeling it I suggest you with another class as u say what you can do it make a small text menu with serveral options depending on the option selected for example:


1: Display an Item/Invoice
2: Buy(Set) and item or make a new purchase
3: Quit this menu

I'll select option 2 and in doing this I'll call the set methods and att info to it. After your done u can redisplay the menu and make another selection for example option 1 which will display the info your inputed last. Hope this is a good guide line=)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9717043
In the real world, this sort of thing is reasonably complex as there are one-to-many realtionships involved. But keep it simple for now - you can extend it later maybe.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9718212
:-)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question