Home > Software design >  How to display customer table
How to display customer table

Time:05-02

I'm not a programmer and this is an assignment question. I wrote the program and its working fine but can't print the output in this specific format. Here down is my assignment:

        //pgm to find discount and amt
    import java.util.*;
    public class assg1
    {
        public void main()
        {
            int d,dis,t;
            Scanner sc=new Scanner(System.in);
            System.out.print("Enter the no. of customers: ");
            int x=sc.nextInt();
            {
                int i;
                for(i=1;;i  )
                {
                    System.out.print("Enter your name: ");
                    String a=sc.next();
                    System.out.println("Ticket charge: ");
                    int n=sc.nextInt();
                    if(n<25001)
                    d=2;
                    else if(n>=25001||n<=35000)
                    d=10;
                    else if(n>=35001||n<=55000)
                    d=12;
                    else if(n>=55001||n<=70000)
                    d=16;
                    else
                    d=18;
                    dis=d*n/100;
                    t=n-dis;
                    if(i==x)
                    break;
                }
            }
        }
    }

I did till this but dont know what to do in the part after t = n - dis. I tried using \t but the order of the output turns out differently everytime. If anyone has suggestions please let me know.

CodePudding user response:

The tricky bit is printing this so the title line and the entry are aligned. You can use System.out.printf with enough padding. One way to do it:

System.out.printf("%-5s %-20s %-15s %-15s %-15s%n", "Sl.No", "Name", "Ticket Charges", "Discount", "Net Amount");
System.out.printf("%-5d %-20s %-15d %-15d %-15d%n", i, a, n, d, t);

Explanation:

  • "%-20s" prints a string with at least 20 characters. If the string is shorter, it is padded with spaces to the right.
  • "%-20d" same, but for an integer.

This will print entries like the following:

Sl.No Name                 Ticket Charges  Discount        Net Amount     
1     Prasad Karkamkar     17000           2               16660   
Sl.No Name                 Ticket Charges  Discount        Net Amount   
2     Mia Goodwin          90000           10              81000              

CodePudding user response:

You have to make Customer class who have name, ticket charges, discount and net amount. You have to follow this instruction for solve your problem.

  • Make array of Customer class in main method.
  • You condition for discount is wrong. I fix it in here down code.
  • Java main method is always static otherwise you get exception Main method is not static in class so, change public void main() to public static void main(String args[])
  • Read Java Naming Conventions, Class name start from upper case letter.

Here down is code:

import java.util.*;
class Customer
{
    int discount;
    String name;
    double ticketCharge, netAmount, totalDiscount;
    
    // take input from customer
    void readInput()
    {
        Scanner sc1 = new Scanner(System.in);
        System.out.print("Enter your name: ");
        name = sc1.next();
        System.out.println("Ticket charge: ");
        ticketCharge = sc1.nextInt();
    }
    
    // print name
    void showName()
    {
        System.out.print(name);
    }
    
    // print ticket charge
    void showTicketCharge()
    {
        System.out.print(ticketCharge);
    }
    
    // Total discount got by customer as per ticket price
    void showTotalDiscount()
    {
        // codition start from higher ticket charge not lower ticket charge
        if(ticketCharge > 70000)
        {
            discount = 18;
        }
        else if(ticketCharge >= 55001 || ticketCharge <= 70000)
        {
            discount = 16;
        }
        else if(ticketCharge >= 35001 || ticketCharge <= 55000)
        {
            discount = 12;
        }
        else if(ticketCharge >= 25001 || ticketCharge <= 35000)
        {
            discount = 10;
        }
        else
        {
            discount = 2;
        }
        
        totalDiscount = discount * ticketCharge / 100;
        System.out.print(totalDiscount);
    }
    
    // print net amount
    void showNetAmount()
    {
        netAmount = ticketCharge - totalDiscount;
        System.out.print(netAmount);
    }
}


class Assg1
{
    public static void main(String args[])
    {
        int siNo = 1;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the no. of customers: ");
        int noOfCustormer = sc.nextInt();
        
        // Make array of customer
        Customer customer[] = new Customer[noOfCustormer];
        for(int i = 0; i < noOfCustormer; i  )
        {
            // give a input to the customer
            customer[i] = new Customer();
            customer[i].readInput();
            System.out.print("\n");
        }
        System.out.print("SI.No.\tName\tTicket charges\tDiscount\tNet Amount\n");
        for(int i = 0; i < noOfCustormer; i  )
        {
            // get a output of customer
            System.out.print(siNo);
            System.out.print("\t");
            customer[i].showName();
            System.out.print("\t");
            customer[i].showTicketCharge();
            System.out.print("\t\t");
            customer[i].showTotalDiscount();
            System.out.print("\t\t");
            customer[i].showNetAmount();
            System.out.print("\n");
            siNo  ;
        }

    }
}

Output:

Enter the no. of customers: 2
Enter your name: Jack
Ticket charge: 
56000

Enter your name: Michael
Ticket charge: 
36000

SI.No.  Name    Ticket charges  Discount        Net Amount
1       Jack    56000.0         8960.0          47040.0
2       Michael 36000.0         5760.0          30240.0
  • Related