Witam.
Jako że C# to mój pierwszy język obiektowy nie bardzo łapie jak budować program w ten sposób.
Miałem potrzebę napisania programu który będzie pobierał dane o zamówieniach ze sklepu internetowego (przez SOAP) i
na podstawie tych danych generował plik csv który następnie będzie wczytywany na stronie przewoźnika (Koloporter-Expres)
jako import przesyłek. Program działa ale NIE MA on nic wspólnego z obiektowością i tu pojawia się moja prośba o podpowiedzi (wskazówki) w jaki sposób przepisać go z uwzględnieniem tego typu programowania. Mile widziane również wskazówki jak uprościć kod.
Będę wdzięczny za każdą możliwą pomoc.
KOD C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication1.webapi;
using System.Xml;
using System.IO;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Webapi_Soap_ApiService service = new Webapi_Soap_ApiService();
string session;
XmlNode[] res;
Object result;
int idnr;
string csv_file = "D:\\paczki-" + DateTime.Today.ToShortDateString() + ".csv";
string[] addr;
string[] headers = { "USLUGA", ";", "ZLECENIODAWCA", ";", "PLATNIK", ";", "CK_ST", ";", "DATA_N", ";", "DATA_D", ";", "N_CK", ";", "N_NAZWA", ";", "N_ULICA", ";", "N_MIEJSCOWOSC", ";", "N_KOD_POCZTOWY", ";", "N_NR_DOMU", ";", "N_NR_LOK", ";", "N_OS_PRYW", ";", "N_NIP", ";", "N_OS_NADAJACA", ";", "N_TEL_ST", ";", "N_TEL_GSM", ";", "N_EMAIL", ";", "O_CK", ";", "O_NAZWA", ";", "O_ULICA", ";", "O_MIEJSCOWOSC", ";", "O_KOD_POCZTOWY", ";", "O_NR_DOMU", ";", "O_NR_LOK", ";", "O_OS_PRYW", ";", "O_NIP", ";", "O_OS_NADAJACA", ";", "O_TEL_ST", ";", "O_TEL_GSM", ";", "O_EMAIL", ";", "E_0", ";", "E_1", ";", "E_5", ";", "E_10", ";", "E_15", ";", "E_20", ";", "E_30", ";", "L_40", ";", "L_60", ";", "L_80", ";", "L_100", ";", "L_150", ";", "L_200", ";", "L_250", ";", "L_300", ";", "L_350", ";", "L_400", ";", "L_450", ";", "L_500", ";", "L_600", ";", "L_700", ";", "L_800", ";", "L_900", ";", "L_1000", ";", "ROD_1_TYP", ";", "ROD_1_OPIS", ";", "ROD_2_TYP", ";", "ROD_2_OPIS", ";", "ROD_3_TYP", ";", "ROD_3_OPIS", ";", "ROD_4_TYP", ";", "ROD_4_OPIS", ";", "ROD_5_TYP", ";", "ROD_5_OPIS", ";", "ROD_6_TYP", ";", "ROD_6_OPIS", ";", "ROD_7_TYP", ";", "ROD_7_OPIS", ";", "ROD_8_TYP", ";", "ROD_8_OPIS", ";", "ROD_9_TYP", ";", "ROD_9_OPIS", ";", "U_POD", ";", "U_UBEZP", ";", "U_WART_UBEZP", ";", "U_POBRANIE", ";", "U_WART_POBRANIA", ";", "U_RACH_POBRANIA", ";", "U_NAD_17", ";", "U_NAD_AW_TEL", ";", "U_NAD_AW_SMS", ";", "U_NAD_AW_MAIL", ";", "U_DOST_AW_TEL", ";", "U_DOST_AW_SMS", ";", "U_DOST_AW_MAIL", ";", "U_DOST_NSTD", ";", "U_DOST_GODZ", ";", "U_DOST_DRW", ";", "U_DOST_POTW_MAIL", ";", "U_DOST_POTW_SMS", ";", "U_BRAK_PALET", ";", "U_ROZLADUNEK", ";", "U_ADR", ";", "UWAGI", ";", "OPIS", ";", "NR_PRZESYLKI", ";", "NR_REF_1", ";", "NR_REF_2", ";", "DZ_CK", ";", "DZ_NAZWA", ";", "DZ_ULICA", ";", "DZ_MIEJSCOWOSC", ";", "DZ_KOD_POCZTOWY", ";", "DZ_NR_DOMU", ";", "DZ_NR_LOK", ";", "DZ_OS_PRYW", ";", "DZ_NIP", ";", "DZ_OS_NADAJACA", ";", "DZ_TEL_ST", ";", "DZ_TEL_GSM", ";", "DZ_EMAIL", ";", "ILOSC_NIESTANDARD", ";", "NIEST_WYSOKOSC", ";", "NIEST_DLUGOSC", ";", "NIEST_SZEROKOSC"};
string[] values = { "E", ";", "mój_nr", ";", "ZL", ";", "mój_nr", ";", "0", ";", "0", ";", "mój_nr", ";", "Sklep", ";", "adres1", ";", "Miasto", ";", "kod_p", ";", "adres2", ";", "", ";", "N", ";", "nip", ";", "imię i nazwisko", ";", "", ";", "telefon", ";", "mail", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "N", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "N", ";", "N", ";", "", ";", "N", ";", "", ";", "", ";", "N", ";", "N", ";", "N", ";", "N", ";", "N", ";", "N", ";", "N", ";", "N", ";", "", ";", "N", ";", "N", ";", "N", ";", "", ";", "N", ";", "N", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";", "", ";" };
public MainWindow()
{
InitializeComponent();
}
public void Login(string login1, string pass)
{
try
{
session = service.login(login1,pass);
LBtn.IsEnabled = false;
LBtn.IsDefault = false;
listBtn.IsEnabled = true;
listBtn.IsDefault = true;
nrbox.IsEnabled = true;
nrbox.Focus();
label9.Foreground = System.Windows.Media.Brushes.Green;
showlog("ID Sesji: " + session);
showlog("Zalogowano jako: " + Ltxt.Text);
}
catch
{
label9.Foreground = System.Windows.Media.Brushes.Red;
showlog("Logowanie nie powiodło się");
return;
}
}
public void call(int data)
{
try
{
result = service.call(session, "order.info", new Object[] { data, true });
showdata();
}
catch
{
label9.Foreground = System.Windows.Media.Brushes.Red;
showlog("Wystąpił błąd w funkcji call - order.info");
return;
}
}
public void csv_make(string file)
{
try
{
/* fname.Text, ";"
, lname.Text, ";"
, street.Text, ";"
, nr.Text, ";"
, city.Text, ";"
, code.Text, ";"
, price.Text,*/
FileStream fs = new FileStream(file, FileMode.CreateNew, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
foreach (string line in headers)
{
sw.Write(line);
}
sw.WriteLine();
foreach (string line in values)
{
sw.Write(line);
}
sw.WriteLine();
sw.Close();
label9.Foreground = System.Windows.Media.Brushes.Green;
showlog(headers.Rank.ToString());
showlog("Zapisano do pliku informacje o zamówieniu nr " + res[1].InnerText);
}
catch
{
label9.Foreground = System.Windows.Media.Brushes.Red;
showlog("Plik " + csv_file + " już istnieje.");
csv_add(file);
}
}
public void csv_add (string file)
{
try
{
FileStream fs = new FileStream(file, FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
foreach (string line in values)
{
sw.Write(line);
}
sw.WriteLine();
sw.Close();
label9.Foreground = System.Windows.Media.Brushes.Green;
showlog("Zapisano do pliku informacje o zamówieniu nr " + res[1].InnerText);
}
catch
{
label9.Foreground = System.Windows.Media.Brushes.Red;
showlog("Zapis do pliku nie powiódł się");
}
}
public void showlog(string data)
{
logLbox.Items.Add(data);
label9.Content = data;
}
public void showdata()
{
res = (XmlNode[])result;
fname.Inlines.Clear();
fname.Inlines.Add(res[36].ChildNodes[3].InnerText);
lname.Inlines.Clear();
lname.Inlines.Add(res[36].ChildNodes[4].InnerText);
values[40] = fname.Text + " " + lname.Text;
if (Int16.Parse(res[11].InnerText) == 10) pcheck.IsChecked = true;
else pcheck.IsChecked = false;
address.Inlines.Clear();
address.Inlines.Add(res[36].ChildNodes[9].InnerText);
if (res[36].ChildNodes[9].InnerText.StartsWith("ul."))
{
string[] addr = res[36].ChildNodes[9].InnerText.Remove(0, 3).Split(' ');
street.Clear();
street.Text = addr[0];
nr.Clear();
nr.Text = addr[1];
if (addr[1].Contains('/'))
{
string[] a = addr[1].Split('/');
values[48] = a[0];
values[50] = a[1];
}
else
{
values[48] = addr[1];
}
}
else if (res[36].ChildNodes[9].InnerText.StartsWith("ul "))
{
string[] addr = res[36].ChildNodes[9].InnerText.Remove(0, 3).Split(' ');
street.Clear();
street.Text = addr[0];
nr.Clear();
nr.Text = addr[1];
if (addr[1].Contains('/'))
{
string[] a = addr[1].Split('/');
values[48] = a[0];
values[50] = a[1];
}
else
{
values[48] = addr[1];
}
}
else if (res[36].ChildNodes[9].InnerText.StartsWith("ul"))
{
string[] addr = res[36].ChildNodes[9].InnerText.Remove(0, 2).Split(' ');
street.Clear();
street.Text = addr[0];
nr.Clear();
nr.Text = addr[1];
if (addr[1].Contains('/'))
{
string[] a = addr[1].Split('/');
values[48] = a[0];
values[50] = a[1];
}
else
{
values[48] = addr[1];
}
}
else
{
string[] addr = res[36].ChildNodes[9].InnerText.Split(' ');
street.Clear();
street.Text = addr[0];
nr.Clear();
nr.Text = addr[1];
if (addr[1].Contains('/'))
{
string[] a = addr[1].Split('/');
values[48] = a[0];
values[50] = a[1];
}
else
{
values[48] = addr[1];
}
}
values[42] = street.Text;
city.Inlines.Clear();
city.Inlines.Add(res[36].ChildNodes[7].InnerText);
values[44] = city.Text;
code.Inlines.Clear();
code.Inlines.Add(res[36].ChildNodes[8].InnerText);
values[46] = code.Text;
price.Inlines.Clear();
price.Inlines.Add(res[8].InnerText);
tel.Inlines.Clear();
tel.Inlines.Add(res[36].ChildNodes[13].InnerText);
values[60] = tel.Text;
ItemClear();
for (int i = 0; res[37].ChildNodes.Count > i; i++)
{
showitem(i + 1 + " " + res[37].ChildNodes[i].ChildNodes[8].InnerText);
values[192] = values[192] + ", " + res[37].ChildNodes[i].ChildNodes[8].InnerText;
}
values[192] = values[192].Remove(0, 2);
if (values[192].Length >= 250)
{
values[192] = values[192].Remove(248);
values[192] = values[192] + " ...";
}
label9.Foreground = System.Windows.Media.Brushes.Green;
showlog("Dane zamówienia " + res[1].InnerText);
showlog("Zamówienie nr " + res[1].InnerText + " - " + res[37].ChildNodes.Count.ToString() + " produktów");
}
public void ItemClear()
{
itemLbox.Items.Clear();
values[192] = "";
}
public void showitem(string data)
{
itemLbox.Items.Add(data);
}
public void check()
{
if (comboBox1.SelectedIndex == -1) MessageBox.Show("Wybierz wage przesyłki");
else if (comboBox1.SelectedIndex == 0) values[66] = "1";
else if (comboBox1.SelectedIndex == 1) values[68] = "1";
else if (comboBox1.SelectedIndex == 2) values[70] = "1";
if (pcheck.IsChecked.Value)
{
values[154] = "T";
values[156] = price.Text;
values[158] = "09249000050000400100040829";
}
if (itemLbox.Items.Count==0) values[192]="Zabawki";
}
public void LBtn_Click(object sender, RoutedEventArgs e)
{
Login(Ltxt.Text, Ptxt.Password);
}
public void listBtn_Click(object sender, RoutedEventArgs e)
{
idnr = Convert.ToInt32(nrbox.Text);
showlog(idnr.ToString());
call(idnr);
}
private void itemLbox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (itemLbox.SelectedItem != null)
{
String prod = itemLbox.SelectedItem.ToString();
MessageBox.Show(prod);
}
}
private void csvbut_Click(object sender, RoutedEventArgs e)
{
check();
csv_make(csv_file);
}
}
}
KOD XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Shoper To K-EX" Height="700" Width="838" >
<Grid>
<Button Content="Login" Height="57" IsDefault="True" HorizontalAlignment="Left" Margin="183,77,0,0" Name="LBtn" VerticalAlignment="Top" Width="109" Click="LBtn_Click" IsEnabled="True" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="57,77,0,0" Name="Ltxt" VerticalAlignment="Top" Width="120" Text="" />
<Label Content="Login" Height="28" HorizontalAlignment="Left" Margin="12,72,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Pass" Height="28" HorizontalAlignment="Left" Margin="12,106,0,0" Name="label2" VerticalAlignment="Top" />
<PasswordBox Height="23" HorizontalAlignment="Left" Margin="57,111,0,0" Name="Ptxt" VerticalAlignment="Top" Width="120" DataContext="{Binding}" Password="" />
<ListBox Height="273" HorizontalAlignment="Left" Margin="310,72,0,0" Name="itemLbox" VerticalAlignment="Top" Width="486" MouseDoubleClick="itemLbox_MouseDoubleClick" DataContext="{Binding}">
<ListBox.ContextMenu>
<ContextMenu ForceCursor="True" IsManipulationEnabled="True" />
</ListBox.ContextMenu>
</ListBox>
<ListBox Height="298" HorizontalAlignment="Left" Margin="310,351,0,0" Name="logLbox" VerticalAlignment="Top" Width="486" />
<Button Content="Pobierz" Height="23" HorizontalAlignment="Left" Margin="183,148,0,0" Name="listBtn" VerticalAlignment="Top" Width="109" Click="listBtn_Click" IsEnabled="False" />
<Label Content="Imię" Height="28" HorizontalAlignment="Left" Margin="39,184,0,0" Name="label3" VerticalAlignment="Top" />
<Label Content="Nazwisko" Height="28" HorizontalAlignment="Left" Margin="12,208,0,0" Name="label4" VerticalAlignment="Top" />
<Label Content="Ulica" Height="28" HorizontalAlignment="Left" Margin="19,0,0,257" Name="label5" VerticalAlignment="Bottom" />
<TextBlock Height="17" HorizontalAlignment="Left" Margin="84,190,0,0" Name="fname" Text="" VerticalAlignment="Top" Width="189" />
<TextBlock Height="17" HorizontalAlignment="Left" Margin="78,214,0,0" Name="lname" Text="" VerticalAlignment="Top" Width="214" />
<TextBox Height="21" HorizontalAlignment="Left" Margin="55,379,0,0" Name="street" Text="" VerticalAlignment="Top" Width="144" />
<TextBox Height="21" Margin="209,379,0,0" Name="nr" Text="" VerticalAlignment="Top" HorizontalAlignment="Left" Width="31" />
<TextBlock Height="17" HorizontalAlignment="Left" Margin="55,416,0,0" Name="city" Text="" VerticalAlignment="Top" Width="144" />
<TextBlock Height="17" HorizontalAlignment="Left" Margin="208,416,0,0" Name="code" Text="" VerticalAlignment="Top" Width="64" />
<Label Content="Wartość" Height="28" HorizontalAlignment="Left" Margin="16,258,0,0" Name="label6" VerticalAlignment="Top" />
<TextBlock Height="17" HorizontalAlignment="Left" Margin="78,267,0,0" Name="price" VerticalAlignment="Top" Width="80" />
<TextBlock Height="17" HorizontalAlignment="Left" Margin="78,239,0,0" Name="tel" Text="" VerticalAlignment="Top" Width="150" />
<Label Content="Telefon" Height="28" HorizontalAlignment="Left" Margin="22,232,0,0" Name="label7" VerticalAlignment="Top" />
<Label Content="Miasto" Height="28" HorizontalAlignment="Left" Margin="9,410,0,0" Name="label8" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="57,148,0,0" Name="nrbox" VerticalAlignment="Top" Width="120" IsEnabled="False" />
<Button Content="Zapisz" Height="24" HorizontalAlignment="Left" Margin="23,446,0,0" Name="csvbut" VerticalAlignment="Top" Width="269" Click="csvbut_Click" />
<Label Content="Witam!" Height="54" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label9" VerticalAlignment="Top" Width="784" FontSize="24" HorizontalContentAlignment="Center" FontWeight="Bold" FontFamily="Calibri" VerticalContentAlignment="Center" />
<Label Content="Waga" Height="28" HorizontalAlignment="Left" Margin="28,289,0,0" Name="label10" VerticalAlignment="Top" />
<CheckBox Content="Pobranie" HorizontalAlignment="Left" Margin="228,270,0,375" Name="pcheck" IsChecked="False" IsThreeState="False" />
<Label Content="Adres" Height="28" HorizontalAlignment="Left" Margin="139,317,0,0" Name="label11" VerticalAlignment="Top" FontSize="16" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="78,292,0,0" Name="comboBox1" VerticalAlignment="Top" Width="80" >
<ComboBoxItem Content="1 KG" IsSelected="True"/>
<ComboBoxItem Content="5 KG" />
<ComboBoxItem Content="10 KG" />
</ComboBox>
<TextBlock Height="24" HorizontalAlignment="Left" Margin="39,342,0,0" Name="address" Text="" VerticalAlignment="Top" Width="253" FontSize="16" TextAlignment="Center" />
</Grid>
</Window>
Screen aplikacji w załączniku