Question

Below, you can find the description of your labwork for today. You can also find the...

Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section.

You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections.

In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define an ArrayList<Customer> object.

You also need to change the necessary code when accessing these lists, so that your existing code works without any errors.

I HAVE A CODE LIKE THIS IN JAVA, CAN YOU MODIFY CODE:

package labwork5;

import java.util.Scanner;

enum StockValue {
   AAA("Triple A (top-tier)"), AAB("Double A – B"), ABB("A – Double B"),
   BBB("Triple B (lowest recommended for credibility)"), BBC("Double B – C"), BCC("B – Double C"),
   CCC("Triple C (lowest quality)");

   private String str;

   StockValue(String str) {
       this.str = str;
   }

   public String getValue() {
       return str;
   }
}

public class Stock extends Item {

   private StockValue rating;
   private int numberOfShares;

   public Stock() {
       this(null);
   }

   public Stock(String unrated) {
       super();
       Scanner sc = new Scanner(System.in);
       System.out.print("Please enter company name: ");
       setName(sc.nextLine());
       if (unrated == null) {
           System.out.print("Please enter rating: ");
           rating = StockValue.valueOf(sc.nextLine());
       } else {
           rating = StockValue.valueOf(unrated);
       }
       System.out.print("Please enter stock price: ");
       setPrice(Double.parseDouble(sc.nextLine()));
       System.out.print("Please enter number of the shares: ");
       numberOfShares = Integer.parseInt(sc.nextLine());
       System.out.println();
       System.out.println("ID #" + getID() + " is given for Stock " + getName() + "!");
   }

   public void displayInformation() {
       super.displayInformation();
       System.out.println("Company name is: " + getName());
       System.out.println("Rating is: " + rating.getValue());
       System.out.println("Stock price is: " + getPrice());
       System.out.println("Number of shares is: " + numberOfShares);
   }

   public StockValue getRating() {
       return rating;
   }

   public void setRating(StockValue rating) {
       this.rating = rating;
   }

---

package labwork5;

import java.util.Date;

public class Trade {
  
   private Item item;
   private Date date;

   public Trade(Item item) {
       this.item = item;
       date = new Date();
   }

   public void displayTradeInfo() {
       System.out.println("Trade Date:" + date);
       item.displayInformation();
   }

}

   public int getNumberOfShares() {
       return numberOfShares;
   }

   public void setNumberOfShares(int numberOfShares) {
       this.numberOfShares = numberOfShares;
   }

}
-----

package labwork5;

public class Item {
  
   private static int counter = 0;
   private int ID;
   private String name;
   private double price;
  
   public Item() {
       counter++;
       ID = counter;
   }
  
   public void displayInformation() {
       System.out.println("ID# is " + getID());
   }

   public static int getCounter() {
       return counter;
   }

   public static void setCounter(int counter) {
       Item.counter = counter;
   }

   public int getID() {
       return ID;
   }

   public void setID(int iD) {
       ID = iD;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }

}
---

package labwork5;

import java.util.Scanner;

public class StockTest {

   public static void main(String[] args) {

       Customer[] customerArray = new Customer[10];
       Item[] itemArray = new Item[10];

       int cCounter = 0;
       int iCounter = 0;

       Scanner sc = new Scanner(System.in);

       while (true) {
           System.out.println("1. Create a new Customer");
           System.out.println("2. Create a new Item");
           System.out.println("3. Initiate a Trade");
           System.out.println("4. Display all customers");
           System.out.println("5. Display all items");
           System.out.println("0. Exit");
           int choice = sc.nextInt();

           System.out.println();

           if (choice == 1) {
               customerArray[cCounter++] = new Customer();
               System.out.println();
           } else if (choice == 2) {
               System.out.println("1. Create a new Stock");
               System.out.println("2. Create a new Unrated Stock");
               System.out.println("3. Create a new MoneyPair");
               choice = sc.nextInt();
              
               System.out.println();

               if (choice == 1) {
                   itemArray[iCounter++] = new Stock();
                   System.out.println();
               } else if (choice == 2) {
                   itemArray[iCounter++] = new Stock("CCC");
                   System.out.println();
               } else if (choice == 3) {
                   itemArray[iCounter++] = new MoneyPair();
                   System.out.println();
               }
           } else if (choice == 3) {
               System.out.print("Please enter a customer ID: ");
               int cID = sc.nextInt();
               System.out.print("Please enter an item ID: ");
               int sID = sc.nextInt();
               int i, j;
               for (i = 0; i < cCounter; i++) {
                   if (cID == customerArray[i].getId()) {
                       break;
                   }
               }
               for (j = 0; j < iCounter; j++) {
                   if (sID == itemArray[j].getID()) {
                       break;
                   }
               }
               if (i != cCounter && j != iCounter) {
                   Trade tmp = new Trade(itemArray[j]);
                   customerArray[i].setOpenTrade(tmp);
               } else {
                   System.out.println("Given Customer ID or Stock ID is not found!");
               }
               System.out.println();
           } else if (choice == 4) {
               for (int i = 0; i < cCounter; i++) {
                   customerArray[i].displayCustomerInfo();
                   System.out.println();
               }
           } else if (choice == 5) {
               for (int j = 0; j < iCounter; j++) {
                   itemArray[j].displayInformation();
                   System.out.println();
               }
           } else {
               break;
           }

       }

   }

}

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

