JAVA.
Main()
When submitting this lab, submit a .java file calledShoppingList4, and create the following structure in Eclipse:
For this assignment you will be adding an important function tothe shopping list you created earlier in the course; Open List.
When the user selects Open List from the menu, the program willask if they are sure they want to overwrite the current shoppinglist being edited with the file on the fixed storage device andvalidate that the user responds with a Y or N.
openList(Scanner, shoppingList, FILENAME):
This function will return the shopping list (original orOpened) back to main().
Example Run
1. Add Items
2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: TerminateInvalid response. Please enter a choice from the menu(1-7).
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 6Are you sure you wish to overwrite your current shopping listwith the 'Shopping List.txt' file? (Y/N)sureInvalid response. Please answer with a 'Y' or 'N'Are you sure you wish to overwrite your current shopping listwith the 'Shopping List.txt' file? (Y/N)y
File Error: File 'Shopping List.txt' not found! Please savethe list before trying to open it.
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 1
Add an item to the list (or just hit 'ENTER' when done):Apples:6'Apples:6' has been added to the Shopping List.
Add an item to the list (or just hit 'ENTER' whendone):1 items have been added to your Shopping List.
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 5Are you sure you wish to overwrite the 'Shopping List.txt' filewith your current list? (Y/N)nShopping List not saved.
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 5Are you sure you wish to overwrite the 'Shopping List.txt' filewith your current list? (Y/N)yShopping List saved to file: Shopping List.txt.
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 1
Add an item to the list (or just hit 'ENTER' when done):Oranges:4'Oranges:4' has been added to the Shopping List.
Add an item to the list (or just hit 'ENTER' whendone):1 items have been added to your Shopping List.
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 3
---------------------Shopping List---------------------Apples 6Oranges 4---------------------
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 6Are you sure you wish to overwrite your current shopping listwith the 'Shopping List.txt' file? (Y/N)nShopping List not opened.
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 3
---------------------Shopping List---------------------Apples 6Oranges 4---------------------
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 6Are you sure you wish to overwrite your current shopping listwith the 'Shopping List.txt' file? (Y/N)yShopping List overwritten from file: ShoppingList.txt.
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 3
---------------------Shopping List---------------------Apples 6---------------------
1. Add Items2. Delete Items3. Show Items4. Sort Items5. Save List6. Open List7. ExitPlease enter a command: 7
Goodbye
MY SHOPPING LIST TO USE
import java.io.FileNotFoundException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Scanner;
public class ShoppingList2 {
public static void main(String[] args) {// TODO Auto-generated method stub
Scanner sIn = new Scanner(System.in);ArrayList<String> shoppingList = newArrayList<String>();String choice;boolean done = false;do{System.out.println();System.out.println("1. Add Items");System.out.println("2. Delete Items");System.out.println("3. Show Items");System.out.println("4. Sort Items");System.out.println("5. Save List");System.out.println("6. Exit");System.out.print("Please enter a command: ");choice = sIn.nextLine();switch (choice){case "1":System.out.println(addItems(sIn, shoppingList) + " items have beenadded to your Shopping List.");break;case "2":System.out.println(deleteItems(sIn, shoppingList) + " items havebeen deleted from your Shopping List.");break;case "3":showItems(shoppingList);break;case "4":sortItems(shoppingList);break;case "5":saveList(sIn, shoppingList, "ShoppingList.txt");break;case "6":System.out.println();System.out.println("Goodbye");done = true;break;default: System.out.println("Invalid response. Please enter achoice from the menu (1-5).");}} while (!done);sIn.close();}//-----------------methods------------------public static void sortItems(ArrayList<String>shoppingList){for (int i = 0; i < shoppingList.size(); i++){for (int j = i + 1; j < shoppingList.size(); j++){if (shoppingList.get(i).compareTo(shoppingList.get(j)) >0){String s = shoppingList.get(i);shoppingList.set(i, shoppingList.get(j));shoppingList.set(j, s);}}}System.out.println("The Shopping List has been sorted.");showItems(shoppingList);}
public static void showItems(ArrayList<String>shoppingList){System.out.println("---------------------------");System.out.println("Shopping List");System.out.println("---------------------------");for(int c = 0; c < shoppingList.size(); c++){int pos = shoppingList.get(c).indexOf(":");String name = shoppingList.get(c).substring(0, pos);String price = shoppingList.get(c).substring(pos + 1);System.out.println(name + " " + price);}System.out.println("---------------------------");}
public static String deleteItems(Scanner sIn,ArrayList<String> shoppingList){
String itemToDelete;int count = 0;do{System.out.print("Delete an item from the list (or just hit 'ENTER'when done): ");itemToDelete = sIn.nextLine();if (itemToDelete.length() > 0 &&!shoppingList.contains(itemToDelete)){System.out.println("Invalid Response! " + itemToDelete + " is NOTan item in the list.");showItems(shoppingList);}else if (itemToDelete.length() > 0 &&shoppingList.contains(itemToDelete)){System.out.println(itemToDelete + " has been deleted from theShopping List.");shoppingList.remove(itemToDelete);count++;}} while (itemToDelete.length() > 0);return "" + count;}
public static String addItems(Scanner sIn,ArrayList<String> shoppingList){String itemToAdd;int count = 0;do{System.out.print("Add an item to the list (or just hit 'ENTER' whendone): ");itemToAdd = sIn.nextLine();if (itemToAdd.length() > 0){int pos = itemToAdd.indexOf(":");if(pos != -1){System.out.println(itemToAdd + " has been added to the ShoppingList.");shoppingList.add(itemToAdd);count++;}elseSystem.out.println("Invalid Entry. No ':' found. Entry must be inthe form '<item>:<amount>'");}} while (itemToAdd.length() > 0);return "" + count;}public static void saveList(Scanner sc, ArrayList<String>shoppingList , String FILENAME) {PrintWriter pw=null;//declaring PrintWriter object to write tofileSystem.out.println("Are you sure you wish to overwrite the'Shopping List.txt' file with your current list? (Y/N) ");String ans=sc.nextLine();//taking user responseif(ans.equalsIgnoreCase("y")) {//if answer is y or Ytry {pw=new PrintWriter(FILENAME);//Opening the file} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}//--------------//looping over the total shopping listfor(int c = 0; c < shoppingList.size(); c++){pw.println(shoppingList.get(c));//writing each item to thefile}pw.close();//closing the fileSystem.out.println("Shopping List saved to file: ShoppingList.txt.");}else//otherwise do not saveSystem.out.println("Shopping List did NOT saved to file ");System.out.println("---------------------------");}
}
JAVA. Main() When submitting this lab, submit a .java file called ShoppingList4, and create the following structure in E
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am