Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i have this java code but the for loop isnt working or looping can anyone fix it ? import java.util.Scanner; class shop { class invoice

i have this java code but the for loop isnt working or looping can anyone fix it ?

import java.util.Scanner;

class shop {

class invoice { int no; // the Invoice No String custName; // the Customer name int amount; // the Invoice Amount invoice next; // points to the next Invoice invoice previous;

invoice(int no, String custName, int amount) {

this.no = no;

this.custName = custName; this.amount=amount;

}

invoice() {

no = 0;

custName = ""; amount =0;

}

}

invoice head, tail;

int count;

public shop() { head=null; count = 0; invoice hNode = new invoice (); invoice tNode=new invoice(); hNode.next=tNode; head=hNode; tail=tNode;

} public void add(int no, String name, int amount){ if(head==null){ head=new invoice(no,name,amount); tail=head; } else{ invoice curr=head; while(curr.next!=null){ curr=curr.next; curr.next=new invoice(no,name,amount); tail=curr.next ; } } } public void delete(int no) {

if (head.next == tail) {

System.out.println("This invoice doesnt exist");

return;

}

invoice p = head;

Object item = p.no;

while (p != tail) {

if (p.no == no) {

p.next.previous = p.previous;

p.previous.next = p.next;

count--; } p = p.next;

}

} public void printinvoice() { invoice curr=head; while(curr!=null){ System.out.println(curr); curr=curr.next; } } int countinvoice(){ invoice curr=head; while(curr!=null){ count+=1; curr=curr.next; } return count; }

int sum(){ invoice curr=head; while(curr!=null){ count+=1; curr=curr.next; } return count; } public int countInvoice() {

return count;

}

} public class assignment{ public static void main(String[] args) { Scanner input= new Scanner(System.in); shop shop = new shop(); int n = 0; System.out.print("Enter the number of Invoices in the shop: "); n = input.nextInt(); while (n

int no = 0; String name; int amount = 0; for(int i = 1; i

System.out.print("Enter Customer Name: "); name = input.next();

System.out.print("Enter ammount: "); amount = input.nextInt(); while(amount

shop.add(no, name, amount); System.out.println(); } System.out.println();

System.out.println(" printing invoices "); shop.printinvoice(); System.out.println();

System.out.println("pritning number of invoices "); shop.countinvoice(); System.out.println(); int del; System.out.println("enter number of invoice to be deleted"); del=input.nextInt(); if(del!=n){ System.out.println("invoice doesnt exist"); } else{ shop.delete(del); System.out.println("invoice number"+del+"is deleted"); } System.out.println("Invoices count = " + shop.countInvoice());

} } image text in transcribed

run: Enter the number of Invoices in the shop: 3 Enter No: 1 Enter Customer Name: do Enter ammount: 23

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

3.4 Define HRIS and describe its main components.

Answered: 1 week ago

Question

Read the instructions

Answered: 1 week ago

Question

4. Did you rethink your decision?

Answered: 1 week ago

Question

3. Did you seek anyones advice?

Answered: 1 week ago

Question

7. What traps should she avoid?

Answered: 1 week ago