Shopping cart object problems using methods, and arrays no arrayLists. java

In summary: Int(); //System.out.println("Option selected-"+option); //If the user selects option 1, the method setClientName will be called. //It will ask for the input of the client name. //The method printShoppingCart will be called to print the information of the shopping cart. }Input your option: 1. Set client nameEnter client name
  • #1
lypena35
18
0
Hello, I cannot seem to get the calculations correct for my shopping cart to print out the tickets and total how many tickets and how much they are. The first set of code is for the shopping cart the comment sections are for what is suppose to be happening in each section. I have the menu working and the ticket class working but getting the program to add a new ticket to the array and total the number of tickets with the price is where I am having problems with. Thank you.
This is my shopping cart object that goes along with the ticket class this is where my problem is
Code:
/**
*You have been given the task of creating a program that creates a shopping cart for an online company that sells tickets for events. Your program will have a menu that allows a user to create a shopping cart, add tickets and checkout. For simplicity, the user will input the information of the ticket.
**/

import java.util.Scanner;

public class ShoppingCart2{
private String clientName;
private Ticket [] ticketsPurchased = new Ticket [20];
private int ticketNumber = 0;
private double totalPurchase;
private double individualTickets;

    //Default constructor
public ShoppingCart2(){

}

    //second constructor for the class that receives as parameter the clientName.
    
public ShoppingCart2(String clientNameIn){
this.clientName = clientNameIn;
}

    //Setter method for the attribute name
public void setClientName(String clientNameIn){
this.clientName = clientNameIn;
}
//Setter method for the arrtibute totalPurchase
public void setTotalPurchase(double totalPurchaseIn){
this.totalPurchase = totalPurchaseIn;
}

//Getter Method for the attribute name
public String getClientName(){
return this.clientName;
}

public double getTotalPurchase(){
return this.totalPurchase;
}

    
    //other methods-Actuator methods
    
    //the method addToCart that adds a new ticket into the array of purchased tickets.
    //This method receives newTicket, an instance of the class Ticket, adds it to the list
    //of ticketsPurchased and makes the newTicket unavailable. 
    
public void addToCart(Ticket newTicket){
	addToCart(newTicket);
	newTicket.setAvailable(false);
	ticketNumber ++;
	
}
//The method calculateTotal that calculates the total purchase by:
    //1. Adding the price of all the tickets in the list
    //2. Adding 15% of taxes.
    //3. Set the totalPurchase to the total purchase calculated.
 [B]This part[/B]   
[CODE]public void calculateTotal(){
    double total=0;
    
    //This statement adds the price of individual tickets.
    individualTickets+=ticketNumber;
   
    
    //This statement assigned the value of the total purchase to the total +15%.
     totalPurchase=individualTickets +.15;
}
    
  //The method printShoppingCart prints this information of the shopping cart (each ticket).
[B]This part[/B]
    public void printShoppingCart(){
    for (int j=0;j<totalPurchase;j++) {}   
    }

//Main method of the class ShoppingCart which creates an instance of the class
//shopping cart and enables input user with a menu.

public static void main(String[] args){
ShoppingCart myShoppingCart=new ShoppingCart();
Scanner input= new Scanner(System.in);
String answ="";
int option=0;
String name;

//While method for the menu options

while(option<=2){
System.out.println("1. Set client name");
System.out.println("2. Add tickets to the shopping cart");
System.out.println("3. Checkout");
System.out.print("Input your option: ");
option=input.nextInt();
System.out.println("Option selected-"+option);
if(option==1){
//Option 1 will request input for the client name.
System.out.print("\n Enter client name:");
name=input.next();
}
Code:
   //Option 3 will create a new ticket, request the input of ticket information
                //and add the new ticket to the shopping cart with the method addToCart.
[B]This is where the new tickets are suppose to be entered[/B]
           if(option==2){
               System.out.print("\n Enter the event of the ticket:");
                Ticket ticket=new Ticket();
                myShoppingCart.printShoppingCart();
           }
[B]This is where we are suppose to get the total[/B]
            //Option 3 will do the checkout by:
                //Calculating the total of the shopping cart,
                //printing the contents of the shopping cart and exiting the program.
            if(option==3){
               System.out.println("Your total is: "); myShoppingCart.calculateTotal();
            }
else{
System.out.println("Unrecognized input, try again");
}
}
}
}
[/CODE]