well, you have missed to provide some of the classes like Customer.java, MoneyPair.java.

Although I have improvised and set this up for you.I have added collections to StockTest.java as that is the only place it is applicable. please fine below the code. Hope this helps.

 package labwork5; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class StockTest { public static void main(String[] args) {         Boolean[] present = {false}; List<Customer> customerList = new ArrayList<Customer>(); List<Item> itemList = new ArrayList<Item>(); Scanner sc = new Scanner(System.in); while (true) { System.out.println("1. Create a new Customer"); System.out.println("2. Create a new Item"); System.out.println("3. Initiate a Trade"); System.out.println("4. Display all customers"); System.out.println("5. Display all items"); System.out.println("0. Exit"); int choice = sc.nextInt(); System.out.println(); if (choice == 1) {    customerList.add(new Customer()); System.out.println(); } else if (choice == 2) { System.out.println("1. Create a new Stock"); System.out.println("2. Create a new Unrated Stock"); System.out.println("3. Create a new MoneyPair"); choice = sc.nextInt(); System.out.println(); if (choice == 1) { itemList.add(new Stock()); } else if (choice == 2) { itemList.add(new Stock("CCC")); } else if (choice == 3) { itemList.add(new MoneyPair()); } System.out.println(); } else if (choice == 3) {    present[0]=false; System.out.print("Please enter a customer ID: "); int cID = sc.nextInt(); System.out.print("Please enter an item ID: "); int sID = sc.nextInt(); /** * Assumption: There is always one unique id per customer. * There is always one unique id per item. * * Use streams api along with collections to filter first * the item id of the user, and then go to customer Id of the user * and attach the item to it. */ itemList .stream()     .filter(p -> p.getID() == sID)       .forEach(a -> {customerList.stream()                                          .filter(p -> p.getId() == cID)                                       .forEach(c ->                                                               { c.setOpenTrade(new Trade(a));                                                                  present[0]=true;                                                               });                                      }); if(!present[0]) { System.out.println("Given Customer ID or Stock ID is not found!"); } System.out.println(); } else if (choice == 4) {      //use streams foreach with collections to display customer info.        customerList.stream().forEach(c -> c.displayCustomerInfo()); System.out.println(); } else if (choice == 5) {         //user streams foreach with collections to display information of       //item          itemList.stream().forEach(c -> c.displayInformation()); System.out.println(); } else { break; } } sc.close(); } }

Please find below screenshot/pic too, just in case the code above gets scrambled (even though I have applied formatting from the HomeworkLib website).

Add a comment
Know the answer?
Add Answer to:
Below, you can find the description of your labwork for today. You can also find the...
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
  • Here is everything I have on my codes, the compiler does not allow me to input...

    Here is everything I have on my codes, the compiler does not allow me to input my name, it will print out "Please enter your name: " but totally ignores it and does not allow me to input my name and just proceeds to my result of ID and Sale. Please help. I've bolded the part where it I should be able to input my name package salesperson; public class Salesperson { String Names; int ID; double Sales; // craeting...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Need help debugging. first class seems fine. second class is shooting an error on s =...

    Need help debugging. first class seems fine. second class is shooting an error on s = super.getString(prompt);   third class is giving me an error in the while loop where int num = console.getInt("Enter an integer:"); //-------------------------------------------------------------------------- import java.util.Scanner; public class Console {     private Scanner sc;     boolean isValid;     int i;     double d;        public Console()     {         sc = new Scanner(System.in);     }     public String getString(String prompt)     {         System.out.print(prompt);         return sc.nextLine();...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

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