- #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
//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();
}
else{
System.out.println("Unrecognized input, try again");
}
}
}
}
[/CODE]
This is my Ticket class and it works perfectly
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();
}
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);
}
}