This is my Ticket class and it works perfectly
Code:
/***
 **Class Ticket
 **
 ***/
public class Ticket {

 //Attributes
 private String event;
 private String seat;
 private float price;
 private boolean available;

 //Default Constructor
 public Ticket() {}

 //Second Constructor

 public Ticket(String eventIn, String seatIn, float priceIn, boolean availableIn) {
  this.event = eventIn;
  this.seat = seatIn;
  this.price = priceIn;
  this.available = availableIn;
 }

 //Third Constructor

 public Ticket(String eventIn, String seatIn, float priceIn) {
  this.event = eventIn;
  this.seat = seatIn;
  this.price = priceIn;
  this.available = true;
 }

 //Setters

 public void setEvent(String eventIn) {
  this.event = eventIn;
 }

 public void setSeat(String seatIn) {
  this.seat = seatIn;
 }

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

 public void setAvailable(boolean availableIn) {
  this.available = availableIn;
 }

 //Getters

 public String getEvent() {
  return this.event;
 }

 public String getSeat() {
  return this.seat;
 }

 public float getPrice() {
  return this.price;
 }

 public boolean getAvailable() {
  return this.available;
 }

 //Actuators/Method

 public void printTicket() {
  System.out.print("Event: " + this.event + "," + " Seat: " + seat+",");
  System.out.printf(" Price: $%.2f", this.price);
  if (this.available)
   System.out.println(", AVAILABLE.");
  else
   System.out.println(", UNAVAILABLE.");
 }

 //Define main method

 public static void main(String[] args) {

  Ticket newTicket = new Ticket("The Nutcracker", "2A", 75);
  newTicket.printTicket();
  newTicket.setAvailable(false);
     
 }

}
 
Technology news on Phys.org
  • #2
Please describe what each of the following variables is supposed to store.

Code:
private Ticket [] ticketsPurchased = new Ticket [20];
private int ticketNumber = 0;
private double totalPurchase;
private double individualTickets;

Please describe in words what the method [m]addToCart[/m] should do and how it should do it. Be specific.

Please describe how to change $x$ in order to increase it by 15%.

In the future, please try to avoid posting long segments of code. Don't post parts that work; instead try to isolate the problematic part as much as possible. Provide the description that is often required for bug reports: a short description of the problem, expected behavior, actual behavior. If your problem is with math (such as adding 15% of taxes), there is no reason to post code.
 
  • #3
Thank you very much for your help and tip on just posting the problem code. I was able to finally fix all the issues. However, I have another assignment to make a fake credit card encryption type project. If I run into any problems with it, I will be sure to post only the problem code. Thanks again!
 

What is a shopping cart object?

A shopping cart object is a programming concept used to store and manage information about items that a customer wishes to purchase. It typically contains properties such as item name, price, quantity, and total cost.

How are methods used in shopping cart objects?

Methods are used to perform actions on shopping cart objects, such as adding or removing items, calculating total cost, and displaying the contents of the cart. They allow for more efficient and organized code by encapsulating specific behaviors within the object.

Why are arrays used in shopping cart objects?

Arrays are used to store multiple items in a shopping cart object. They allow for easy access and manipulation of the data, such as adding or removing items, updating quantities, and displaying all items in the cart.

What are the limitations of not using ArrayLists in shopping cart objects?

Without ArrayLists, shopping cart objects may have limited functionality and be more difficult to manage. For example, it may be more challenging to add or remove items, keep track of quantities, and display the contents of the cart. Additionally, arrays have a fixed size, so the cart may not be able to hold an unlimited number of items.

How can I troubleshoot shopping cart object problems in Java?

If you encounter issues with shopping cart objects in Java, some troubleshooting steps you can take include checking for errors in your code, ensuring that all methods and variables are properly declared and used, and testing your code with different scenarios to identify any bugs. You can also seek help from online resources or consult with other programmers for assistance.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
774
  • Programming and Computer Science
Replies
4
Views
834
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
636
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
Back
Top