• 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.