The Artima Developer Community
Sponsored Link

Java Answers Forum
Please help with swiching program to inher

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
brittany willis

Posts: 1
Nickname: brittrock
Registered: Apr, 2011

Please help with swiching program to inher Posted: Apr 21, 2011 7:12 PM
Reply to this message Reply
Advertisement
This is the two program I have to do but I have to take this and change it in to these two. Please help me understand what I have to do. Thank You

http://courses.muskingum.edu/@@/E36DA67BBD03BD5AB2A86906FB09CE0B/courses/1/SP11-CPSC-211-1/content/_36902_1/Lab3.pdf


http://courses.muskingum.edu/@@/E36DA67BBD03BD5AB2A86906FB09CE0B/courses/1/SP11-CPSC-211-1/content/_38016_1/Lab4.pdf

import java.util.Scanner;


public class Transaction
{
private int id;
private String type;
private double amount;

public boolean readTransaction (Scanner transScanner)
{
boolean result = false;

if (transScanner.hasNext())
{
id = transScanner.nextInt ();
type = transScanner.next ();
amount = transScanner.nextDouble ();

result = true;
}

return result;
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getType()
{
return type;
}

public void setType(String type)
{
this.type = type;
}

public double getAmount()
{
return amount;
}

public void setAmount(double amount)
{
this.amount = amount;
}
}


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lab2
{
public static void main (String [] args)
{
File custFile = new File ("customer.dat");
File transFile = new File ("transaction.dat");
File oldCustFile = new File("oldcustomer.dat");

// Make sure the files exist
if (checkForFileErrors (custFile, transFile))
return;

// Rename the customer file
custFile.renameTo(oldCustFile);

// Open the files for reading
Scanner custScanner = null;
Scanner transScanner = null;

try
{
custScanner = new Scanner (oldCustFile);
custScanner.useDelimiter("[:\r\n]+");

transScanner = new Scanner (transFile);
transScanner.useDelimiter("[:\r\n]+");
}
catch (FileNotFoundException e)
{
System.out.println ("Cound not open a file for reading");
return;
}

// Open the new customer file for writing
PrintWriter custWriter = null;

try
{
custWriter = new PrintWriter (custFile);
}
catch (FileNotFoundException e)
{
System.out.println ("Cound not open customer.dat for writing");
return;
}

// Process the files
try
{
Customer cust = new Customer ();
Transaction trans = new Transaction ();

// Read the first record of both files
boolean haveCustomer = cust.readCustomer (custScanner);
boolean haveTransaction = trans.readTransaction (transScanner);

// As long as we have customers
while (haveCustomer)
{
// Find transactions that match
while (haveTransaction && trans.getId() < cust.getId())
{
// We have a transaction, and the id is less than the current
// customer id. If we'd had a matching customer for this transaction,
// we wouldn't be reading it here, so this is an error.
System.out.println ("Transaction not matching any customer: id " + trans.getId() + ", type " + trans.getType() + ", amount " + trans.getAmount());
haveTransaction = trans.readTransaction (transScanner);
}

// Process transactions that have the same id
while (haveTransaction && trans.getId() == cust.getId())
{
if (trans.getType().equals("S"))
cust.setBalance(cust.getBalance() - trans.getAmount());

if (trans.getType().equals("P"))
cust.setBalance(cust.getBalance() + trans.getAmount());

haveTransaction = trans.readTransaction (transScanner);
}

// By the time we get here, we've run out of transactions for
// the current customer, so let's write out the customer to
// the new customer.dat
cust.writeCustomer (custWriter);

// And read in the next customer to process
haveCustomer = cust.readCustomer (custScanner);
}

// At this point, we've read through the entire oldcustomer.dat file
// We might still have unmatched transactions left over where the
// transaction id was greater than any of the customer ids, so let's process those

while (trans != null)
{
System.out.println ("Transaction not matching any customer: id " + trans.getId() + ", type " + trans.getType() + ", amount " + trans.getAmount());
haveTransaction = trans.readTransaction (transScanner);
}
}
catch (Exception e)
{
// Not the best way to handle this, but okay for this lab
e.printStackTrace ();
}


// Close the files
custScanner.close ();
transScanner.close ();
custWriter.close ();

}

public static boolean checkForFileErrors (File custFile, File transFile)
{
boolean errorFound = false;

if (! custFile.exists())
{
System.out.println ("No customer.dat found!");
errorFound = true;
}

if (! transFile.exists())
{
System.out.println ("No transaction.dat found!");
errorFound = true;
}

return errorFound;
}
}

import java.io.PrintWriter;
import java.util.Scanner;


public class Customer
{
private int id;
private String name;
private String address;
private double balance;

public boolean readCustomer (Scanner custScanner)
{
boolean result = false;

if (custScanner.hasNext())
{
id = custScanner.nextInt ();
name = custScanner.next ();
address = custScanner.next ();
balance = custScanner.nextDouble();

result = true;
}

return result;
}

public void writeCustomer (PrintWriter custWriter)
{
custWriter.println(id+":"+name+":"+address+":"+balance);
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getName()
{
return name;
}

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

public String getAddress()
{
return address;
}

public void setAddress(String address)
{
this.address = address;
}

public double getBalance()
{
return balance;
}

public void setBalance(double balance)
{
this.balance = balance;
}
}
Please help

Topic: Large arrays with lots of stuff Previous Topic   Next Topic Topic: Detecting oscilation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use