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

  • Context: Java 
  • Thread starter Thread starter lypena35
  • Start date Start date
  • Tags Tags
    Arrays Cart Java
Click For Summary
SUMMARY

The discussion centers on issues encountered while implementing a shopping cart in Java for ticket sales. The user struggles with correctly adding tickets to an array and calculating the total price, including taxes. Key problems identified include the incorrect implementation of the addToCart method and the calculation logic in calculateTotal. The user successfully resolved their issues after receiving guidance on isolating problematic code segments and focusing on specific errors.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of object-oriented programming concepts
  • Familiarity with arrays in Java
  • Basic knowledge of tax calculations in programming
NEXT STEPS
  • Review Java array manipulation techniques
  • Learn about Java exception handling to manage user input errors
  • Explore Java methods for calculating percentages and totals
  • Investigate best practices for debugging Java applications
USEFUL FOR

Java developers, software engineers, and students learning object-oriented programming who are interested in building e-commerce applications or managing shopping cart functionalities.

lypena35
Messages
18
Reaction score
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
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.
 
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!
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K