Question

You live in a big apartment block, and sorting mail and parcels is quite a complex...

You live in a big apartment block, and sorting mail and parcels is quite a complex task. Write a program that reads in a file with all the deliveries, asks for the user's name and prints out a summary of all the mail.

Your program should read in a file named mail.txt that looks like this:

Jane Fairfax,Letter
Frank Churchill,Letter
Emma Woodhouse,Letter
Frank Churchill,Letter
Harriet Smith,Package
Emma Woodhouse,Letter
Philip Elton,Package
Emma Woodhouse,Package

Your program should work like this:

Name: Emma Woodhouse
2 Letters
1 Package

Here is another example:

Name: Jane Fairfax
1 Letter
No Packages

If the person doesn't have any mail, your program should work like this:

Name: Elizabeth Bennet
No mail
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note:

#######

As the programming language is not specified in question, Java is used. Please give the correct file path. currently it is assumed mail.txt file is in same directory as that of program

//######################## PGM START ##################################

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class CheckMail {

   public static void main(String[] args) {
       String line[]=new String[2];
       int letterCount=0;
       int packageCount=0;
      
       //array list to store name and item
       ArrayList<String> n1=new ArrayList<String>();
       ArrayList<String> v1=new ArrayList<String>();
       Scanner s=new Scanner(System.in);
      
       Scanner file1=null;
       try {
           //reading the file mail.txt
           file1 = new Scanner(new File("mail.txt"));
           //while file has next line
           while(file1.hasNext()) {
               //read the line, split using ,
               line=file1.nextLine().split(",");
               //store the result in array list
               n1.add(line[0]);
               v1.add(line[1]);
           }
       } catch (FileNotFoundException e) {
           System.out.println("Cant find the file mail.txt in current folder");
           e.printStackTrace();
       }
       //request name from user
       System.out.print("Name: ");
       String name=s.nextLine();
       //iterate till end of array list
       for(int i=0;i<n1.size();i++ ) {
           //check if name matches
           if(name.equalsIgnoreCase(n1.get(i))) {
              
               //increment letter or package count if it is present
               if(v1.get(i).equalsIgnoreCase("Letter"))
                   letterCount++;
               else if(v1.get(i).equalsIgnoreCase("Package"))
                   packageCount++;
           }  
       }
      
       //print msg according to letter and package count
       if(letterCount>0)
           System.out.println(letterCount+" Letter");
       if(packageCount>0)
           System.out.println(packageCount+" Package");
       if(letterCount==0 && packageCount==0)
           System.out.println("No mail");
       else if(letterCount>0 && packageCount==0)
           System.out.println("No Packages");
       else if(letterCount==0 && packageCount>0)
           System.out.println("No Letters");
      
      
       s.close();
       file1.close();

   }

}


//############################## PGM END #############################

OUTPUT
#########

Add a comment
Know the answer?
Add Answer to:
You live in a big apartment block, and sorting mail and parcels is quite a complex...
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
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