• 0

Stuck in a Java question


Question

Hey,

So,I'm a newbie to Java,I have an assignment and I'm stuck,I know online there's a lot of paid services who'd solve this for me,I don't want the answer.i've been trying to understand this for while.

-I have to use the toString method and have a test class.

-My problem is I don't understand how we'd update the invoice using just one constructor,and  a set method(in short,how to make the output appear like it should).I Feel that what I did is wrong.

-Original Question ,output both mine and the how it supposed to be are the attachments.

 

First class:

public class Invoice {

        private String PartNumber;
      private String Description; 
      private int Quantity ;
     private double Price ;
     
     public Invoice(String PartNumber,String Description,int Quantity,double Price){
         this.PartNumber=PartNumber;
         this.Description=Description;
         this.Quantity=Quantity;
         this.Price=Price;
       
     }
     public Invoice(){
        PartNumber="1234";
        Description ="Hammer";
        Quantity=2;
        Price=14.95;
         }
     
      public void setPartNumber(String PartNumber){
          this.PartNumber=PartNumber;
      }
      
      public void setDescription(String Description){
          this.Description=Description;
      }
      
      
       public void setQuantity(int Quantity){
          this.Quantity=Quantity;
      }
         public void setPrice(double Price){
          this.Price=Price;
      }
      
      public double getInvoiceAmount(){
          return Quantity*Price;
      }
      
      public String toString(){
             return String.format("Part Number:%s\nDescription:%s\nQuantity:%d\nPrice:%.2f\nInvoice Amount:%.2f",PartNumber,Description,Quantity,Price,getInvoiceAmount());
             } 
     
        public void display(){
            System.out.println(toString());
        }
    }

Capture.PNG

Capture22.PNG

Myoutput.PNG

Second class:


public class InvoiceTest {
    public static void main(String[] args) {
Invoice a1=new Invoice();
a1.display();
Invoice a2=new Invoice();
a2.setDescription("Yellow Hammer");
a2.setPrice(19.49);
a2.setQuantity(3);
a2.display();
Invoice a3=new Invoice("5678"," Paint Brush",0,0.0);
        a3.display();
        Invoice a4=new Invoice("5678"," Paint Brush",0,0.0);
        a4.setQuantity(3);
        a4.setPrice(4.94);
        a4.display();


    }
}
 

 

Link to comment
https://www.neowin.net/forum/topic/1358584-stuck-in-a-java-question/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

When they say "updated invoice" they mean you should modify an existing Invoice, not create a new one.

So in total you're going to call the constructor twice in your program.

To update each invoice you're going to use the `set..` methods and then call .toString() a second time on that same instance.

 

Your Invoice class should NOT have a default constructor (constructor with no parameters) that initializes it to the values of the first invoice example. You should use the constructor that takes in those initial values and assigns them immediately. And just delete the other one.

 

// Delete this code!!
public Invoice(){
        PartNumber="1234";
        Description ="Hammer";
        Quantity=2;
        Price=14.95;
         }

 

Does that make sense?

  • 0
  On 15/03/2018 at 17:31, Andre S. said:

When they say "updated invoice" they mean you should modify an existing Invoice, not create a new one.

So in total you're going to call the constructor twice in your program.

To update each invoice you're going to use the `set..` methods and then call .toString() a second time on that same instance.

 

Your Invoice class should NOT have a default constructor (constructor with no parameters) that initializes it to the values of the first invoice example. You should use the constructor that takes in those initial values and assigns them immediately. And just delete the other one.

 

// Delete this code!!
public Invoice(){
        PartNumber="1234";
        Description ="Hammer";
        Quantity=2;
        Price=14.95;
         }

 

Does that make sense?

Expand  

Thank you homie.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I think it's bloody great, old chap!
    • Not the deal that 2nd hand purchasers tend to look for - no.  It's cheaper than the sum of it's parts new - yes.  But people looking for 2nd hand, pre-built computers are rarely looking for what you are selling. Don't misunderstand; as an informed enthusiast I like that computer, and as an informed enthusiast I am often asked "My son wants a gaming PC and here's my budget" - in which case, as an informed enthusiast I would totally point them in this sort of direction.  But I am not the mass market, hence you may well have to wait longer for a sale. If you're looking for speed - then breaking it into parts is likely to pay off better on that front, as there will be more people who suit that market and their likelihood of buying 2nd hand is more fruitful.  But - chore!
    • What is the purpose of this article? You didn't even mention that CPU is powering it, so we don't know what GPU/iGPU it has. We don't know if the memory is on chip, on the motherboard, is expandable or replaceable. And that's only the internals. Display type, resolution, brightest you didn't even mention the DPI. Without one or more if these capabilities no one who reads these strung together words will know if they should or shouldn't buy it. The only HINT is from the sticker on the photo of the laptop. Q: what do you get for wasting people's time? Their must be some value or you wouldn't have wasted your time either.
    • That is some storage. I have an old F4-210 and it works well for what it is, just a shame Terramaster will not update the software for it.
  • Recent Achievements

    • Week One Done
      Yianis earned a badge
      Week One Done
    • Veteran
      Travesty went up a rank
      Veteran
    • One Month Later
      somar86 earned a badge
      One Month Later
    • Week One Done
      somar86 earned a badge
      Week One Done
    • Apprentice
      Adrian Williams went up a rank
      Apprentice
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      260
    3. 3
      +Edouard
      186
    4. 4
      +FloatingFatMan
      176
    5. 5
      snowy owl
      132
  • Tell a friend

    Love Neowin? Tell a friend!