Question

can you solve it in java please Create the following: 1. Class Invoice ( the node...

can you solve it in java please

Create the following:

1. Class Invoice ( the node ) that includes three instance variables:

    int No; // the Invoice No

            String CustName; // the Customer name

            int Amount; // the Invoice Amount

Invoice next; // points to the next Invoice

Default and overloaded constructors

2. Class Shop that includes three instance variables:

Invoice head;

Invoice Tail;

Your class should have the following:

• A method that initializes the instance variables.

void add (int, String, int) creates and adds the Invoice information ( No, CustName and Amount) in the singly linked list.

void delete (int) to delete an Invoice in the single linked list.

void printInvoices() prints all the Invoices in the single linked list.

int countInvoices () counts the number of Invoices in the list.

int Sum () counts the summation of the Invoices.

click in the file bellow to view the source code for add-head and delete methods

addHead-delete-methods.docx

Write a test application. In the main method, do the following:

• Create a Shop object.

• ask the user to enter the number of Invoices in the Shop.

• Input the No, CustName and Amount of each Invoice and add it to the Shop class (Hint: Use a loop).

• Print the Invoices.

• Input the No. of the Invoice to be deleted. Delete the Invoice

• Print the Invoices.

• Print the number of Invoice.

• Print the summation of all Invoices

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
//Invoice class
class Invoice{
   int no;//account no
   String custname;//custmer no
   int ammount;//ammount
   Invoice next;//pointing to next Invoice
   //Constructer
   public Invoice(int no,String custname,int ammount){
       this.no=no;
       this.custname=custname;
       this.ammount=ammount;
       next=null;
   }
   //toString method which gives Invoice
   public String toString(){
       return (no+" "+custname+" "+ammount);
   }
}
//shop class
class Shop{
  
   Invoice head;
   Invoice tail;
   Scanner sc;
   //Default constructer
   public Shop(){
       head=null;
       tail=null;
       sc=new Scanner(System.in);
   }
   //add method to add Invoice
   public void add(int no,String custname,int ammount){
       if(head==null){   //head==null
           head=new Invoice(no,custname,ammount);
           tail=head; //Intiall head and tail points to same node
       }
       else{  
           tail.next=new Invoice(no,custname,ammount);//adding Invoice at the end
           tail=tail.next;//moving tail
       }
   }
   //printint Invoices
   public void printInvoices(){
       Invoice temp=head;
       while(temp!=null){
           System.out.println(temp);
           temp=temp.next;
       }
   }
   //counting invoices
   public int countInvoices(){
       Invoice temp=head;
       int count=0;
       while(temp!=null){
           count++;
           temp=temp.next;
       }
       return count;
   }
   //counting invoices
   public int sum(){
       Invoice temp=head;
       int count=0;
       while(temp!=null){
           count=count+temp.ammount;
           temp=temp.next;
       }
       return count;
   }
   //deleteing a specifice account
   public void delete(int no){
       //if head exits
       if(head!=null){
           Invoice temp=head;
           //if first invoice account is equal to required account
           if(no==head.no){
               temp=null;
               head=head.next;
           }
           else{
               Invoice prevTemp=null;
               boolean found=false;
               //to find invoic account
               while(temp!=null){
                   if(no==temp.no){
                       found=true;    //account found
                       break;
                   }
                   prevTemp=temp;
                   temp=temp.next;
               }
               if(found){
                   //chaning links
              
                   Invoice freeTemp=temp;
                   prevTemp.next=temp.next;
                   freeTemp=null;
               }
           }
       }
   }
   public void menu(){
       System.out.println("Enter\n 1->add Invoice \n 2->print Invoices \n 3->delete Invoice \n 4->print number of invoice \n 5->print summation of invoice\n0->exit");
   }
   //createInvoice
   public void createInvioce(){
      
       System.out.printf("Enter accountno::");
       int no=sc.nextInt();
       System.out.printf("Enter Customername::");
       String custname=sc.next();
       System.out.printf("Enter ammount::");
       int ammount=sc.nextInt();
       this.add(no,custname,ammount);
  
   }
   //main method
   public static void main(String args[]){
       Shop s=new Shop();
       Scanner sc=new Scanner(System.in);
       int option,no;
       while(true){
           s.menu();
           System.out.println("Enter option");
           option=sc.nextInt();
           if(option==1){
               s.createInvioce();
           }
           else if(option==2){
               s.printInvoices();
           }
           else if(option==3){
               System.out.printf("Enter account no to delete::");
               no=sc.nextInt();
               s.delete(no);
           }
           else if(option==4){
               System.out.println("Total no of invoices::"+s.countInvoices());
           }
           else if(option==5){
               System.out.println("Total Summation of invoices::"+s.sum());
           }
           else if(option==6){
               break;
           }
       }
   }
}

nter 1-add Invoice 2->print Invoices 3-delete Invoice 4-print number of invoice xprint summation of invoice ->exit nter optio
Enter option otal no of invoices::3 Enter 1-add Invoice 2->print Invoices 3-delete Invoice 4-print number of invoice xprint s
Add a comment
Know the answer?
Add Answer to:
can you solve it in java please Create the following: 1. Class Invoice ( the node...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes...

    JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results: Use streams to sort the Invoice objects by partDescription, then display the results. Use streams to sort the Invoice objects by pricePerItem, then display the results. Use streams to...

  • In Java The following Java implementation of a class Node is given: private class Node<Object> {...

    In Java The following Java implementation of a class Node is given: private class Node<Object> { Node() { this(null, null); } Node(Object d) { this(d, null); } Node(Object d, Node n) { data = d; next = n; } Object data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the class Node described above, write a MySingleLinkedList...

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • You are now going to create a LinkedList class, that will work very similarly to the...

    You are now going to create a LinkedList class, that will work very similarly to the Stack class seen in the book (and used in the previous exercise). Then write new methods as follows: add ( LinkedList::Link* l, int n ): will insert in the linked list, after link l, a chain of n new links. Each link will store an integer that will be set to receive, in order, a value starting from 0 until n-1. In the last...

  • We have written the following Node class: class Node { // instance variables private int value;...

    We have written the following Node class: class Node { // instance variables private int value; private Node next; // constructor public Node(int value) { this.value = value; } // get the 'next' variable public Node getNext() { return this.next; } // get the 'value' variable public int getValue() { return this.value; } // set the 'next' variable public void setNext(Node next) { this.next = next; } } TASK: Create a class called List that has the following properties: It...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Ever...

    C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a "next" pointer. Textbook Туре String String String Attribute title author ISBN Textbook* next Library class should have a head node as an attribute to keep the list of the books. Also, following member...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT